def setUp(self): global executable self.loop = asyncio.get_event_loop() self.pool = ProcessPoolExecutor(max_workers=4) self.publisher = Communicator() self.consumer = Communicator() self.in_out_func = executable self.consumer.consume = AsyncMock(return_value=None)
def setUp(self): global executable self.loop = asyncio.get_event_loop() ex_h = self.loop.get_exception_handler() def err_handler(context, error): ex_h(context, error) raise Exception(str(error)) self.loop.set_exception_handler(err_handler) self.pool = ProcessPoolExecutor(max_workers=4) self.publisher = Communicator() self.sink_func = executable self.pool.submit = MagicMock(return_value=_Future(1))
class TestSinkWorker(unittest.TestCase): def setUp(self): global executable self.loop = asyncio.get_event_loop() ex_h = self.loop.get_exception_handler() def err_handler(context, error): ex_h(context, error) raise Exception(str(error)) self.loop.set_exception_handler(err_handler) self.pool = ProcessPoolExecutor(max_workers=4) self.publisher = Communicator() self.sink_func = executable self.pool.submit = MagicMock(return_value=_Future(1)) def test_sinking(self): global callback terminate_event = asyncio.Event() sink_worker = SinkWorker(self.loop, self.pool, self.sink_func, self.publisher, asyncio.Event(), terminate_event, callback=callback) future = self.loop.create_task(sink_worker.start()) self.loop.run_until_complete(fake_sleep(1)) terminate_event.set() self.loop.create_task(self.publisher.consume(1)) self.loop.run_until_complete(fake_sleep(1)) self.pool.submit.assert_called_once_with(self.sink_func, None, 1) assert future.exception() is None
def add_http_listener(self, endpoint): self.start_event = asyncio.Event() self.terminate_event = asyncio.Event() self.start_event_name = str(uuid4()) self.start_events[self.start_event_name] = self.start_event self.terminate_events.append(self.terminate_event) from asyncexec.channels.http_c import HTTPListener assert 'http' in self.middleware_config communicator = Communicator() http_listener = HTTPListener(self.loop, self.middleware_config['http'], endpoint, communicator) self.coroutines.append(http_listener) self.previous_communicator = communicator return self
def add_worker(self, func): if self.previous_communicator is None or len(self.coroutines) is 0: raise Exception( "Cannot add a worker without anything to listen from") next_communicator = Communicator() ready_event = asyncio.Event() in_out_worker = InOutWorker(self.loop, self.pool, func, self.previous_communicator, next_communicator, ready_event, self.terminate_event) self.ready_events[self.start_event_name].append(ready_event) self.previous_communicator = next_communicator self.coroutines.append(in_out_worker) return self
def add_generator(self, func): self.start_event = asyncio.Event() self.start_event_name = str(uuid4()) self.start_events[self.start_event_name] = self.start_event self.terminate_event = asyncio.Event() self.terminate_events.append(self.terminate_event) self.previous_communicator = Communicator() generator_worker = GeneratorWorker(self.loop, self.pool, func, self.previous_communicator, self.start_event, self.terminate_event) self.coroutines.append(generator_worker) return self
def add_listener(self, tech, queue): assert self.middleware_config.get(tech) is not None self.start_event = asyncio.Event() self.terminate_event = asyncio.Event() self.start_event_name = str(uuid4()) self.start_events[self.start_event_name] = self.start_event self.terminate_events.append(self.terminate_event) communicator = Communicator() listener = ListenerFactory.instantiate( tech, self.loop, self.middleware_config.get(tech), queue, communicator, self.start_event, self.terminate_event, flow_id=self.id) self.coroutines.append(listener) self.previous_communicator = communicator return self
class TestInOutWorker(unittest.TestCase): def setUp(self): global executable self.loop = asyncio.get_event_loop() self.pool = ProcessPoolExecutor(max_workers=4) self.publisher = Communicator() self.consumer = Communicator() self.in_out_func = executable self.consumer.consume = AsyncMock(return_value=None) def test_sinking(self): inout_worker = InOutWorker(self.loop, self.pool, self.in_out_func, self.publisher, self.consumer, asyncio.Event(), asyncio.Event()) self.loop.create_task(self.publisher.consume(1)) self.loop.create_task(inout_worker.start()) self.loop.run_until_complete(asyncio.sleep(.1)) self.consumer.consume.assert_called_once_with(1) def tearDown(self): for task in asyncio.Task.all_tasks(): task.cancel()
def add_broadcast_publisher(self, tech, *queues): from asyncexec.channels import CompositePublisher assert self.middleware_config.get(tech) is not None if self.previous_communicator is None: raise Exception( "Cannot add a publisher to middleware without anything to publish from" ) consumers = [] for queue in queues: logger.info("Adding queue {}".format(queue)) ready_event = asyncio.Event() communicator = Communicator() publisher = PublisherFactory.instantiate( tech, self.loop, self.middleware_config.get(tech), queue, communicator, ready_event, self.terminate_event, flow_id=self.id) self.ready_events[self.start_event_name].append(ready_event) consumers.append(communicator) self.coroutines.append(publisher) ready_event = asyncio.Event() composite_publisher = CompositePublisher(self.loop, self.previous_communicator, consumers, ready_event, self.terminate_event, flow_id=self.id) self.ready_events[self.start_event_name].append(ready_event) logger.info("added composite") self.coroutines.append(composite_publisher) self.previous_communicator = None return self
def setUp(self): global gen_func self.loop = asyncio.get_event_loop() self.gen_func = gen_func self.consumer = Communicator() self.consumer.consume = AsyncMock(return_value=None)