| 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 | from bt2 import port as bt2_port |
| 23 | |
| 24 | |
| 25 | class ConnectionTestCase(unittest.TestCase): |
| 26 | def test_create(self): |
| 27 | class MySource( |
| 28 | bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator |
| 29 | ): |
| 30 | def __init__(self, config, params, obj): |
| 31 | self._add_output_port('out') |
| 32 | |
| 33 | class MySink(bt2._UserSinkComponent): |
| 34 | def __init__(self, config, params, obj): |
| 35 | self._add_input_port('in') |
| 36 | |
| 37 | def _user_consume(self): |
| 38 | raise bt2.Stop |
| 39 | |
| 40 | graph = bt2.Graph() |
| 41 | src = graph.add_component(MySource, 'src') |
| 42 | sink = graph.add_component(MySink, 'sink') |
| 43 | conn = graph.connect_ports(src.output_ports['out'], sink.input_ports['in']) |
| 44 | self.assertIs(type(conn), bt2_connection._ConnectionConst) |
| 45 | |
| 46 | def test_downstream_port(self): |
| 47 | class MySource( |
| 48 | bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator |
| 49 | ): |
| 50 | def __init__(self, config, params, obj): |
| 51 | self._add_output_port('out') |
| 52 | |
| 53 | class MySink(bt2._UserSinkComponent): |
| 54 | def __init__(self, config, params, obj): |
| 55 | self._add_input_port('in') |
| 56 | |
| 57 | def _user_consume(self): |
| 58 | raise bt2.Stop |
| 59 | |
| 60 | graph = bt2.Graph() |
| 61 | src = graph.add_component(MySource, 'src') |
| 62 | sink = graph.add_component(MySink, 'sink') |
| 63 | conn = graph.connect_ports(src.output_ports['out'], sink.input_ports['in']) |
| 64 | self.assertEqual(conn.downstream_port.addr, sink.input_ports['in'].addr) |
| 65 | self.assertIs(type(conn), bt2_connection._ConnectionConst) |
| 66 | self.assertIs(type(conn.downstream_port), bt2_port._InputPortConst) |
| 67 | |
| 68 | def test_upstream_port(self): |
| 69 | class MySource( |
| 70 | bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator |
| 71 | ): |
| 72 | def __init__(self, config, params, obj): |
| 73 | self._add_output_port('out') |
| 74 | |
| 75 | class MySink(bt2._UserSinkComponent): |
| 76 | def __init__(self, config, params, obj): |
| 77 | self._add_input_port('in') |
| 78 | |
| 79 | def _user_consume(self): |
| 80 | raise bt2.Stop |
| 81 | |
| 82 | graph = bt2.Graph() |
| 83 | src = graph.add_component(MySource, 'src') |
| 84 | sink = graph.add_component(MySink, 'sink') |
| 85 | conn = graph.connect_ports(src.output_ports['out'], sink.input_ports['in']) |
| 86 | self.assertEqual(conn.upstream_port.addr, src.output_ports['out'].addr) |
| 87 | self.assertIs(type(conn.upstream_port), bt2_port._OutputPortConst) |
| 88 | |
| 89 | |
| 90 | if __name__ == '__main__': |
| 91 | unittest.main() |