From: Simon Marchi Date: Fri, 31 May 2019 15:24:29 +0000 (-0400) Subject: bt2: Adapt test_connection.py and make it pass X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=2c67587ab59a15b012008ca38cc13fb77f0eb776 bt2: Adapt test_connection.py and make it pass The test_connection test is adapted to account for: - Connections can't be in an ended state anymore. - We don't need to define equality between connections. - Private connections don't exist anymore. The bindings are also simplified by the fact that private connections don't exist anymore, and connections can't end. Otherwise, changes are straightforward to adapt to the existing API. Change-Id: I52cb89512df1e8cb0cd88670c6d63e68287556c3 Signed-off-by: Simon Marchi Signed-off-by: Francis Deslauriers Reviewed-on: https://review.lttng.org/c/babeltrace/+/1280 Reviewed-by: Philippe Proulx Tested-by: jenkins --- diff --git a/bindings/python/bt2/bt2/component.py b/bindings/python/bt2/bt2/component.py index df8eee63..e41e00bd 100644 --- a/bindings/python/bt2/bt2/component.py +++ b/bindings/python/bt2/bt2/component.py @@ -589,7 +589,12 @@ class _UserComponent(metaclass=_UserComponentType): def _accept_port_connection_from_native(self, self_port_ptr, self_port_type, other_port_ptr): port = bt2.port._create_self_from_ptr_and_get_ref( self_port_ptr, self_port_type) - other_port_type = native_bt.PORT_TYPE_INPUT if self_port_type == native_bt.PORT_TYPE_OUTPUT else native_bt.PORT_TYPE_OUTPUT + + if self_port_type == native_bt.PORT_TYPE_OUTPUT: + other_port_type = native_bt.PORT_TYPE_INPUT + else: + other_port_type = native_bt.PORT_TYPE_OUTPUT + other_port = bt2.port._create_from_ptr_and_get_ref( other_port_ptr, other_port_type) res = self._accept_port_connection(port, other_port_ptr) @@ -605,7 +610,12 @@ class _UserComponent(metaclass=_UserComponentType): def _port_connected_from_native(self, self_port_ptr, self_port_type, other_port_ptr): port = bt2.port._create_self_from_ptr_and_get_ref( self_port_ptr, self_port_type) - other_port_type = native_bt.PORT_TYPE_INPUT if self_port_type == native_bt.PORT_TYPE_OUTPUT else native_bt.PORT_TYPE_OUTPUT + + if self_port_type == native_bt.PORT_TYPE_OUTPUT: + other_port_type = native_bt.PORT_TYPE_INPUT + else: + other_port_type = native_bt.PORT_TYPE_OUTPUT + other_port = bt2.port._create_from_ptr_and_get_ref( other_port_ptr, other_port_type) self._port_connected(port, other_port) diff --git a/bindings/python/bt2/bt2/connection.py b/bindings/python/bt2/bt2/connection.py index a15c6792..4c52a776 100644 --- a/bindings/python/bt2/bt2/connection.py +++ b/bindings/python/bt2/bt2/connection.py @@ -20,61 +20,24 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. -from bt2 import native_bt, object, utils +from bt2 import native_bt, utils import bt2.message_iterator -import collections.abc import bt2.port -import copy import bt2 -def _handle_status(status, gen_error_msg): - if status == native_bt.CONNECTION_STATUS_GRAPH_IS_CANCELED: - raise bt2.GraphCanceled - elif status == native_bt.CONNECTION_STATUS_IS_ENDED: - raise bt2.ConnectionEnded - elif status < 0: - raise bt2.Error(gen_error_msg) - - -def _create_private_from_ptr(ptr): - obj = _PrivateConnection._create_from_ptr(ptr) - obj._pub_ptr = native_bt.connection_from_private(ptr) - assert(obj._pub_ptr) - return obj - - -class _Connection(object._SharedObject): - @staticmethod - def _downstream_port(ptr): - port_ptr = native_bt.connection_get_downstream_port(ptr) - utils._handle_ptr(port_ptr, "cannot get connection object's downstream port object") - return bt2.port._create_from_ptr(port_ptr) - - @staticmethod - def _upstream_port(ptr): - port_ptr = native_bt.connection_get_upstream_port(ptr) - utils._handle_ptr(port_ptr, "cannot get connection object's upstream port object") - return bt2.port._create_from_ptr(port_ptr) +class _Connection(bt2.object._SharedObject): + _get_ref = staticmethod(native_bt.connection_get_ref) + _put_ref = staticmethod(native_bt.connection_put_ref) @property def downstream_port(self): - return self._downstream_port(self._ptr) + port_ptr = native_bt.connection_borrow_downstream_port_const(self._ptr) + utils._handle_ptr(port_ptr, "cannot get connection object's downstream port object") + return bt2.port._create_from_ptr_and_get_ref(port_ptr, native_bt.PORT_TYPE_INPUT) @property def upstream_port(self): - return self._upstream_port(self._ptr) - - @staticmethod - def _is_ended(ptr): - return native_bt.connection_is_ended(ptr) == 1 - - @property - def is_ended(self): - return self._is_ended(self._ptr) - - def __eq__(self, other): - if type(other) not in (_Connection, _PrivateConnection): - return False - - return self.addr == other.addr + port_ptr = native_bt.connection_borrow_upstream_port_const(self._ptr) + utils._handle_ptr(port_ptr, "cannot get connection object's upstream port object") + return bt2.port._create_from_ptr_and_get_ref(port_ptr, native_bt.PORT_TYPE_OUTPUT) diff --git a/bindings/python/bt2/bt2/port.py b/bindings/python/bt2/bt2/port.py index c91d9889..8ec04657 100644 --- a/bindings/python/bt2/bt2/port.py +++ b/bindings/python/bt2/bt2/port.py @@ -28,6 +28,24 @@ import bt2.message import bt2 +def _create_from_ptr_and_get_ref(ptr, port_type): + cls = _PORT_TYPE_TO_PYCLS.get(port_type, None) + + if cls is None: + raise bt2.Error('unknown port type: {}'.format(port_type)) + + return cls._create_from_ptr_and_get_ref(ptr) + + +def _create_self_from_ptr_and_get_ref(ptr, port_type): + cls = _PORT_TYPE_TO_USER_PYCLS.get(port_type, None) + + if cls is None: + raise bt2.Error('unknown port type: {}'.format(port_type)) + + return cls._create_from_ptr_and_get_ref(ptr) + + class _Port(object._SharedObject): @classmethod def _get_ref(cls, ptr): @@ -92,3 +110,15 @@ class _UserComponentInputPort(_UserComponentPort, _InputPort): class _UserComponentOutputPort(_UserComponentPort, _OutputPort): _as_self_port_ptr = staticmethod(native_bt.self_component_port_output_as_self_component_port) + + +_PORT_TYPE_TO_PYCLS = { + native_bt.PORT_TYPE_INPUT: _InputPort, + native_bt.PORT_TYPE_OUTPUT: _OutputPort, +} + + +_PORT_TYPE_TO_USER_PYCLS = { + native_bt.PORT_TYPE_INPUT: _UserComponentInputPort, + native_bt.PORT_TYPE_OUTPUT: _UserComponentOutputPort, +} diff --git a/tests/bindings/python/bt2/test_connection.py b/tests/bindings/python/bt2/test_connection.py index 1ff93283..490c984b 100644 --- a/tests/bindings/python/bt2/test_connection.py +++ b/tests/bindings/python/bt2/test_connection.py @@ -1,10 +1,7 @@ -from bt2 import value import unittest -import copy import bt2 -@unittest.skip("this is broken") class ConnectionTestCase(unittest.TestCase): def test_create(self): class MyIter(bt2._UserMessageIterator): @@ -29,56 +26,6 @@ class ConnectionTestCase(unittest.TestCase): conn = graph.connect_ports(src.output_ports['out'], sink.input_ports['in']) self.assertIsInstance(conn, bt2._Connection) - self.assertNotIsInstance(conn, bt2._PrivateConnection) - - def test_is_ended_false(self): - class MyIter(bt2._UserMessageIterator): - def __next__(self): - raise bt2.Stop - - class MySource(bt2._UserSourceComponent, - message_iterator_class=MyIter): - def __init__(self, params): - self._add_output_port('out') - - class MySink(bt2._UserSinkComponent): - def __init__(self, params): - self._add_input_port('in') - - def _consume(self): - raise bt2.Stop - - graph = bt2.Graph() - src = graph.add_component(MySource, 'src') - sink = graph.add_component(MySink, 'sink') - conn = graph.connect_ports(src.output_ports['out'], - sink.input_ports['in']) - self.assertFalse(conn.is_ended) - - def test_is_ended_true(self): - class MyIter(bt2._UserMessageIterator): - def __next__(self): - raise bt2.Stop - - class MySource(bt2._UserSourceComponent, - message_iterator_class=MyIter): - def __init__(self, params): - self._add_output_port('out') - - class MySink(bt2._UserSinkComponent): - def __init__(self, params): - self._add_input_port('in') - - def _consume(self): - raise bt2.Stop - - graph = bt2.Graph() - src = graph.add_component(MySource, 'src') - sink = graph.add_component(MySink, 'sink') - conn = graph.connect_ports(src.output_ports['out'], - sink.input_ports['in']) - src.output_ports['out'].disconnect() - self.assertTrue(conn.is_ended) def test_downstream_port(self): class MyIter(bt2._UserMessageIterator): @@ -127,268 +74,3 @@ class ConnectionTestCase(unittest.TestCase): conn = graph.connect_ports(src.output_ports['out'], sink.input_ports['in']) self.assertEqual(conn.upstream_port.addr, src.output_ports['out'].addr) - - def test_eq(self): - class MyIter(bt2._UserMessageIterator): - def __next__(self): - raise bt2.Stop - - class MySource(bt2._UserSourceComponent, - message_iterator_class=MyIter): - def __init__(self, params): - self._add_output_port('out') - - class MySink(bt2._UserSinkComponent): - def __init__(self, params): - self._add_input_port('in') - - def _consume(self): - raise bt2.Stop - - graph = bt2.Graph() - src = graph.add_component(MySource, 'src') - sink = graph.add_component(MySink, 'sink') - conn = graph.connect_ports(src.output_ports['out'], - sink.input_ports['in']) - self.assertEqual(conn, conn) - - def test_eq_invalid(self): - class MyIter(bt2._UserMessageIterator): - def __next__(self): - raise bt2.Stop - - class MySource(bt2._UserSourceComponent, - message_iterator_class=MyIter): - def __init__(self, params): - self._add_output_port('out') - - class MySink(bt2._UserSinkComponent): - def __init__(self, params): - self._add_input_port('in') - - def _consume(self): - raise bt2.Stop - - graph = bt2.Graph() - src = graph.add_component(MySource, 'src') - sink = graph.add_component(MySink, 'sink') - conn = graph.connect_ports(src.output_ports['out'], - sink.input_ports['in']) - self.assertNotEqual(conn, 23) - - -@unittest.skip("this is broken") -class PrivateConnectionTestCase(unittest.TestCase): - def test_create(self): - class MyIter(bt2._UserMessageIterator): - def __next__(self): - raise bt2.Stop - - class MySource(bt2._UserSourceComponent, - message_iterator_class=MyIter): - def __init__(self, params): - self._add_output_port('out') - - class MySink(bt2._UserSinkComponent): - def __init__(self, params): - self._add_input_port('in') - - def _consume(self): - raise bt2.Stop - - def _port_connected(self, port, other_port): - nonlocal priv_conn - priv_conn = port.connection - - priv_conn = None - graph = bt2.Graph() - src = graph.add_component(MySource, 'src') - sink = graph.add_component(MySink, 'sink') - conn = graph.connect_ports(src.output_ports['out'], - sink.input_ports['in']) - self.assertIsInstance(priv_conn, bt2._PrivateConnection) - self.assertEqual(conn, priv_conn) - del priv_conn - - def test_is_ended_false(self): - class MyIter(bt2._UserMessageIterator): - def __next__(self): - raise bt2.Stop - - class MySource(bt2._UserSourceComponent, - message_iterator_class=MyIter): - def __init__(self, params): - self._add_output_port('out') - - class MySink(bt2._UserSinkComponent): - def __init__(self, params): - self._add_input_port('in') - - def _consume(self): - raise bt2.Stop - - def _port_connected(self, port, other_port): - nonlocal priv_conn - priv_conn = port.connection - - priv_conn = None - graph = bt2.Graph() - src = graph.add_component(MySource, 'src') - sink = graph.add_component(MySink, 'sink') - conn = graph.connect_ports(src.output_ports['out'], - sink.input_ports['in']) - self.assertFalse(priv_conn.is_ended) - del priv_conn - - def test_is_ended_true(self): - class MyIter(bt2._UserMessageIterator): - def __next__(self): - raise bt2.Stop - - class MySource(bt2._UserSourceComponent, - message_iterator_class=MyIter): - def __init__(self, params): - self._add_output_port('out') - - class MySink(bt2._UserSinkComponent): - def __init__(self, params): - self._add_input_port('in') - - def _consume(self): - raise bt2.Stop - - def _port_connected(self, port, other_port): - nonlocal priv_conn - priv_conn = port.connection - - priv_conn = None - graph = bt2.Graph() - src = graph.add_component(MySource, 'src') - sink = graph.add_component(MySink, 'sink') - conn = graph.connect_ports(src.output_ports['out'], - sink.input_ports['in']) - sink.input_ports['in'].disconnect() - self.assertTrue(priv_conn.is_ended) - del priv_conn - - def test_downstream_port(self): - class MyIter(bt2._UserMessageIterator): - def __next__(self): - raise bt2.Stop - - class MySource(bt2._UserSourceComponent, - message_iterator_class=MyIter): - def __init__(self, params): - self._add_output_port('out') - - class MySink(bt2._UserSinkComponent): - def __init__(self, params): - self._add_input_port('in') - - def _consume(self): - raise bt2.Stop - - def _port_connected(self, port, other_port): - nonlocal priv_port - priv_conn = port.connection - priv_port = priv_conn.downstream_port - - priv_port = None - graph = bt2.Graph() - src = graph.add_component(MySource, 'src') - sink = graph.add_component(MySink, 'sink') - conn = graph.connect_ports(src.output_ports['out'], - sink.input_ports['in']) - self.assertEqual(priv_port.addr, sink.input_ports['in'].addr) - del priv_port - - def test_upstream_port(self): - class MyIter(bt2._UserMessageIterator): - def __next__(self): - raise bt2.Stop - - class MySource(bt2._UserSourceComponent, - message_iterator_class=MyIter): - def __init__(self, params): - self._add_output_port('out') - - class MySink(bt2._UserSinkComponent): - def __init__(self, params): - self._add_input_port('in') - - def _consume(self): - raise bt2.Stop - - def _port_connected(self, port, other_port): - nonlocal priv_port - priv_conn = port.connection - priv_port = priv_conn.upstream_port - - priv_port = None - graph = bt2.Graph() - src = graph.add_component(MySource, 'src') - sink = graph.add_component(MySink, 'sink') - conn = graph.connect_ports(src.output_ports['out'], - sink.input_ports['in']) - self.assertEqual(priv_port.addr, src.output_ports['out'].addr) - del priv_port - - def test_eq(self): - class MyIter(bt2._UserMessageIterator): - def __next__(self): - raise bt2.Stop - - class MySource(bt2._UserSourceComponent, - message_iterator_class=MyIter): - def __init__(self, params): - self._add_output_port('out') - - class MySink(bt2._UserSinkComponent): - def __init__(self, params): - self._add_input_port('in') - - def _consume(self): - raise bt2.Stop - - def _port_connected(self, port, other_port): - nonlocal priv_conn - priv_conn = port.connection - - priv_conn = None - graph = bt2.Graph() - src = graph.add_component(MySource, 'src') - sink = graph.add_component(MySink, 'sink') - conn = graph.connect_ports(src.output_ports['out'], - sink.input_ports['in']) - self.assertEqual(priv_conn, conn) - del priv_conn - - def test_eq_invalid(self): - class MyIter(bt2._UserMessageIterator): - def __next__(self): - raise bt2.Stop - - class MySource(bt2._UserSourceComponent, - message_iterator_class=MyIter): - def __init__(self, params): - self._add_output_port('out') - - class MySink(bt2._UserSinkComponent): - def __init__(self, params): - self._add_input_port('in') - - def _consume(self): - raise bt2.Stop - - def _port_connected(self, port, other_port): - nonlocal priv_conn - priv_conn = port.connection - - priv_conn = None - graph = bt2.Graph() - src = graph.add_component(MySource, 'src') - sink = graph.add_component(MySink, 'sink') - conn = graph.connect_ports(src.output_ports['out'], - sink.input_ports['in']) - self.assertNotEqual(priv_conn, 23) - del priv_conn