bt2: Adapt test_connection.py and make it pass
authorSimon Marchi <simon.marchi@efficios.com>
Fri, 31 May 2019 15:24:29 +0000 (11:24 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Wed, 5 Jun 2019 17:47:34 +0000 (13:47 -0400)
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 <simon.marchi@efficios.com>
Signed-off-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1280
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Tested-by: jenkins
bindings/python/bt2/bt2/component.py
bindings/python/bt2/bt2/connection.py
bindings/python/bt2/bt2/port.py
tests/bindings/python/bt2/test_connection.py

index df8eee63bc27dece4edfbc7a31ce3dd2550c7fc5..e41e00bd129d1616e6f587a403fd8d50cc695ec9 100644 (file)
@@ -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)
index a15c67927bc75e136ba76e1d64071b6577a5a719..4c52a776b32d38c81f5429fd1198f09e9ba487a4 100644 (file)
 # 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)
index c91d9889b4ced413856d225b53058f68ca958027..8ec04657f9dfdd09637278adcda05c00a820e8f6 100644 (file)
@@ -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,
+}
index 1ff932835bf332011e5939fce7a3154e42b06171..490c984b20b70eba45e224a4c86df388e0cbdab8 100644 (file)
@@ -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
This page took 0.03025 seconds and 4 git commands to generate.