class DispatcherTests(TestCase): def setUp(self): self.dispatcher = Dispatcher() def test_register_rejects_non_listener(self): with self.assertRaises(AssertionError): self.dispatcher.register("this is not a listener") def test_notifies_only_once_despite_multiple_registration(self): listener = self._fake_listener() self.dispatcher.register(listener) self.dispatcher.register(listener) self.dispatcher.task_created(FAKE_REQUEST) listener.task_created.assert_called_once_with(FAKE_REQUEST) def test_dispatch(self): invocations = [ ("task_created", [FAKE_TASK]), ("task_accepted", [FAKE_TASK]), ("task_rejected", [FAKE_TASK]), ("task_assigned_to", [FAKE_TASK, FAKE_WORKER]), ("task_activated", [FAKE_TASK]), ("task_paused", [FAKE_TASK]), ("task_failed", [FAKE_TASK]), ("task_successful", [FAKE_TASK]), ("task_cancelled", [FAKE_TASK]), ("posting_of", [FAKE_SERVICE, FAKE_REQUEST]), ("acceptance_of", [FAKE_REQUEST]), ("rejection_of", [FAKE_REQUEST]), ("success_of", [FAKE_REQUEST]), ("failure_of", [FAKE_REQUEST]), ("timeout_of", [FAKE_REQUEST]), ("worker_created", [FAKE_WORKER]), ("worker_created", [FAKE_WORKER]), ("worker_busy", [FAKE_WORKER]), ("worker_idle", [FAKE_WORKER]), ("worker_shutdown", [FAKE_WORKER]), ] for (method_name, parameters) in invocations: self._do_test_dispatch_of(method_name, *parameters) def _do_test_dispatch_of(self, method_name, *parameters): listener = self._fake_listener() method = getattr(self.dispatcher, method_name) method(*parameters) delegate = getattr(listener, method_name) delegate.assert_called_once_with(*parameters) def _fake_listener(self): listener = MagicMock(Listener) self.dispatcher.register(listener) return listener
def _create_monitor(self, period=50): environment = self.simulation.environment.create_local_environment() environment.define(Symbols.LISTENER, Dispatcher()) self._create_fake_service(environment) monitor = Monitor(Symbols.MONITOR, environment, period) self.simulation.environment.define(Symbols.MONITOR, monitor) return monitor
class DispatcherTests(TestCase): def setUp(self): self.dispatcher = Dispatcher() def test_register_rejects_non_listener(self): with self.assertRaises(AssertionError): self.dispatcher.register("this is not a listener") def test_notifies_only_once_despite_multiple_registration(self): listener = self._fake_listener() self.dispatcher.register(listener) self.dispatcher.register(listener) self.dispatcher.arrival_of(FAKE_REQUEST) listener.arrival_of.assert_called_once_with(FAKE_REQUEST) def test_dispatch(self): invocations = [("arrival_of", [FAKE_REQUEST]), ("rejection_of", [FAKE_REQUEST]), ("posting_of", [FAKE_SERVICE, FAKE_REQUEST]), ("success_of", [FAKE_REQUEST]), ("failure_of", [FAKE_REQUEST]), ("timeout_of", [FAKE_REQUEST]), ("storage_of", [FAKE_REQUEST]), ("selection_of", [FAKE_REQUEST]), ("resuming", [FAKE_REQUEST]), ("error_replied_to", [FAKE_REQUEST]), ("success_replied_to", [FAKE_REQUEST])] for (method_name, parameters) in invocations: self._do_test_dispatch_of(method_name, *parameters) def _do_test_dispatch_of(self, method_name, *parameters): listener = self._fake_listener() method = getattr(self.dispatcher, method_name) method(*parameters) delegate = getattr(listener, method_name) delegate.assert_called_once_with(*parameters) def _fake_listener(self): listener = MagicMock(Listener) self.dispatcher.register(listener) return listener
def setUp(self): self.dispatcher = Dispatcher()
def create_listener(self): return Dispatcher()