| 1 | # |
| 2 | # Copyright (C) 2019 EfficiOS Inc. |
| 3 | # |
| 4 | # This program is free software; you can redistribute it and/or |
| 5 | # modify it under the terms of the GNU General Public License |
| 6 | # as published by the Free Software Foundation; only version 2 |
| 7 | # of the License. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU General Public License for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU General Public License |
| 15 | # along with this program; if not, write to the Free Software |
| 16 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | # |
| 18 | |
| 19 | import unittest |
| 20 | import bt2 |
| 21 | from bt2 import connection as bt2_connection |
| 22 | |
| 23 | |
| 24 | class ConnectionTestCase(unittest.TestCase): |
| 25 | def test_create(self): |
| 26 | class MyIter(bt2._UserMessageIterator): |
| 27 | def __next__(self): |
| 28 | raise bt2.Stop |
| 29 | |
| 30 | class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): |
| 31 | def __init__(self, params, obj): |
| 32 | self._add_output_port('out') |
| 33 | |
| 34 | class MySink(bt2._UserSinkComponent): |
| 35 | def __init__(self, params, obj): |
| 36 | self._add_input_port('in') |
| 37 | |
| 38 | def _user_consume(self): |
| 39 | raise bt2.Stop |
| 40 | |
| 41 | graph = bt2.Graph() |
| 42 | src = graph.add_component(MySource, 'src') |
| 43 | sink = graph.add_component(MySink, 'sink') |
| 44 | conn = graph.connect_ports(src.output_ports['out'], sink.input_ports['in']) |
| 45 | self.assertIs(type(conn), bt2_connection._ConnectionConst) |
| 46 | |
| 47 | def test_downstream_port(self): |
| 48 | class MyIter(bt2._UserMessageIterator): |
| 49 | def __next__(self): |
| 50 | raise bt2.Stop |
| 51 | |
| 52 | class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): |
| 53 | def __init__(self, params, obj): |
| 54 | self._add_output_port('out') |
| 55 | |
| 56 | class MySink(bt2._UserSinkComponent): |
| 57 | def __init__(self, params, obj): |
| 58 | self._add_input_port('in') |
| 59 | |
| 60 | def _user_consume(self): |
| 61 | raise bt2.Stop |
| 62 | |
| 63 | graph = bt2.Graph() |
| 64 | src = graph.add_component(MySource, 'src') |
| 65 | sink = graph.add_component(MySink, 'sink') |
| 66 | conn = graph.connect_ports(src.output_ports['out'], sink.input_ports['in']) |
| 67 | self.assertEqual(conn.downstream_port.addr, sink.input_ports['in'].addr) |
| 68 | self.assertIs(type(conn), bt2_connection._ConnectionConst) |
| 69 | |
| 70 | def test_upstream_port(self): |
| 71 | class MyIter(bt2._UserMessageIterator): |
| 72 | def __next__(self): |
| 73 | raise bt2.Stop |
| 74 | |
| 75 | class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): |
| 76 | def __init__(self, params, obj): |
| 77 | self._add_output_port('out') |
| 78 | |
| 79 | class MySink(bt2._UserSinkComponent): |
| 80 | def __init__(self, params, obj): |
| 81 | self._add_input_port('in') |
| 82 | |
| 83 | def _user_consume(self): |
| 84 | raise bt2.Stop |
| 85 | |
| 86 | graph = bt2.Graph() |
| 87 | src = graph.add_component(MySource, 'src') |
| 88 | sink = graph.add_component(MySink, 'sink') |
| 89 | conn = graph.connect_ports(src.output_ports['out'], sink.input_ports['in']) |
| 90 | self.assertEqual(conn.upstream_port.addr, src.output_ports['out'].addr) |