X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=tests%2Fbindings%2Fpython%2Fbt2%2Ftest_graph.py;h=f6f5dcaae9eab4a5bc70d06e8f1fcb873d19a1f2;hb=694c792bc8f078c02acde68a3390acafbb36b2f4;hp=8ed585a626aa0ff4d0753949d6ca99c03a78cc61;hpb=5f25509b06eb8dd0a76d3068894d9f8fefd63178;p=babeltrace.git diff --git a/tests/bindings/python/bt2/test_graph.py b/tests/bindings/python/bt2/test_graph.py index 8ed585a6..f6f5dcaa 100644 --- a/tests/bindings/python/bt2/test_graph.py +++ b/tests/bindings/python/bt2/test_graph.py @@ -1,3 +1,21 @@ +# +# Copyright (C) 2019 EfficiOS Inc. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; only version 2 +# of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# + from bt2 import value import collections import unittest @@ -6,20 +24,18 @@ import bt2 class _MyIter(bt2._UserMessageIterator): - def __init__(self): + def __init__(self, self_output_port): self._build_meta() self._at = 0 def _build_meta(self): self._tc = self._component._create_trace_class() self._t = self._tc() - self._sc = self._tc.create_stream_class() + self._sc = self._tc.create_stream_class(supports_packets=True) self._ec = self._sc.create_event_class(name='salut') self._my_int_ft = self._tc.create_signed_integer_field_class(32) payload_ft = self._tc.create_structure_field_class() - payload_ft += collections.OrderedDict([ - ('my_int', self._my_int_ft), - ]) + payload_ft += [('my_int', self._my_int_ft)] self._ec.payload_field_type = payload_ft self._stream = self._t.create_stream(self._sc) self._packet = self._stream.create_packet() @@ -46,6 +62,9 @@ class GraphTestCase(unittest.TestCase): def _consume(self): pass + def _graph_is_configured(self): + pass + comp = self._graph.add_component(MySink, 'salut') self.assertEqual(comp.name, 'salut') @@ -54,9 +73,12 @@ class GraphTestCase(unittest.TestCase): def _consume(self): pass + def _graph_is_configured(self): + pass + comp = self._graph.add_component(MySink, 'salut') assert comp - comp2 = self._graph.add_component(comp.component_class, 'salut2') + comp2 = self._graph.add_component(comp.cls, 'salut2') self.assertEqual(comp2.name, 'salut2') def test_add_component_params(self): @@ -70,6 +92,9 @@ class GraphTestCase(unittest.TestCase): def _consume(self): pass + def _graph_is_configured(self): + pass + params = {'hello': 23, 'path': '/path/to/stuff'} comp = self._graph.add_component(MySink, 'salut', params) self.assertEqual(params, comp_params) @@ -79,40 +104,47 @@ class GraphTestCase(unittest.TestCase): with self.assertRaises(TypeError): self._graph.add_component(int, 'salut') - def test_connect_ports(self): - class MyIter(bt2._UserMessageIterator): - def __next__(self): - raise bt2.Stop + def test_add_component_invalid_logging_level_type(self): + class MySink(bt2._UserSinkComponent): + def _consume(self): + pass - class MySource(bt2._UserSourceComponent, - message_iterator_class=MyIter): - def __init__(self, params): - self._add_output_port('out') + def _graph_is_configured(self): + pass + with self.assertRaises(TypeError): + self._graph.add_component(MySink, 'salut', logging_level='yo') + + def test_add_component_invalid_logging_level_value(self): class MySink(bt2._UserSinkComponent): - def __init__(self, params): - self._add_input_port('in') + def _consume(self): + pass + + def _graph_is_configured(self): + pass + with self.assertRaises(ValueError): + self._graph.add_component(MySink, 'salut', logging_level=12345) + + def test_add_component_logging_level(self): + class MySink(bt2._UserSinkComponent): def _consume(self): - raise bt2.Stop + pass - src = self._graph.add_component(MySource, 'src') - sink = self._graph.add_component(MySink, 'sink') + def _graph_is_configured(self): + pass - conn = self._graph.connect_ports(src.output_ports['out'], - sink.input_ports['in']) - self.assertTrue(src.output_ports['out'].is_connected) - self.assertTrue(sink.input_ports['in'].is_connected) - self.assertEqual(src.output_ports['out'].connection._ptr, conn._ptr) - self.assertEqual(sink.input_ports['in'].connection._ptr, conn._ptr) + comp = self._graph.add_component( + MySink, 'salut', logging_level=bt2.LoggingLevel.DEBUG + ) + self.assertEqual(comp.logging_level, bt2.LoggingLevel.DEBUG) - def test_connect_ports_invalid_direction(self): + def test_connect_ports(self): class MyIter(bt2._UserMessageIterator): def __next__(self): raise bt2.Stop - class MySource(bt2._UserSourceComponent, - message_iterator_class=MyIter): + class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): def __init__(self, params): self._add_output_port('out') @@ -123,20 +155,26 @@ class GraphTestCase(unittest.TestCase): def _consume(self): raise bt2.Stop + def _graph_is_configured(self): + pass + src = self._graph.add_component(MySource, 'src') sink = self._graph.add_component(MySink, 'sink') - with self.assertRaises(TypeError): - conn = self._graph.connect_ports(sink.input_ports['in'], - src.output_ports['out']) + conn = self._graph.connect_ports( + src.output_ports['out'], sink.input_ports['in'] + ) + self.assertTrue(src.output_ports['out'].is_connected) + self.assertTrue(sink.input_ports['in'].is_connected) + self.assertEqual(src.output_ports['out'].connection.addr, conn.addr) + self.assertEqual(sink.input_ports['in'].connection.addr, conn.addr) - def test_connect_ports_refused(self): + def test_connect_ports_invalid_direction(self): class MyIter(bt2._UserMessageIterator): def __next__(self): raise bt2.Stop - class MySource(bt2._UserSourceComponent, - message_iterator_class=MyIter): + class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): def __init__(self, params): self._add_output_port('out') @@ -147,30 +185,30 @@ class GraphTestCase(unittest.TestCase): def _consume(self): raise bt2.Stop - def _accept_port_connection(self, port, other_port): - return False + def _graph_is_configured(self): + pass src = self._graph.add_component(MySource, 'src') sink = self._graph.add_component(MySink, 'sink') - with self.assertRaises(bt2.PortConnectionRefused): - conn = self._graph.connect_ports(src.output_ports['out'], - sink.input_ports['in']) + with self.assertRaises(TypeError): + conn = self._graph.connect_ports( + sink.input_ports['in'], src.output_ports['out'] + ) def test_cancel(self): self.assertFalse(self._graph.is_canceled) self._graph.cancel() self.assertTrue(self._graph.is_canceled) - # Test that Graph.run() raises bt2.GraphCanceled if the graph gets canceled + # Test that Graph.run() raises bt2.Canceled if the graph gets canceled # during execution. def test_cancel_while_running(self): class MyIter(_MyIter): def __next__(self): return self._create_stream_beginning_message(self._stream) - class MySource(bt2._UserSourceComponent, - message_iterator_class=MyIter): + class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): def __init__(self, params): self._add_output_port('out') @@ -192,7 +230,7 @@ class GraphTestCase(unittest.TestCase): up = graph.add_component(MySource, 'down') down = graph.add_component(MySink, 'up') graph.connect_ports(up.output_ports['out'], down.input_ports['in']) - with self.assertRaises(bt2.GraphCanceled): + with self.assertRaises(bt2.Canceled): graph.run() def test_run(self): @@ -215,8 +253,7 @@ class GraphTestCase(unittest.TestCase): self._at += 1 return msg - class MySource(bt2._UserSourceComponent, - message_iterator_class=MyIter): + class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): def __init__(self, params): self._add_output_port('out') @@ -234,7 +271,7 @@ class GraphTestCase(unittest.TestCase): self.assertIsInstance(msg, bt2.message._PacketBeginningMessage) elif comp_self._at >= 2 and comp_self._at <= 6: self.assertIsInstance(msg, bt2.message._EventMessage) - self.assertEqual(msg.event.event_class.name, 'salut') + self.assertEqual(msg.event.cls.name, 'salut') elif comp_self._at == 7: self.assertIsInstance(msg, bt2.message._PacketEndMessage) elif comp_self._at == 8: @@ -247,8 +284,9 @@ class GraphTestCase(unittest.TestCase): src = self._graph.add_component(MySource, 'src') sink = self._graph.add_component(MySink, 'sink') - conn = self._graph.connect_ports(src.output_ports['out'], - sink.input_ports['in']) + conn = self._graph.connect_ports( + src.output_ports['out'], sink.input_ports['in'] + ) self._graph.run() def test_run_again(self): @@ -267,8 +305,7 @@ class GraphTestCase(unittest.TestCase): self._at += 1 return msg - class MySource(bt2._UserSourceComponent, - message_iterator_class=MyIter): + class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): def __init__(self, params): self._add_output_port('out') @@ -296,8 +333,9 @@ class GraphTestCase(unittest.TestCase): src = self._graph.add_component(MySource, 'src') sink = self._graph.add_component(MySink, 'sink') - conn = self._graph.connect_ports(src.output_ports['out'], - sink.input_ports['in']) + conn = self._graph.connect_ports( + src.output_ports['out'], sink.input_ports['in'] + ) with self.assertRaises(bt2.TryAgain): self._graph.run() @@ -323,8 +361,7 @@ class GraphTestCase(unittest.TestCase): self._at += 1 return msg - class MySource(bt2._UserSourceComponent, - message_iterator_class=MyIter): + class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): def __init__(self, params): self._add_output_port('out') @@ -353,10 +390,11 @@ class GraphTestCase(unittest.TestCase): src = self._graph.add_component(MySource, 'src') sink = self._graph.add_component(MySink, 'sink') - conn = self._graph.connect_ports(src.output_ports['out'], - sink.input_ports['in']) + conn = self._graph.connect_ports( + src.output_ports['out'], sink.input_ports['in'] + ) - with self.assertRaises(bt2.Error): + with self.assertRaises(bt2._Error): self._graph.run() def test_listeners(self): @@ -364,8 +402,7 @@ class GraphTestCase(unittest.TestCase): def __next__(self): raise bt2.Stop - class MySource(bt2._UserSourceComponent, - message_iterator_class=MyIter): + class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): def __init__(self, params): self._add_output_port('out') self._add_output_port('zero') @@ -377,6 +414,9 @@ class GraphTestCase(unittest.TestCase): def _consume(self): raise bt2.Stop + def _graph_is_configured(self): + pass + def _port_connected(self, port, other_port): self._add_input_port('taste') @@ -384,20 +424,26 @@ class GraphTestCase(unittest.TestCase): nonlocal calls calls.append((port_added_listener, component, port)) - def ports_connected_listener(upstream_component, upstream_port, - downstream_component, downstream_port): + def ports_connected_listener( + upstream_component, upstream_port, downstream_component, downstream_port + ): nonlocal calls - calls.append((ports_connected_listener, - upstream_component, upstream_port, - downstream_component, downstream_port)) + calls.append( + ( + ports_connected_listener, + upstream_component, + upstream_port, + downstream_component, + downstream_port, + ) + ) calls = [] self._graph.add_port_added_listener(port_added_listener) self._graph.add_ports_connected_listener(ports_connected_listener) src = self._graph.add_component(MySource, 'src') sink = self._graph.add_component(MySink, 'sink') - self._graph.connect_ports(src.output_ports['out'], - sink.input_ports['in']) + self._graph.connect_ports(src.output_ports['out'], sink.input_ports['in']) self.assertEqual(len(calls), 5) @@ -428,8 +474,7 @@ class GraphTestCase(unittest.TestCase): def __next__(self): raise bt2.Stop - class MySource(bt2._UserSourceComponent, - message_iterator_class=MyIter): + class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): def __init__(self, params): self._add_output_port('out') self._add_output_port('zero') @@ -441,6 +486,9 @@ class GraphTestCase(unittest.TestCase): def _consume(self): raise bt2.Stop + def _graph_is_configured(self): + pass + def _port_connected(self, port, other_port): self._add_input_port('taste') @@ -457,9 +505,12 @@ class GraphTestCase(unittest.TestCase): def _consume(self): raise bt2.Stop + def _graph_is_configured(self): + pass + graph = bt2.Graph() - with self.assertRaises(bt2.Error): + with self.assertRaises(bt2._Error): graph.add_component(MySink, 'comp') def test_raise_in_port_added_listener(self): @@ -470,13 +521,16 @@ class GraphTestCase(unittest.TestCase): def _consume(self): raise bt2.Stop + def _graph_is_configured(self): + pass + def port_added_listener(component, port): raise ValueError('oh noes!') graph = bt2.Graph() graph.add_port_added_listener(port_added_listener) - with self.assertRaises(bt2.Error): + with self.assertRaises(bt2._Error): graph.add_component(MySink, 'comp') def test_raise_in_ports_connected_listener(self): @@ -484,8 +538,7 @@ class GraphTestCase(unittest.TestCase): def __next__(self): raise bt2.Stop - class MySource(bt2._UserSourceComponent, - message_iterator_class=MyIter): + class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): def __init__(self, params): self._add_output_port('out') @@ -496,7 +549,12 @@ class GraphTestCase(unittest.TestCase): def _consume(self): raise bt2.Stop - def ports_connected_listener(upstream_port, downstream_port): + def _graph_is_configured(self): + pass + + def ports_connected_listener( + upstream_component, upstream_port, downstream_component, downstream_port + ): raise ValueError('oh noes!') graph = bt2.Graph() @@ -504,5 +562,5 @@ class GraphTestCase(unittest.TestCase): up = graph.add_component(MySource, 'down') down = graph.add_component(MySink, 'up') - with self.assertRaises(bt2.Error): + with self.assertRaises(bt2._Error): graph.connect_ports(up.output_ports['out'], down.input_ports['in'])