| 1 | # SPDX-License-Identifier: GPL-2.0-only |
| 2 | # |
| 3 | # Copyright (C) 2019 EfficiOS Inc. |
| 4 | # |
| 5 | |
| 6 | import unittest |
| 7 | |
| 8 | import bt2 |
| 9 | from bt2 import port as bt2_port |
| 10 | |
| 11 | |
| 12 | class PortTestCase(unittest.TestCase): |
| 13 | @staticmethod |
| 14 | def _create_comp(comp_cls, name=None): |
| 15 | graph = bt2.Graph() |
| 16 | |
| 17 | if name is None: |
| 18 | name = "comp" |
| 19 | |
| 20 | return graph.add_component(comp_cls, name) |
| 21 | |
| 22 | def test_src_add_output_port(self): |
| 23 | class MySource( |
| 24 | bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator |
| 25 | ): |
| 26 | def __init__(comp_self, config, params, obj): |
| 27 | port = comp_self._add_output_port("out") |
| 28 | self.assertEqual(port.name, "out") |
| 29 | |
| 30 | comp = self._create_comp(MySource) |
| 31 | self.assertEqual(len(comp.output_ports), 1) |
| 32 | self.assertIs(type(comp.output_ports["out"]), bt2_port._OutputPortConst) |
| 33 | |
| 34 | # Test adding output port with duplicate name to source. |
| 35 | def test_src_add_output_port_dup_name_raises(self): |
| 36 | class MySource( |
| 37 | bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator |
| 38 | ): |
| 39 | def __init__(comp_self, config, params, obj): |
| 40 | comp_self._add_output_port("out") |
| 41 | |
| 42 | with self.assertRaisesRegex( |
| 43 | ValueError, |
| 44 | "source component `comp` already contains an output port named `out`", |
| 45 | ): |
| 46 | comp_self._add_output_port("out") |
| 47 | |
| 48 | nonlocal seen |
| 49 | seen = True |
| 50 | |
| 51 | seen = False |
| 52 | self._create_comp(MySource) |
| 53 | self.assertTrue(seen) |
| 54 | |
| 55 | def test_flt_add_output_port(self): |
| 56 | class MyFilter( |
| 57 | bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator |
| 58 | ): |
| 59 | def __init__(comp_self, config, params, obj): |
| 60 | port = comp_self._add_output_port("out") |
| 61 | self.assertEqual(port.name, "out") |
| 62 | |
| 63 | comp = self._create_comp(MyFilter) |
| 64 | self.assertEqual(len(comp.output_ports), 1) |
| 65 | |
| 66 | # Test adding output port with duplicate name to filter. |
| 67 | def test_flt_add_output_port_dup_name_raises(self): |
| 68 | class MyFilter( |
| 69 | bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator |
| 70 | ): |
| 71 | def __init__(comp_self, config, params, obj): |
| 72 | comp_self._add_output_port("out") |
| 73 | |
| 74 | with self.assertRaisesRegex( |
| 75 | ValueError, |
| 76 | "filter component `comp` already contains an output port named `out`", |
| 77 | ): |
| 78 | comp_self._add_output_port("out") |
| 79 | |
| 80 | nonlocal seen |
| 81 | seen = True |
| 82 | |
| 83 | seen = False |
| 84 | self._create_comp(MyFilter) |
| 85 | self.assertTrue(seen) |
| 86 | |
| 87 | def test_flt_add_input_port(self): |
| 88 | class MyFilter( |
| 89 | bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator |
| 90 | ): |
| 91 | def __init__(comp_self, config, params, obj): |
| 92 | port = comp_self._add_input_port("in") |
| 93 | self.assertEqual(port.name, "in") |
| 94 | |
| 95 | comp = self._create_comp(MyFilter) |
| 96 | self.assertEqual(len(comp.input_ports), 1) |
| 97 | self.assertIs(type(comp.input_ports["in"]), bt2_port._InputPortConst) |
| 98 | |
| 99 | # Test adding input port with duplicate name to filter. |
| 100 | def test_flt_add_input_port_dup_name_raises(self): |
| 101 | class MyFilter( |
| 102 | bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator |
| 103 | ): |
| 104 | def __init__(comp_self, config, params, obj): |
| 105 | comp_self._add_input_port("in") |
| 106 | |
| 107 | with self.assertRaisesRegex( |
| 108 | ValueError, |
| 109 | "filter component `comp` already contains an input port named `in`", |
| 110 | ): |
| 111 | comp_self._add_input_port("in") |
| 112 | |
| 113 | nonlocal seen |
| 114 | seen = True |
| 115 | |
| 116 | seen = False |
| 117 | self._create_comp(MyFilter) |
| 118 | self.assertTrue(seen) |
| 119 | |
| 120 | def test_sink_add_input_port(self): |
| 121 | class MySink(bt2._UserSinkComponent): |
| 122 | def __init__(comp_self, config, params, obj): |
| 123 | port = comp_self._add_input_port("in") |
| 124 | self.assertEqual(port.name, "in") |
| 125 | |
| 126 | def _user_consume(self): |
| 127 | pass |
| 128 | |
| 129 | comp = self._create_comp(MySink) |
| 130 | self.assertEqual(len(comp.input_ports), 1) |
| 131 | |
| 132 | # Test adding input port with duplicate name to sink. |
| 133 | def test_sink_add_input_port_dup_name_raises(self): |
| 134 | class MySink(bt2._UserSinkComponent): |
| 135 | def __init__(comp_self, config, params, obj): |
| 136 | comp_self._add_input_port("in") |
| 137 | |
| 138 | with self.assertRaisesRegex( |
| 139 | ValueError, |
| 140 | "sink component `comp` already contains an input port named `in`", |
| 141 | ): |
| 142 | comp_self._add_input_port("in") |
| 143 | |
| 144 | nonlocal seen |
| 145 | seen = True |
| 146 | |
| 147 | def _user_consume(self): |
| 148 | pass |
| 149 | |
| 150 | seen = False |
| 151 | self._create_comp(MySink) |
| 152 | self.assertTrue(seen) |
| 153 | |
| 154 | def test_user_src_output_ports_getitem(self): |
| 155 | class MySource( |
| 156 | bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator |
| 157 | ): |
| 158 | def __init__(comp_self, config, params, obj): |
| 159 | port1 = comp_self._add_output_port("clear") |
| 160 | port2 = comp_self._add_output_port("print") |
| 161 | port3 = comp_self._add_output_port("insert") |
| 162 | self.assertEqual(port3.addr, comp_self._output_ports["insert"].addr) |
| 163 | self.assertEqual(port2.addr, comp_self._output_ports["print"].addr) |
| 164 | self.assertEqual(port1.addr, comp_self._output_ports["clear"].addr) |
| 165 | |
| 166 | self._create_comp(MySource) |
| 167 | |
| 168 | def test_user_flt_output_ports_getitem(self): |
| 169 | class MyFilter( |
| 170 | bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator |
| 171 | ): |
| 172 | def __init__(comp_self, config, params, obj): |
| 173 | port1 = comp_self._add_output_port("clear") |
| 174 | port2 = comp_self._add_output_port("print") |
| 175 | port3 = comp_self._add_output_port("insert") |
| 176 | self.assertEqual(port3.addr, comp_self._output_ports["insert"].addr) |
| 177 | self.assertEqual(port2.addr, comp_self._output_ports["print"].addr) |
| 178 | self.assertEqual(port1.addr, comp_self._output_ports["clear"].addr) |
| 179 | |
| 180 | self._create_comp(MyFilter) |
| 181 | |
| 182 | def test_user_flt_input_ports_getitem(self): |
| 183 | class MyFilter( |
| 184 | bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator |
| 185 | ): |
| 186 | def __init__(comp_self, config, params, obj): |
| 187 | port1 = comp_self._add_input_port("clear") |
| 188 | port2 = comp_self._add_input_port("print") |
| 189 | port3 = comp_self._add_input_port("insert") |
| 190 | self.assertEqual(port3.addr, comp_self._input_ports["insert"].addr) |
| 191 | self.assertEqual(port2.addr, comp_self._input_ports["print"].addr) |
| 192 | self.assertEqual(port1.addr, comp_self._input_ports["clear"].addr) |
| 193 | |
| 194 | self._create_comp(MyFilter) |
| 195 | |
| 196 | def test_user_sink_input_ports_getitem(self): |
| 197 | class MySink(bt2._UserSinkComponent): |
| 198 | def __init__(comp_self, config, params, obj): |
| 199 | port1 = comp_self._add_input_port("clear") |
| 200 | port2 = comp_self._add_input_port("print") |
| 201 | port3 = comp_self._add_input_port("insert") |
| 202 | self.assertEqual(port3.addr, comp_self._input_ports["insert"].addr) |
| 203 | self.assertEqual(port2.addr, comp_self._input_ports["print"].addr) |
| 204 | self.assertEqual(port1.addr, comp_self._input_ports["clear"].addr) |
| 205 | |
| 206 | def _user_consume(self): |
| 207 | pass |
| 208 | |
| 209 | self._create_comp(MySink) |
| 210 | |
| 211 | def test_user_src_output_ports_getitem_invalid_key(self): |
| 212 | class MySource( |
| 213 | bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator |
| 214 | ): |
| 215 | def __init__(comp_self, config, params, obj): |
| 216 | comp_self._add_output_port("clear") |
| 217 | comp_self._add_output_port("print") |
| 218 | comp_self._add_output_port("insert") |
| 219 | |
| 220 | with self.assertRaises(KeyError): |
| 221 | comp_self._output_ports["hello"] |
| 222 | |
| 223 | self._create_comp(MySource) |
| 224 | |
| 225 | def test_user_flt_output_ports_getitem_invalid_key(self): |
| 226 | class MyFilter( |
| 227 | bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator |
| 228 | ): |
| 229 | def __init__(comp_self, config, params, obj): |
| 230 | comp_self._add_output_port("clear") |
| 231 | comp_self._add_output_port("print") |
| 232 | comp_self._add_output_port("insert") |
| 233 | |
| 234 | with self.assertRaises(KeyError): |
| 235 | comp_self._output_ports["hello"] |
| 236 | |
| 237 | self._create_comp(MyFilter) |
| 238 | |
| 239 | def test_user_flt_input_ports_getitem_invalid_key(self): |
| 240 | class MyFilter( |
| 241 | bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator |
| 242 | ): |
| 243 | def __init__(comp_self, config, params, obj): |
| 244 | comp_self._add_input_port("clear") |
| 245 | comp_self._add_input_port("print") |
| 246 | comp_self._add_input_port("insert") |
| 247 | |
| 248 | with self.assertRaises(KeyError): |
| 249 | comp_self._input_ports["hello"] |
| 250 | |
| 251 | self._create_comp(MyFilter) |
| 252 | |
| 253 | def test_user_sink_input_ports_getitem_invalid_key(self): |
| 254 | class MySink(bt2._UserSinkComponent): |
| 255 | def __init__(comp_self, config, params, obj): |
| 256 | comp_self._add_input_port("clear") |
| 257 | comp_self._add_input_port("print") |
| 258 | comp_self._add_input_port("insert") |
| 259 | |
| 260 | with self.assertRaises(KeyError): |
| 261 | comp_self._input_ports["hello"] |
| 262 | |
| 263 | def _user_consume(self): |
| 264 | pass |
| 265 | |
| 266 | self._create_comp(MySink) |
| 267 | |
| 268 | def test_user_src_output_ports_len(self): |
| 269 | class MySource( |
| 270 | bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator |
| 271 | ): |
| 272 | def __init__(comp_self, config, params, obj): |
| 273 | comp_self._add_output_port("clear") |
| 274 | comp_self._add_output_port("print") |
| 275 | comp_self._add_output_port("insert") |
| 276 | self.assertEqual(len(comp_self._output_ports), 3) |
| 277 | |
| 278 | self._create_comp(MySource) |
| 279 | |
| 280 | def test_user_flt_output_ports_len(self): |
| 281 | class MyFilter( |
| 282 | bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator |
| 283 | ): |
| 284 | def __init__(comp_self, config, params, obj): |
| 285 | comp_self._add_output_port("clear") |
| 286 | comp_self._add_output_port("print") |
| 287 | comp_self._add_output_port("insert") |
| 288 | self.assertEqual(len(comp_self._output_ports), 3) |
| 289 | |
| 290 | self._create_comp(MyFilter) |
| 291 | |
| 292 | def test_user_flt_input_ports_len(self): |
| 293 | class MyFilter( |
| 294 | bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator |
| 295 | ): |
| 296 | def __init__(comp_self, config, params, obj): |
| 297 | comp_self._add_input_port("clear") |
| 298 | comp_self._add_input_port("print") |
| 299 | comp_self._add_input_port("insert") |
| 300 | self.assertEqual(len(comp_self._input_ports), 3) |
| 301 | |
| 302 | self._create_comp(MyFilter) |
| 303 | |
| 304 | def test_user_sink_input_ports_len(self): |
| 305 | class MySink(bt2._UserSinkComponent): |
| 306 | def __init__(comp_self, config, params, obj): |
| 307 | comp_self._add_input_port("clear") |
| 308 | comp_self._add_input_port("print") |
| 309 | comp_self._add_input_port("insert") |
| 310 | self.assertEqual(len(comp_self._input_ports), 3) |
| 311 | |
| 312 | def _user_consume(self): |
| 313 | pass |
| 314 | |
| 315 | self._create_comp(MySink) |
| 316 | |
| 317 | def test_user_src_output_ports_iter(self): |
| 318 | class MySource( |
| 319 | bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator |
| 320 | ): |
| 321 | def __init__(comp_self, config, params, obj): |
| 322 | port1 = comp_self._add_output_port("clear") |
| 323 | port2 = comp_self._add_output_port("print") |
| 324 | port3 = comp_self._add_output_port("insert") |
| 325 | ports = [] |
| 326 | |
| 327 | for port_name, port in comp_self._output_ports.items(): |
| 328 | ports.append((port_name, port)) |
| 329 | |
| 330 | self.assertEqual(ports[0][0], "clear") |
| 331 | self.assertEqual(ports[0][1].addr, port1.addr) |
| 332 | self.assertEqual(ports[1][0], "print") |
| 333 | self.assertEqual(ports[1][1].addr, port2.addr) |
| 334 | self.assertEqual(ports[2][0], "insert") |
| 335 | self.assertEqual(ports[2][1].addr, port3.addr) |
| 336 | |
| 337 | self._create_comp(MySource) |
| 338 | |
| 339 | def test_user_flt_output_ports_iter(self): |
| 340 | class MyFilter( |
| 341 | bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator |
| 342 | ): |
| 343 | def __init__(comp_self, config, params, obj): |
| 344 | port1 = comp_self._add_output_port("clear") |
| 345 | port2 = comp_self._add_output_port("print") |
| 346 | port3 = comp_self._add_output_port("insert") |
| 347 | ports = [] |
| 348 | |
| 349 | for port_name, port in comp_self._output_ports.items(): |
| 350 | ports.append((port_name, port)) |
| 351 | |
| 352 | self.assertEqual(ports[0][0], "clear") |
| 353 | self.assertEqual(ports[0][1].addr, port1.addr) |
| 354 | self.assertEqual(ports[1][0], "print") |
| 355 | self.assertEqual(ports[1][1].addr, port2.addr) |
| 356 | self.assertEqual(ports[2][0], "insert") |
| 357 | self.assertEqual(ports[2][1].addr, port3.addr) |
| 358 | |
| 359 | self._create_comp(MyFilter) |
| 360 | |
| 361 | def test_user_flt_input_ports_iter(self): |
| 362 | class MyFilter( |
| 363 | bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator |
| 364 | ): |
| 365 | def __init__(comp_self, config, params, obj): |
| 366 | port1 = comp_self._add_input_port("clear") |
| 367 | port2 = comp_self._add_input_port("print") |
| 368 | port3 = comp_self._add_input_port("insert") |
| 369 | ports = [] |
| 370 | |
| 371 | for port_name, port in comp_self._input_ports.items(): |
| 372 | ports.append((port_name, port)) |
| 373 | |
| 374 | self.assertEqual(ports[0][0], "clear") |
| 375 | self.assertEqual(ports[0][1].addr, port1.addr) |
| 376 | self.assertEqual(ports[1][0], "print") |
| 377 | self.assertEqual(ports[1][1].addr, port2.addr) |
| 378 | self.assertEqual(ports[2][0], "insert") |
| 379 | self.assertEqual(ports[2][1].addr, port3.addr) |
| 380 | |
| 381 | self._create_comp(MyFilter) |
| 382 | |
| 383 | def test_user_sink_input_ports_iter(self): |
| 384 | class MySink(bt2._UserSinkComponent): |
| 385 | def __init__(comp_self, config, params, obj): |
| 386 | port1 = comp_self._add_input_port("clear") |
| 387 | port2 = comp_self._add_input_port("print") |
| 388 | port3 = comp_self._add_input_port("insert") |
| 389 | ports = [] |
| 390 | |
| 391 | for port_name, port in comp_self._input_ports.items(): |
| 392 | ports.append((port_name, port)) |
| 393 | |
| 394 | self.assertEqual(ports[0][0], "clear") |
| 395 | self.assertEqual(ports[0][1].addr, port1.addr) |
| 396 | self.assertEqual(ports[1][0], "print") |
| 397 | self.assertEqual(ports[1][1].addr, port2.addr) |
| 398 | self.assertEqual(ports[2][0], "insert") |
| 399 | self.assertEqual(ports[2][1].addr, port3.addr) |
| 400 | |
| 401 | def _user_consume(self): |
| 402 | pass |
| 403 | |
| 404 | self._create_comp(MySink) |
| 405 | |
| 406 | def test_gen_src_output_ports_getitem(self): |
| 407 | port1 = None |
| 408 | port2 = None |
| 409 | port3 = None |
| 410 | |
| 411 | class MySource( |
| 412 | bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator |
| 413 | ): |
| 414 | def __init__(comp_self, config, params, obj): |
| 415 | nonlocal port1, port2, port3 |
| 416 | port1 = comp_self._add_output_port("clear") |
| 417 | port2 = comp_self._add_output_port("print") |
| 418 | port3 = comp_self._add_output_port("insert") |
| 419 | |
| 420 | comp = self._create_comp(MySource) |
| 421 | self.assertEqual(port3.addr, comp.output_ports["insert"].addr) |
| 422 | self.assertEqual(port2.addr, comp.output_ports["print"].addr) |
| 423 | self.assertEqual(port1.addr, comp.output_ports["clear"].addr) |
| 424 | del port1 |
| 425 | del port2 |
| 426 | del port3 |
| 427 | |
| 428 | def test_gen_flt_output_ports_getitem(self): |
| 429 | port1 = None |
| 430 | port2 = None |
| 431 | port3 = None |
| 432 | |
| 433 | class MyFilter( |
| 434 | bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator |
| 435 | ): |
| 436 | def __init__(comp_self, config, params, obj): |
| 437 | nonlocal port1, port2, port3 |
| 438 | port1 = comp_self._add_output_port("clear") |
| 439 | port2 = comp_self._add_output_port("print") |
| 440 | port3 = comp_self._add_output_port("insert") |
| 441 | |
| 442 | comp = self._create_comp(MyFilter) |
| 443 | self.assertEqual(port3.addr, comp.output_ports["insert"].addr) |
| 444 | self.assertEqual(port2.addr, comp.output_ports["print"].addr) |
| 445 | self.assertEqual(port1.addr, comp.output_ports["clear"].addr) |
| 446 | del port1 |
| 447 | del port2 |
| 448 | del port3 |
| 449 | |
| 450 | def test_gen_flt_input_ports_getitem(self): |
| 451 | port1 = None |
| 452 | port2 = None |
| 453 | port3 = None |
| 454 | |
| 455 | class MyFilter( |
| 456 | bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator |
| 457 | ): |
| 458 | def __init__(comp_self, config, params, obj): |
| 459 | nonlocal port1, port2, port3 |
| 460 | port1 = comp_self._add_input_port("clear") |
| 461 | port2 = comp_self._add_input_port("print") |
| 462 | port3 = comp_self._add_input_port("insert") |
| 463 | |
| 464 | comp = self._create_comp(MyFilter) |
| 465 | self.assertEqual(port3.addr, comp.input_ports["insert"].addr) |
| 466 | self.assertEqual(port2.addr, comp.input_ports["print"].addr) |
| 467 | self.assertEqual(port1.addr, comp.input_ports["clear"].addr) |
| 468 | del port1 |
| 469 | del port2 |
| 470 | del port3 |
| 471 | |
| 472 | def test_gen_sink_input_ports_getitem(self): |
| 473 | port1 = None |
| 474 | port2 = None |
| 475 | port3 = None |
| 476 | |
| 477 | class MySink(bt2._UserSinkComponent): |
| 478 | def __init__(comp_self, config, params, obj): |
| 479 | nonlocal port1, port2, port3 |
| 480 | port1 = comp_self._add_input_port("clear") |
| 481 | port2 = comp_self._add_input_port("print") |
| 482 | port3 = comp_self._add_input_port("insert") |
| 483 | |
| 484 | def _user_consume(self): |
| 485 | pass |
| 486 | |
| 487 | comp = self._create_comp(MySink) |
| 488 | self.assertEqual(port3.addr, comp.input_ports["insert"].addr) |
| 489 | self.assertEqual(port2.addr, comp.input_ports["print"].addr) |
| 490 | self.assertEqual(port1.addr, comp.input_ports["clear"].addr) |
| 491 | del port1 |
| 492 | del port2 |
| 493 | del port3 |
| 494 | |
| 495 | def test_gen_src_output_ports_getitem_invalid_key(self): |
| 496 | class MySource( |
| 497 | bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator |
| 498 | ): |
| 499 | def __init__(comp_self, config, params, obj): |
| 500 | comp_self._add_output_port("clear") |
| 501 | comp_self._add_output_port("print") |
| 502 | comp_self._add_output_port("insert") |
| 503 | |
| 504 | comp = self._create_comp(MySource) |
| 505 | |
| 506 | with self.assertRaises(KeyError): |
| 507 | comp.output_ports["hello"] |
| 508 | |
| 509 | def test_gen_flt_output_ports_getitem_invalid_key(self): |
| 510 | class MyFilter( |
| 511 | bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator |
| 512 | ): |
| 513 | def __init__(comp_self, config, params, obj): |
| 514 | comp_self._add_output_port("clear") |
| 515 | comp_self._add_output_port("print") |
| 516 | comp_self._add_output_port("insert") |
| 517 | |
| 518 | comp = self._create_comp(MyFilter) |
| 519 | |
| 520 | with self.assertRaises(KeyError): |
| 521 | comp.output_ports["hello"] |
| 522 | |
| 523 | def test_gen_flt_input_ports_getitem_invalid_key(self): |
| 524 | class MyFilter( |
| 525 | bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator |
| 526 | ): |
| 527 | def __init__(comp_self, config, params, obj): |
| 528 | comp_self._add_input_port("clear") |
| 529 | comp_self._add_input_port("print") |
| 530 | comp_self._add_input_port("insert") |
| 531 | |
| 532 | comp = self._create_comp(MyFilter) |
| 533 | |
| 534 | with self.assertRaises(KeyError): |
| 535 | comp.input_ports["hello"] |
| 536 | |
| 537 | def test_gen_sink_input_ports_getitem_invalid_key(self): |
| 538 | class MySink(bt2._UserSinkComponent): |
| 539 | def __init__(comp_self, config, params, obj): |
| 540 | comp_self._add_input_port("clear") |
| 541 | comp_self._add_input_port("print") |
| 542 | comp_self._add_input_port("insert") |
| 543 | |
| 544 | with self.assertRaises(KeyError): |
| 545 | comp_self._input_ports["hello"] |
| 546 | |
| 547 | def _user_consume(self): |
| 548 | pass |
| 549 | |
| 550 | comp = self._create_comp(MySink) |
| 551 | |
| 552 | with self.assertRaises(KeyError): |
| 553 | comp.input_ports["hello"] |
| 554 | |
| 555 | def test_gen_src_output_ports_len(self): |
| 556 | class MySource( |
| 557 | bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator |
| 558 | ): |
| 559 | def __init__(comp_self, config, params, obj): |
| 560 | comp_self._add_output_port("clear") |
| 561 | comp_self._add_output_port("print") |
| 562 | comp_self._add_output_port("insert") |
| 563 | |
| 564 | comp = self._create_comp(MySource) |
| 565 | self.assertEqual(len(comp.output_ports), 3) |
| 566 | |
| 567 | def test_gen_flt_output_ports_len(self): |
| 568 | class MyFilter( |
| 569 | bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator |
| 570 | ): |
| 571 | def __init__(comp_self, config, params, obj): |
| 572 | comp_self._add_output_port("clear") |
| 573 | comp_self._add_output_port("print") |
| 574 | comp_self._add_output_port("insert") |
| 575 | |
| 576 | comp = self._create_comp(MyFilter) |
| 577 | self.assertEqual(len(comp.output_ports), 3) |
| 578 | |
| 579 | def test_gen_flt_input_ports_len(self): |
| 580 | class MyFilter( |
| 581 | bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator |
| 582 | ): |
| 583 | def __init__(comp_self, config, params, obj): |
| 584 | comp_self._add_input_port("clear") |
| 585 | comp_self._add_input_port("print") |
| 586 | comp_self._add_input_port("insert") |
| 587 | |
| 588 | comp = self._create_comp(MyFilter) |
| 589 | self.assertEqual(len(comp.input_ports), 3) |
| 590 | |
| 591 | def test_gen_sink_input_ports_len(self): |
| 592 | class MySink(bt2._UserSinkComponent): |
| 593 | def __init__(comp_self, config, params, obj): |
| 594 | comp_self._add_input_port("clear") |
| 595 | comp_self._add_input_port("print") |
| 596 | comp_self._add_input_port("insert") |
| 597 | |
| 598 | def _user_consume(self): |
| 599 | pass |
| 600 | |
| 601 | comp = self._create_comp(MySink) |
| 602 | self.assertEqual(len(comp.input_ports), 3) |
| 603 | |
| 604 | def test_gen_src_output_ports_iter(self): |
| 605 | port1 = None |
| 606 | port2 = None |
| 607 | port3 = None |
| 608 | |
| 609 | class MySource( |
| 610 | bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator |
| 611 | ): |
| 612 | def __init__(comp_self, config, params, obj): |
| 613 | nonlocal port1, port2, port3 |
| 614 | port1 = comp_self._add_output_port("clear") |
| 615 | port2 = comp_self._add_output_port("print") |
| 616 | port3 = comp_self._add_output_port("insert") |
| 617 | |
| 618 | comp = self._create_comp(MySource) |
| 619 | ports = [] |
| 620 | |
| 621 | for port_name, port in comp.output_ports.items(): |
| 622 | ports.append((port_name, port)) |
| 623 | |
| 624 | self.assertEqual(ports[0][0], "clear") |
| 625 | self.assertEqual(ports[0][1].addr, port1.addr) |
| 626 | self.assertEqual(ports[1][0], "print") |
| 627 | self.assertEqual(ports[1][1].addr, port2.addr) |
| 628 | self.assertEqual(ports[2][0], "insert") |
| 629 | self.assertEqual(ports[2][1].addr, port3.addr) |
| 630 | del port1 |
| 631 | del port2 |
| 632 | del port3 |
| 633 | |
| 634 | def test_gen_flt_output_ports_iter(self): |
| 635 | port1 = None |
| 636 | port2 = None |
| 637 | port3 = None |
| 638 | |
| 639 | class MyFilter( |
| 640 | bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator |
| 641 | ): |
| 642 | def __init__(comp_self, config, params, obj): |
| 643 | nonlocal port1, port2, port3 |
| 644 | port1 = comp_self._add_output_port("clear") |
| 645 | port2 = comp_self._add_output_port("print") |
| 646 | port3 = comp_self._add_output_port("insert") |
| 647 | |
| 648 | comp = self._create_comp(MyFilter) |
| 649 | ports = [] |
| 650 | |
| 651 | for port_name, port in comp.output_ports.items(): |
| 652 | ports.append((port_name, port)) |
| 653 | |
| 654 | self.assertEqual(ports[0][0], "clear") |
| 655 | self.assertEqual(ports[0][1].addr, port1.addr) |
| 656 | self.assertEqual(ports[1][0], "print") |
| 657 | self.assertEqual(ports[1][1].addr, port2.addr) |
| 658 | self.assertEqual(ports[2][0], "insert") |
| 659 | self.assertEqual(ports[2][1].addr, port3.addr) |
| 660 | del port1 |
| 661 | del port2 |
| 662 | del port3 |
| 663 | |
| 664 | def test_gen_flt_input_ports_iter(self): |
| 665 | port1 = None |
| 666 | port2 = None |
| 667 | port3 = None |
| 668 | |
| 669 | class MyFilter( |
| 670 | bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator |
| 671 | ): |
| 672 | def __init__(comp_self, config, params, obj): |
| 673 | nonlocal port1, port2, port3 |
| 674 | port1 = comp_self._add_input_port("clear") |
| 675 | port2 = comp_self._add_input_port("print") |
| 676 | port3 = comp_self._add_input_port("insert") |
| 677 | |
| 678 | comp = self._create_comp(MyFilter) |
| 679 | ports = [] |
| 680 | |
| 681 | for port_name, port in comp.input_ports.items(): |
| 682 | ports.append((port_name, port)) |
| 683 | |
| 684 | self.assertEqual(ports[0][0], "clear") |
| 685 | self.assertEqual(ports[0][1].addr, port1.addr) |
| 686 | self.assertEqual(ports[1][0], "print") |
| 687 | self.assertEqual(ports[1][1].addr, port2.addr) |
| 688 | self.assertEqual(ports[2][0], "insert") |
| 689 | self.assertEqual(ports[2][1].addr, port3.addr) |
| 690 | del port1 |
| 691 | del port2 |
| 692 | del port3 |
| 693 | |
| 694 | def test_gen_sink_input_ports_iter(self): |
| 695 | port1 = None |
| 696 | port2 = None |
| 697 | port3 = None |
| 698 | |
| 699 | class MySink(bt2._UserSinkComponent): |
| 700 | def __init__(comp_self, config, params, obj): |
| 701 | nonlocal port1, port2, port3 |
| 702 | port1 = comp_self._add_input_port("clear") |
| 703 | port2 = comp_self._add_input_port("print") |
| 704 | port3 = comp_self._add_input_port("insert") |
| 705 | |
| 706 | def _user_consume(self): |
| 707 | pass |
| 708 | |
| 709 | comp = self._create_comp(MySink) |
| 710 | ports = [] |
| 711 | |
| 712 | for port_name, port in comp.input_ports.items(): |
| 713 | ports.append((port_name, port)) |
| 714 | |
| 715 | self.assertEqual(ports[0][0], "clear") |
| 716 | self.assertEqual(ports[0][1].addr, port1.addr) |
| 717 | self.assertEqual(ports[1][0], "print") |
| 718 | self.assertEqual(ports[1][1].addr, port2.addr) |
| 719 | self.assertEqual(ports[2][0], "insert") |
| 720 | self.assertEqual(ports[2][1].addr, port3.addr) |
| 721 | del port1 |
| 722 | del port2 |
| 723 | del port3 |
| 724 | |
| 725 | def test_name(self): |
| 726 | class MySink(bt2._UserSinkComponent): |
| 727 | def __init__(comp_self, config, params, obj): |
| 728 | comp_self._add_input_port("clear") |
| 729 | |
| 730 | def _user_consume(self): |
| 731 | pass |
| 732 | |
| 733 | comp = self._create_comp(MySink) |
| 734 | self.assertEqual(comp.input_ports["clear"].name, "clear") |
| 735 | |
| 736 | def test_connection_none(self): |
| 737 | class MySink(bt2._UserSinkComponent): |
| 738 | def __init__(comp_self, config, params, obj): |
| 739 | comp_self._add_input_port("clear") |
| 740 | |
| 741 | def _user_consume(self): |
| 742 | pass |
| 743 | |
| 744 | comp = self._create_comp(MySink) |
| 745 | self.assertIsNone(comp.input_ports["clear"].connection) |
| 746 | |
| 747 | def test_is_connected_false(self): |
| 748 | class MySink(bt2._UserSinkComponent): |
| 749 | def __init__(comp_self, config, params, obj): |
| 750 | comp_self._add_input_port("clear") |
| 751 | |
| 752 | def _user_consume(self): |
| 753 | pass |
| 754 | |
| 755 | comp = self._create_comp(MySink) |
| 756 | self.assertFalse(comp.input_ports["clear"].is_connected) |
| 757 | |
| 758 | def test_self_name(self): |
| 759 | class MySink(bt2._UserSinkComponent): |
| 760 | def __init__(comp_self, config, params, obj): |
| 761 | port = comp_self._add_input_port("clear") |
| 762 | self.assertEqual(port.name, "clear") |
| 763 | |
| 764 | def _user_consume(self): |
| 765 | pass |
| 766 | |
| 767 | self._create_comp(MySink) |
| 768 | |
| 769 | def test_self_connection_none(self): |
| 770 | class MySink(bt2._UserSinkComponent): |
| 771 | def __init__(comp_self, config, params, obj): |
| 772 | port = comp_self._add_input_port("clear") |
| 773 | self.assertIsNone(port.connection) |
| 774 | |
| 775 | def _user_consume(self): |
| 776 | pass |
| 777 | |
| 778 | self._create_comp(MySink) |
| 779 | |
| 780 | def test_self_is_connected_false(self): |
| 781 | class MySink(bt2._UserSinkComponent): |
| 782 | def __init__(comp_self, config, params, obj): |
| 783 | port = comp_self._add_input_port("clear") |
| 784 | self.assertFalse(port.is_connected) |
| 785 | |
| 786 | def _user_consume(self): |
| 787 | pass |
| 788 | |
| 789 | self._create_comp(MySink) |
| 790 | |
| 791 | def test_source_self_port_user_data(self): |
| 792 | class MyUserData: |
| 793 | def __del__(self): |
| 794 | nonlocal objects_deleted |
| 795 | objects_deleted += 1 |
| 796 | |
| 797 | class MySource( |
| 798 | bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator |
| 799 | ): |
| 800 | def __init__(comp_self, config, params, obj): |
| 801 | nonlocal user_datas |
| 802 | |
| 803 | p = comp_self._add_output_port("port1") |
| 804 | user_datas.append(p.user_data) |
| 805 | p = comp_self._add_output_port("port2", 2) |
| 806 | user_datas.append(p.user_data) |
| 807 | p = comp_self._add_output_port("port3", MyUserData()) |
| 808 | user_datas.append(p.user_data) |
| 809 | |
| 810 | user_datas = [] |
| 811 | objects_deleted = 0 |
| 812 | |
| 813 | comp = self._create_comp(MySource) |
| 814 | self.assertEqual(len(user_datas), 3) |
| 815 | self.assertIs(user_datas[0], None) |
| 816 | self.assertEqual(user_datas[1], 2) |
| 817 | self.assertIs(type(user_datas[2]), MyUserData) |
| 818 | |
| 819 | # Verify that the user data gets freed. |
| 820 | self.assertEqual(objects_deleted, 0) |
| 821 | del user_datas |
| 822 | del comp |
| 823 | self.assertEqual(objects_deleted, 1) |
| 824 | |
| 825 | def test_filter_self_port_user_data(self): |
| 826 | class MyUserData: |
| 827 | def __del__(self): |
| 828 | nonlocal objects_deleted |
| 829 | objects_deleted += 1 |
| 830 | |
| 831 | class MyFilter( |
| 832 | bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator |
| 833 | ): |
| 834 | def __init__(comp_self, config, params, obj): |
| 835 | nonlocal user_datas |
| 836 | |
| 837 | p = comp_self._add_output_port("port1") |
| 838 | user_datas.append(p.user_data) |
| 839 | p = comp_self._add_output_port("port2", "user data string") |
| 840 | user_datas.append(p.user_data) |
| 841 | p = comp_self._add_output_port("port3", MyUserData()) |
| 842 | user_datas.append(p.user_data) |
| 843 | |
| 844 | p = comp_self._add_input_port("port4") |
| 845 | user_datas.append(p.user_data) |
| 846 | p = comp_self._add_input_port("port5", user_data={"user data": "dict"}) |
| 847 | user_datas.append(p.user_data) |
| 848 | p = comp_self._add_input_port("port6", MyUserData()) |
| 849 | user_datas.append(p.user_data) |
| 850 | |
| 851 | user_datas = [] |
| 852 | objects_deleted = 0 |
| 853 | |
| 854 | comp = self._create_comp(MyFilter) |
| 855 | self.assertEqual(len(user_datas), 6) |
| 856 | self.assertIs(user_datas[0], None) |
| 857 | self.assertEqual(user_datas[1], "user data string") |
| 858 | self.assertIs(type(user_datas[2]), MyUserData) |
| 859 | self.assertIs(user_datas[3], None) |
| 860 | self.assertEqual(user_datas[4], {"user data": "dict"}) |
| 861 | self.assertIs(type(user_datas[5]), MyUserData) |
| 862 | |
| 863 | # Verify that the user data gets freed. |
| 864 | self.assertEqual(objects_deleted, 0) |
| 865 | del user_datas |
| 866 | del comp |
| 867 | self.assertEqual(objects_deleted, 2) |
| 868 | |
| 869 | def test_sink_self_port_user_data(self): |
| 870 | class MyUserData: |
| 871 | def __del__(self): |
| 872 | nonlocal objects_deleted |
| 873 | objects_deleted += 1 |
| 874 | |
| 875 | class MySink(bt2._UserSinkComponent): |
| 876 | def __init__(comp_self, config, params, obj): |
| 877 | nonlocal user_datas |
| 878 | |
| 879 | p = comp_self._add_input_port("port1") |
| 880 | user_datas.append(p.user_data) |
| 881 | p = comp_self._add_input_port("port2", MyUserData()) |
| 882 | user_datas.append(p.user_data) |
| 883 | |
| 884 | def _user_consume(self): |
| 885 | pass |
| 886 | |
| 887 | user_datas = [] |
| 888 | objects_deleted = 0 |
| 889 | |
| 890 | comp = self._create_comp(MySink) |
| 891 | self.assertEqual(len(user_datas), 2) |
| 892 | self.assertIs(user_datas[0], None) |
| 893 | self.assertIs(type(user_datas[1]), MyUserData) |
| 894 | |
| 895 | # Verify that the user data gets freed. |
| 896 | self.assertEqual(objects_deleted, 0) |
| 897 | del user_datas |
| 898 | del comp |
| 899 | self.assertEqual(objects_deleted, 1) |
| 900 | |
| 901 | |
| 902 | if __name__ == "__main__": |
| 903 | unittest.main() |