Commit | Line | Data |
---|---|---|
0235b0db MJ |
1 | # SPDX-License-Identifier: GPL-2.0-only |
2 | # | |
3 | # Copyright (C) 2019 EfficiOS Inc. | |
4 | # | |
5 | ||
b5cd7d65 FD |
6 | import os |
7 | import time | |
5995b304 SM |
8 | import signal |
9 | ||
10 | import bt2 | |
b5cd7d65 FD |
11 | |
12 | bt2.register_plugin(__name__, "test_exit_status") | |
13 | ||
14 | ||
15 | class StatusIter(bt2._UserMessageIterator): | |
16 | def __init__(self, config, output_port): | |
f5567ea8 | 17 | self.case = output_port.user_data["case"] |
b5cd7d65 FD |
18 | |
19 | def __next__(self): | |
20 | if self.case == "STOP": | |
21 | raise bt2.Stop() | |
22 | if self.case == "INTERRUPTED": | |
23 | os.kill(os.getpid(), signal.SIGINT) | |
24 | ||
25 | # Wait until the graph is in the interrupted state. | |
26 | timeout_s = 10 | |
27 | for _ in range(timeout_s * 10): | |
28 | if self._is_interrupted: | |
29 | raise bt2.TryAgain() | |
30 | ||
31 | time.sleep(0.1) | |
32 | ||
33 | raise Exception( | |
f5567ea8 | 34 | "{} was not interrupted after {} seconds".format( |
b5cd7d65 FD |
35 | self.__class__.__name__, timeout_s |
36 | ) | |
37 | ) | |
38 | ||
39 | elif self.case == "ERROR": | |
40 | raise TypeError("Raising type error") | |
41 | else: | |
42 | raise ValueError("Invalid parameter") | |
43 | ||
44 | ||
45 | @bt2.plugin_component_class | |
46 | class StatusSrc(bt2._UserSourceComponent, message_iterator_class=StatusIter): | |
47 | def __init__(self, config, params, obj): | |
f5567ea8 | 48 | self._add_output_port("out", {"case": params["case"]}) |