Ejemplo n.º 1
0
 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,
     )
Ejemplo n.º 2
0
    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()
Ejemplo n.º 3
0
    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()
Ejemplo n.º 4
0
    '''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,