def test_subscribe_static_bundles(self): """Tests that @subscribe_static bundles register their listeners.""" bundle = subscription_bundle.create_from_static(TestClass) bundle.register() event_bus.post(Event()) self.assertEqual(len(TestClass.instance_event_received), 0) self.assertEqual(len(TestClass.static_event_received), 1)
def test_event_register(self): """Tests that event.register()'d functions can receive posted Events.""" def event_listener(_): self.called_event = True event_bus.register(Event, event_listener) event_bus.post(Event()) self.assertTrue(self.called_event)
def _setup_class(self): """Proxy function to guarantee the base implementation of setup_class is called. """ event_bus.post(TestClassBeginEvent(self)) # Import and register the built-in controller modules specified # in testbed config. for module in self._import_builtin_controllers(): self.register_controller(module, builtin=True) return self.setup_class()
def test_event_unregister(self): """Tests that an event can be registered, and then unregistered.""" def event_listener(_): self.called_event = False registration_id = event_bus.register(Event, event_listener) event_bus.unregister(registration_id) event_bus.post(Event()) self.assertFalse(self.called_event)
def test_post_to_unregistered_event_does_not_call_other_funcs(self): """Tests posting an unregistered event will not call other funcs.""" mock_subscription = Mock() bus = event_bus._event_bus mock_type = Mock() mock_subscription.event_type = mock_type bus._subscriptions[mock_type] = [mock_subscription] event_bus.post(Mock()) self.assertEqual(mock_subscription.deliver.call_count, 0)
def test_subscribe_instance_bundles(self): """Tests that @subscribe bundles register only instance listeners.""" test_run_config = mobly_config_parser.TestRunConfig() test_run_config.testbed_name = '' test_run_config.log_path = '' test_object = TestClass(test_run_config) bundle = subscription_bundle.create_from_instance(test_object) bundle.register() event_bus.post(Event()) self.assertEqual(len(TestClass.instance_event_received), 1) self.assertEqual(len(TestClass.static_event_received), 0)
def test_post_to_registered_event_calls_all_registered_funcs(self): """Tests posting to a registered event calls all registered funcs.""" mock_subscriptions = [Mock(), Mock(), Mock()] bus = event_bus._event_bus for subscription in mock_subscriptions: subscription.order = 0 mock_event = Mock() bus._subscriptions[type(mock_event)] = mock_subscriptions event_bus.post(mock_event) for subscription in mock_subscriptions: subscription.deliver.assert_called_once_with(mock_event)
def _update_test_class_context(event): """Pushes a new TestClassContext to the _contexts stack upon a TestClassBeginEvent. Pops the most recent context off the stack upon a TestClassEndEvent. Posts the context change to the event bus. Args: event: An instance of TestClassBeginEvent or TestClassEndEvent. """ if isinstance(event, TestClassBeginEvent): _contexts.append(_get_context_for_test_class_event(event)) if isinstance(event, TestClassEndEvent): if _contexts: _contexts.pop() event_bus.post(NewTestClassContextEvent())
def test_register_static_subscriptions_registers_properly(self): @register_static_subscriptions class RegisterStaticSubscriptionsClass(object): captured_event = None @staticmethod @subscribe_static(DummyEvent) def on_static_event(evt): RegisterStaticSubscriptionsClass.captured_event = evt event = DummyEvent() event_bus.post(event) self.assertEqual( event, RegisterStaticSubscriptionsClass.captured_event, 'register_static_subscriptions did not subscribe ' 'RegisterStaticSubscriptionsClass.on_static_event.')
def test_register_instance_subscriptions_registers_properly(self): @register_instance_subscriptions class RegisterInstanceSubscriptionsClass(object): def __init__(self): self.captured_event = None @subscribe(DummyEvent) def on_instance_event(self, evt): self.captured_event = evt instance = RegisterInstanceSubscriptionsClass() event = DummyEvent() event_bus.post(event) self.assertEqual( event, instance.captured_event, 'register_instance_subscriptions did not subscribe the instance ' 'function RegisterInstanceSubscriptionsClass.on_instance_event.')
def test_post_with_ignore_errors_calls_all_registered_funcs(self): """Tests posting with ignore_errors=True calls all registered funcs, even if they raise errors. """ def _raise(_): raise Exception mock_event = Mock() mock_subscriptions = [Mock(), Mock(), Mock()] mock_subscriptions[0].deliver.side_effect = _raise bus = event_bus._event_bus for i, subscription in enumerate(mock_subscriptions): subscription.order = i bus._subscriptions[type(mock_event)] = mock_subscriptions event_bus.post(mock_event, ignore_errors=True) for subscription in mock_subscriptions: subscription.deliver.assert_called_once_with(mock_event)
def test_event_deliver_only_to_matching_serial(self, start_fn): """Test that the service only responds to events that matches its device serial. """ event_bus._event_bus = event_bus._EventBus() service = services.AndroidService(mock.Mock()) service.ad.serial = 'right_serial' service.register() wrong_ad = mock.Mock() wrong_ad.serial = 'wrong_serial' wrong_event = AndroidStartServicesEvent(wrong_ad) event_bus.post(wrong_event) start_fn.assert_not_called() right_ad = mock.Mock() right_ad.serial = 'right_serial' right_event = AndroidStartServicesEvent(right_ad) event_bus.post(right_event) start_fn.assert_called_with(right_event)
def exec_one_testcase(self, test_name, test_func): """Executes one test case and update test results. Executes one test case, create a records.TestResultRecord object with the execution information, and add the record to the test class's test results. Args: test_name: Name of the test. test_func: The test function. """ class_name = self.__class__.__name__ tr_record = records.TestResultRecord(test_name, class_name) tr_record.test_begin() self.begin_time = int(tr_record.begin_time) self.log_begin_time = tr_record.log_begin_time self.test_name = tr_record.test_name event_bus.post(TestCaseBeginEvent(self, self.test_name)) self.log.info("%s %s", TEST_CASE_TOKEN, test_name) # Enable test retry if specified in the ACTS config retry_tests = self.user_params.get('retry_tests', []) full_test_name = '%s.%s' % (class_name, self.test_name) if any(name in retry_tests for name in [class_name, full_test_name]): test_func = self.get_func_with_retry(test_func) verdict = None test_signal = None try: try: ret = self._setup_test(self.test_name) asserts.assert_true(ret is not False, "Setup for %s failed." % test_name) verdict = test_func() finally: try: self._teardown_test(self.test_name) except signals.TestAbortAll: raise except Exception as e: self.log.error(traceback.format_exc()) tr_record.add_error("teardown_test", e) self._exec_procedure_func(self._on_exception, tr_record) except (signals.TestFailure, AssertionError) as e: test_signal = e if self.user_params.get( keys.Config.key_test_failure_tracebacks.value, False): self.log.exception(e) tr_record.test_fail(e) self._exec_procedure_func(self._on_fail, tr_record) except signals.TestSkip as e: # Test skipped. test_signal = e tr_record.test_skip(e) self._exec_procedure_func(self._on_skip, tr_record) except (signals.TestAbortClass, signals.TestAbortAll) as e: # Abort signals, pass along. test_signal = e tr_record.test_fail(e) self._exec_procedure_func(self._on_fail, tr_record) raise e except signals.TestPass as e: # Explicit test pass. test_signal = e tr_record.test_pass(e) self._exec_procedure_func(self._on_pass, tr_record) except Exception as e: test_signal = e self.log.error(traceback.format_exc()) # Exception happened during test. tr_record.test_error(e) self._exec_procedure_func(self._on_exception, tr_record) self._exec_procedure_func(self._on_fail, tr_record) else: if verdict or (verdict is None): # Test passed. tr_record.test_pass() self._exec_procedure_func(self._on_pass, tr_record) return tr_record.test_fail() self._exec_procedure_func(self._on_fail, tr_record) finally: self.results.add_record(tr_record) self.summary_writer.dump(tr_record.to_dict(), records.TestSummaryEntryType.RECORD) self.current_test_name = None event_bus.post(TestCaseEndEvent(self, self.test_name, test_signal))
def _teardown_class(self): """Proxy function to guarantee the base implementation of teardown_class is called. """ super()._teardown_class() event_bus.post(TestClassEndEvent(self, self.results))
def test_post_event(self): event_bus.post(Event())