def setUp(self) -> None: self.dispatcher_shutdown = threading.Event() self.manager = multiprocessing.Manager() self.event_queue = EventQueue(self.manager.Queue()) self.dispatcher = EventDispatcher( shutdown=self.dispatcher_shutdown, event_queue=self.event_queue, )
def test_event_subscriber(self, mock_time: mock.Mock) -> None: mock_time.return_value = 1234567 self.dispatcher_shutdown = threading.Event() self.event_queue = EventQueue(multiprocessing.Manager().Queue()) self.dispatcher = EventDispatcher(shutdown=self.dispatcher_shutdown, event_queue=self.event_queue) self.subscriber = EventSubscriber(self.event_queue) self.subscriber.subscribe(self.callback) self.dispatcher.run_once() self.event_queue.publish(request_id='1234', event_name=eventNames.WORK_STARTED, event_payload={'hello': 'events'}, publisher_id=self.__class__.__name__) self.dispatcher.run_once() self.subscriber.unsubscribe() self.dispatcher.run_once() self.dispatcher_shutdown.set()
class TestEventSubscriber(unittest.TestCase): def setUp(self) -> None: self.manager = multiprocessing.Manager() self.event_queue = EventQueue(self.manager.Queue()) def tearDown(self) -> None: self.manager.shutdown() @mock.patch('time.time') def test_event_subscriber(self, mock_time: mock.Mock) -> None: mock_time.return_value = 1234567 self.dispatcher_shutdown = threading.Event() self.dispatcher = EventDispatcher( shutdown=self.dispatcher_shutdown, event_queue=self.event_queue, ) self.subscriber = EventSubscriber(self.event_queue, self.callback) self.subscriber.setup() self.dispatcher.run_once() self.event_queue.publish( request_id='1234', event_name=eventNames.WORK_STARTED, event_payload={'hello': 'events'}, publisher_id=self.__class__.__name__, ) self.dispatcher.run_once() self.subscriber.unsubscribe() self.dispatcher.run_once() self.subscriber.shutdown(do_unsubscribe=False) with self.assertRaises(queue.Empty): self.dispatcher.run_once() self.dispatcher_shutdown.set() def callback(self, ev: Dict[str, Any]) -> None: self.assertEqual( ev, { 'request_id': '1234', 'process_id': os.getpid(), 'thread_id': PUBLISHER_ID, 'event_timestamp': 1234567, 'event_name': eventNames.WORK_STARTED, 'event_payload': { 'hello': 'events' }, 'publisher_id': self.__class__.__name__, }, )
def test_event_subscriber(self, mock_time: mock.Mock) -> None: mock_time.return_value = 1234567 self.dispatcher_shutdown = threading.Event() self.dispatcher = EventDispatcher( shutdown=self.dispatcher_shutdown, event_queue=self.event_queue, ) self.subscriber = EventSubscriber(self.event_queue, self.callback) self.subscriber.setup() self.dispatcher.run_once() self.event_queue.publish( request_id='1234', event_name=eventNames.WORK_STARTED, event_payload={'hello': 'events'}, publisher_id=self.__class__.__name__, ) self.dispatcher.run_once() self.subscriber.unsubscribe() self.dispatcher.run_once() self.subscriber.shutdown(do_unsubscribe=False) with self.assertRaises(queue.Empty): self.dispatcher.run_once() self.dispatcher_shutdown.set()
'''Subscriber callback.''' global num_events_received if payload['request_id'] == main_publisher_request_id: num_events_received[0] += 1 else: num_events_received[1] += 1 # print(payload) if __name__ == '__main__': start_time = time.time() # Start dispatcher thread dispatcher_queue = EventQueue(manager.Queue()) dispatcher_shutdown_event = threading.Event() dispatcher = EventDispatcher(shutdown=dispatcher_shutdown_event, event_queue=dispatcher_queue) dispatcher_thread = threading.Thread(target=dispatcher.run) dispatcher_thread.start() # Create a subscriber subscriber = EventSubscriber(dispatcher_queue) # Internally, subscribe will start a separate thread # to receive incoming published messages subscriber.subscribe(on_event) # Start a publisher process to demonstrate safe exchange # of messages between processes. publisher_shutdown_event = multiprocessing.Event() publisher = multiprocessing.Process(target=publisher_process, args=( publisher_shutdown_event,
class TestEventDispatcher(unittest.TestCase): def setUp(self) -> None: self.dispatcher_shutdown = threading.Event() self.event_queue = EventQueue(multiprocessing.Manager().Queue()) self.dispatcher = EventDispatcher( shutdown=self.dispatcher_shutdown, event_queue=self.event_queue) def tearDown(self) -> None: self.dispatcher_shutdown.set() def test_empties_queue(self) -> None: self.event_queue.publish( request_id='1234', event_name=eventNames.WORK_STARTED, event_payload={'hello': 'events'}, publisher_id=self.__class__.__name__ ) self.dispatcher.run_once() with self.assertRaises(queue.Empty): self.dispatcher.run_once() @mock.patch('time.time') def subscribe(self, mock_time: mock.Mock) -> DictQueueType: mock_time.return_value = 1234567 q = multiprocessing.Manager().Queue() self.event_queue.subscribe(sub_id='1234', channel=q) self.dispatcher.run_once() self.event_queue.publish( request_id='1234', event_name=eventNames.WORK_STARTED, event_payload={'hello': 'events'}, publisher_id=self.__class__.__name__ ) self.dispatcher.run_once() self.assertEqual(q.get(), { 'request_id': '1234', 'process_id': os.getpid(), 'thread_id': threading.get_ident(), 'event_timestamp': 1234567, 'event_name': eventNames.WORK_STARTED, 'event_payload': {'hello': 'events'}, 'publisher_id': self.__class__.__name__, }) return q def test_subscribe(self) -> None: self.subscribe() def test_unsubscribe(self) -> None: q = self.subscribe() self.event_queue.unsubscribe('1234') self.dispatcher.run_once() self.event_queue.publish( request_id='1234', event_name=eventNames.WORK_STARTED, event_payload={'hello': 'events'}, publisher_id=self.__class__.__name__ ) self.dispatcher.run_once() with self.assertRaises(queue.Empty): q.get(timeout=0.1) def test_unsubscribe_on_broken_pipe_error(self) -> None: pass def test_run(self) -> None: pass
class TestEventDispatcher(unittest.TestCase): def setUp(self) -> None: self.dispatcher_shutdown = threading.Event() self.manager = multiprocessing.Manager() self.event_queue = EventQueue(self.manager.Queue()) self.dispatcher = EventDispatcher( shutdown=self.dispatcher_shutdown, event_queue=self.event_queue, ) def tearDown(self) -> None: self.dispatcher_shutdown.set() self.manager.shutdown() def test_empties_queue(self) -> None: self.event_queue.publish( request_id='1234', event_name=eventNames.WORK_STARTED, event_payload={'hello': 'events'}, publisher_id=self.__class__.__name__, ) self.dispatcher.run_once() with self.assertRaises(queue.Empty): self.dispatcher.run_once() @mock.patch('time.time') def subscribe(self, mock_time: mock.Mock) -> connection.Connection: mock_time.return_value = 1234567 relay_recv, relay_send = multiprocessing.Pipe() self.event_queue.subscribe(sub_id='1234', channel=relay_send) # consume the subscribe event self.dispatcher.run_once() # assert subscribed ack self.assertEqual( relay_recv.recv(), { 'event_name': eventNames.SUBSCRIBED, }, ) # publish event self.event_queue.publish( request_id='1234', event_name=eventNames.WORK_STARTED, event_payload={'hello': 'events'}, publisher_id=self.__class__.__name__, ) # consume self.dispatcher.run_once() # assert recv r = relay_recv.recv() print(r) self.assertEqual( r, { 'request_id': '1234', 'process_id': os.getpid(), 'thread_id': threading.get_ident(), 'event_timestamp': 1234567, 'event_name': eventNames.WORK_STARTED, 'event_payload': { 'hello': 'events' }, 'publisher_id': self.__class__.__name__, }, ) return relay_recv def test_subscribe(self) -> None: self.subscribe() def test_unsubscribe(self) -> None: relay_recv = self.subscribe() self.event_queue.unsubscribe('1234') self.dispatcher.run_once() # assert unsubscribe ack self.assertEqual( relay_recv.recv(), { 'event_name': eventNames.UNSUBSCRIBED, }, ) self.event_queue.publish( request_id='1234', event_name=eventNames.WORK_STARTED, event_payload={'hello': 'events'}, publisher_id=self.__class__.__name__, ) self.dispatcher.run_once() with self.assertRaises(EOFError): relay_recv.recv()