diff options
-rw-r--r-- | bitbake/lib/bb/tests/event.py | 120 |
1 files changed, 111 insertions, 9 deletions
diff --git a/bitbake/lib/bb/tests/event.py b/bitbake/lib/bb/tests/event.py index c7eb1fe44c..1ddcb411de 100644 --- a/bitbake/lib/bb/tests/event.py +++ b/bitbake/lib/bb/tests/event.py | |||
@@ -30,28 +30,45 @@ import time | |||
30 | import pickle | 30 | import pickle |
31 | from unittest.mock import Mock | 31 | from unittest.mock import Mock |
32 | from unittest.mock import call | 32 | from unittest.mock import call |
33 | from bb.msg import BBLogFormatter | ||
33 | 34 | ||
34 | 35 | ||
35 | class EventQueueStub(): | 36 | class EventQueueStubBase(object): |
36 | """ Class used as specification for UI event handler queue stub objects """ | 37 | """ Base class for EventQueueStub classes """ |
37 | def __init__(self): | 38 | def __init__(self): |
39 | self.event_calls = [] | ||
38 | return | 40 | return |
39 | 41 | ||
40 | def send(self, event): | 42 | def _store_event_data_string(self, event): |
43 | if isinstance(event, logging.LogRecord): | ||
44 | formatter = BBLogFormatter("%(levelname)s: %(message)s") | ||
45 | self.event_calls.append(formatter.format(event)) | ||
46 | else: | ||
47 | self.event_calls.append(bb.event.getName(event)) | ||
41 | return | 48 | return |
42 | 49 | ||
43 | 50 | ||
44 | class PickleEventQueueStub(): | 51 | class EventQueueStub(EventQueueStubBase): |
52 | """ Class used as specification for UI event handler queue stub objects """ | ||
53 | def __init__(self): | ||
54 | super(EventQueueStub, self).__init__() | ||
55 | |||
56 | def send(self, event): | ||
57 | super(EventQueueStub, self)._store_event_data_string(event) | ||
58 | |||
59 | |||
60 | class PickleEventQueueStub(EventQueueStubBase): | ||
45 | """ Class used as specification for UI event handler queue stub objects | 61 | """ Class used as specification for UI event handler queue stub objects |
46 | with sendpickle method """ | 62 | with sendpickle method """ |
47 | def __init__(self): | 63 | def __init__(self): |
48 | return | 64 | super(PickleEventQueueStub, self).__init__() |
49 | 65 | ||
50 | def sendpickle(self, pickled_event): | 66 | def sendpickle(self, pickled_event): |
51 | return | 67 | event = pickle.loads(pickled_event) |
68 | super(PickleEventQueueStub, self)._store_event_data_string(event) | ||
52 | 69 | ||
53 | 70 | ||
54 | class UIClientStub(): | 71 | class UIClientStub(object): |
55 | """ Class used as specification for UI event handler stub objects """ | 72 | """ Class used as specification for UI event handler stub objects """ |
56 | def __init__(self): | 73 | def __init__(self): |
57 | self.event = None | 74 | self.event = None |
@@ -59,7 +76,7 @@ class UIClientStub(): | |||
59 | 76 | ||
60 | class EventHandlingTest(unittest.TestCase): | 77 | class EventHandlingTest(unittest.TestCase): |
61 | """ Event handling test class """ | 78 | """ Event handling test class """ |
62 | _threadlock_test_calls = [] | 79 | |
63 | 80 | ||
64 | def setUp(self): | 81 | def setUp(self): |
65 | self._test_process = Mock() | 82 | self._test_process = Mock() |
@@ -179,6 +196,33 @@ class EventHandlingTest(unittest.TestCase): | |||
179 | self.assertEqual(self._test_process.event_handler2.call_args_list, | 196 | self.assertEqual(self._test_process.event_handler2.call_args_list, |
180 | expected_event_handler2) | 197 | expected_event_handler2) |
181 | 198 | ||
199 | def test_class_handler_filters(self): | ||
200 | """ Test filters for class handlers """ | ||
201 | mask = ["bb.event.OperationStarted"] | ||
202 | result = bb.event.register("event_handler1", | ||
203 | self._test_process.event_handler1, | ||
204 | mask) | ||
205 | self.assertEqual(result, bb.event.Registered) | ||
206 | result = bb.event.register("event_handler2", | ||
207 | self._test_process.event_handler2, | ||
208 | "*") | ||
209 | self.assertEqual(result, bb.event.Registered) | ||
210 | bb.event.set_eventfilter( | ||
211 | lambda name, handler, event, d : | ||
212 | name == 'event_handler2' and | ||
213 | bb.event.getName(event) == "OperationStarted") | ||
214 | event1 = bb.event.OperationStarted() | ||
215 | event2 = bb.event.OperationCompleted(total=123) | ||
216 | bb.event.fire_class_handlers(event1, None) | ||
217 | bb.event.fire_class_handlers(event2, None) | ||
218 | bb.event.fire_class_handlers(event2, None) | ||
219 | expected_event_handler1 = [] | ||
220 | expected_event_handler2 = [call(event1)] | ||
221 | self.assertEqual(self._test_process.event_handler1.call_args_list, | ||
222 | expected_event_handler1) | ||
223 | self.assertEqual(self._test_process.event_handler2.call_args_list, | ||
224 | expected_event_handler2) | ||
225 | |||
182 | def test_change_handler_event_mapping(self): | 226 | def test_change_handler_event_mapping(self): |
183 | """ Test changing the event mapping for class handlers """ | 227 | """ Test changing the event mapping for class handlers """ |
184 | event1 = bb.event.OperationStarted() | 228 | event1 = bb.event.OperationStarted() |
@@ -259,6 +303,61 @@ class EventHandlingTest(unittest.TestCase): | |||
259 | self.assertEqual(self._test_ui2.event.sendpickle.call_args_list, | 303 | self.assertEqual(self._test_ui2.event.sendpickle.call_args_list, |
260 | expected) | 304 | expected) |
261 | 305 | ||
306 | def test_ui_handler_mask_filter(self): | ||
307 | """ Test filters for UI handlers """ | ||
308 | mask = ["bb.event.OperationStarted"] | ||
309 | debug_domains = {} | ||
310 | self._test_ui1.event = Mock(spec_set=EventQueueStub) | ||
311 | result = bb.event.register_UIHhandler(self._test_ui1, mainui=True) | ||
312 | bb.event.set_UIHmask(result, logging.INFO, debug_domains, mask) | ||
313 | self._test_ui2.event = Mock(spec_set=PickleEventQueueStub) | ||
314 | result = bb.event.register_UIHhandler(self._test_ui2, mainui=True) | ||
315 | bb.event.set_UIHmask(result, logging.INFO, debug_domains, mask) | ||
316 | |||
317 | event1 = bb.event.OperationStarted() | ||
318 | event2 = bb.event.OperationCompleted(total=1) | ||
319 | |||
320 | bb.event.fire_ui_handlers(event1, None) | ||
321 | bb.event.fire_ui_handlers(event2, None) | ||
322 | expected = [call(event1)] | ||
323 | self.assertEqual(self._test_ui1.event.send.call_args_list, | ||
324 | expected) | ||
325 | expected = [call(pickle.dumps(event1))] | ||
326 | self.assertEqual(self._test_ui2.event.sendpickle.call_args_list, | ||
327 | expected) | ||
328 | |||
329 | def test_ui_handler_log_filter(self): | ||
330 | """ Test log filters for UI handlers """ | ||
331 | mask = ["*"] | ||
332 | debug_domains = {'BitBake.Foo': logging.WARNING} | ||
333 | |||
334 | self._test_ui1.event = EventQueueStub() | ||
335 | result = bb.event.register_UIHhandler(self._test_ui1, mainui=True) | ||
336 | bb.event.set_UIHmask(result, logging.ERROR, debug_domains, mask) | ||
337 | self._test_ui2.event = PickleEventQueueStub() | ||
338 | result = bb.event.register_UIHhandler(self._test_ui2, mainui=True) | ||
339 | bb.event.set_UIHmask(result, logging.ERROR, debug_domains, mask) | ||
340 | |||
341 | event1 = bb.event.OperationStarted() | ||
342 | bb.event.fire_ui_handlers(event1, None) # All events match | ||
343 | |||
344 | event_log_handler = bb.event.LogHandler() | ||
345 | logger = logging.getLogger("BitBake") | ||
346 | logger.addHandler(event_log_handler) | ||
347 | logger1 = logging.getLogger("BitBake.Foo") | ||
348 | logger1.warning("Test warning LogRecord1") # Matches debug_domains level | ||
349 | logger1.info("Test info LogRecord") # Filtered out | ||
350 | logger2 = logging.getLogger("BitBake.Bar") | ||
351 | logger2.error("Test error LogRecord") # Matches filter base level | ||
352 | logger2.warning("Test warning LogRecord2") # Filtered out | ||
353 | logger.removeHandler(event_log_handler) | ||
354 | |||
355 | expected = ['OperationStarted', | ||
356 | 'WARNING: Test warning LogRecord1', | ||
357 | 'ERROR: Test error LogRecord'] | ||
358 | self.assertEqual(self._test_ui1.event.event_calls, expected) | ||
359 | self.assertEqual(self._test_ui2.event.event_calls, expected) | ||
360 | |||
262 | def test_fire(self): | 361 | def test_fire(self): |
263 | """ Test fire method used to trigger class and ui event handlers """ | 362 | """ Test fire method used to trigger class and ui event handlers """ |
264 | mask = ["bb.event.ConfigParsed"] | 363 | mask = ["bb.event.ConfigParsed"] |
@@ -295,12 +394,14 @@ class EventHandlingTest(unittest.TestCase): | |||
295 | event2 = bb.event.OperationCompleted(total=123) | 394 | event2 = bb.event.OperationCompleted(total=123) |
296 | bb.event.fire(event1, None) | 395 | bb.event.fire(event1, None) |
297 | bb.event.fire(event2, None) | 396 | bb.event.fire(event2, None) |
397 | event_log_handler = bb.event.LogHandler() | ||
298 | logger = logging.getLogger("BitBake") | 398 | logger = logging.getLogger("BitBake") |
299 | logger.addHandler(bb.event.LogHandler()) | 399 | logger.addHandler(event_log_handler) |
300 | logger.info("Test info LogRecord") | 400 | logger.info("Test info LogRecord") |
301 | logger.warning("Test warning LogRecord") | 401 | logger.warning("Test warning LogRecord") |
302 | with self.assertLogs("BitBake", level="INFO") as cm: | 402 | with self.assertLogs("BitBake", level="INFO") as cm: |
303 | bb.event.print_ui_queue() | 403 | bb.event.print_ui_queue() |
404 | logger.removeHandler(event_log_handler) | ||
304 | self.assertEqual(cm.output, | 405 | self.assertEqual(cm.output, |
305 | ["INFO:BitBake:Test info LogRecord", | 406 | ["INFO:BitBake:Test info LogRecord", |
306 | "WARNING:BitBake:Test warning LogRecord"]) | 407 | "WARNING:BitBake:Test warning LogRecord"]) |
@@ -364,6 +465,7 @@ class EventHandlingTest(unittest.TestCase): | |||
364 | self.assertEqual(self._threadlock_test_calls, | 465 | self.assertEqual(self._threadlock_test_calls, |
365 | ["w1_ui1", "w1_ui2", "w2_ui1", "w2_ui2"]) | 466 | ["w1_ui1", "w1_ui2", "w2_ui1", "w2_ui2"]) |
366 | 467 | ||
468 | |||
367 | def test_disable_threadlock(self): | 469 | def test_disable_threadlock(self): |
368 | """ Test disable_threadlock method """ | 470 | """ Test disable_threadlock method """ |
369 | self._set_threadlock_test_mockups() | 471 | self._set_threadlock_test_mockups() |