def test_publish_callback_exc(self, metrics): fut = Future() fut.set_exception(Exception()) published_callback("topic", time.time(), fut) metrics["PUBLISHED_MESSAGES"].labels.assert_called_with( stream_id="topic", partition=-1, error="Exception") metrics["PUBLISHED_MESSAGES"].labels().inc()
def make_awaitable(result: Any) -> Awaitable[Any]: """ Makes an awaitable, suitable for mocking an `async` function. This uses Futures as they can be awaited multiple times so can be returned to multiple callers. """ future = Future() future.set_result(result) return future
def add_done_callback(self, fn, *, context=None): def BeforeFn(*args, **kargs): try: with GearsSession(self.gearsSession): fn(*args, **kargs) except Exception as e: log(e) Future.add_done_callback(self, BeforeFn, context=context)
def done(task: Future) -> None: try: if fut.done(): # pragma: nocover return if task.exception(): fut.set_exception(task.exception()) else: # pragma: nocover fut.set_result(None) finally: self._got_result = True
def __init__( self, result # type: ResultPrecursor ): orig_result = next(iter(result.orig_result.values())) MutationResult.__init__(self, orig_result.cas, SDK2MutationToken(orig_result)) Future.__init__(self) try: self.set_result(orig_result) except: pass
async def send(data): if data['type'] == 'http.response.start': status_code = data['status'] lib.evhttp_send_reply_start(req, status_code, http_messages.get(status_code, b'Unknown')) if data['type'] == 'http.response.body': body = data['body'] buf = lib.evbuffer_new() lib.evbuffer_add_printf(buf, body) lib.evhttp_send_reply_chunk(req, buf) lib.evhttp_send_reply_end(req) lib.evbuffer_free(buf) future = Future() future.set_result(None) return future
async def main(): shutdown_flag = Future() clients = set() lock = threading.Lock() get_event_loop().run_in_executor(None, run_consumer, shutdown_flag, clients, lock) print("Starting Websocket Server.") try: async with websockets.serve(partial(handle_connection, clients, lock), "localhost", 8080): await Future() finally: shutdown_flag.set_result(True)
async def start(self): server = None self.protocol = Future(loop=self.loop) loop = asyncio.get_event_loop() # Creating the server if self.pid: factory = lambda: Exchanger(self) server = await loop.create_server(factory, port=self.port) logging.debug(f'Listening on port {self.port}') # Setting up a connection with all those who have for peer in self.parties[self.pid + 1:]: while True: try: factory = lambda: Exchanger(self, peer.pid) await loop.create_connection(factory, peer.host, peer.port) logging.debug(f'Connected to {peer.host}:{peer.port}') break except asyncio.CancelledError: raise except Exception as exc: logging.debug(exc) time.sleep(0.1) # Waiting until all the parties are connected await self.protocol logging.debug('Connected') # Server is closed immediately after the connections are set up if server: server.close()
def __init__(self, name, module_path, funcs): super().__init__(funcs) self.temp = {} self.name = name self.module_path = module_path self.config = {} self.future = Future()
def __await__(self): res = None if isInAtomicBlock(): raise Exception("await is not allow inside atomic block") else: res = yield from Future.__await__(self) return res
def group(self, futures, callback=None, name=None): group_future = Future() results_holder = {} def group_callback(): if callable(callback): callback(results_holder) group_future.set_result(results_holder) def future_callback(name, future): results_holder[name] = future.result() async_group = AsyncGroup(self.finish_group.add( self.check_finished(group_callback)), name=name) for name, future in futures.items(): if future.done(): future_callback(name, future) else: self.add_future( future, async_group.add(partial(future_callback, name))) async_group.try_finish_async() return group_future
def __init__(self, *args, **kwargs): super().__init__(**kwargs) self._shutdown_status = _ShutdownStatus.Unset try: self._ready = Future() except RuntimeError: # No event loop running, use concurrent future self._ready = CFuture()
def __init__( self, sdk2_result, # type: SDK2Result expiry=None, # type: Seconds **kwargs): key, value = next(iter(sdk2_result.items()), (None, None)) if isinstance( sdk2_result, AsyncResult) else (sdk2_result.key, sdk2_result) super(SDK2ResultWrapped, self).__init__(key, value.cas, value.rc, expiry, **kwargs) try: Future.__init__(self) self.set_result(ValueWrapper(sdk2_result)) except: pass self._original = sdk2_result
def test_publish_callback(self, metrics): fut = Future() fut.set_result(record_factory()) published_callback("topic", time.time() - 1, fut) metrics["PUBLISHED_MESSAGES"].labels.assert_called_with( stream_id="topic", partition=0, error="none" ) metrics["PUBLISHED_MESSAGES"].labels().inc() metrics["PRODUCER_TOPIC_OFFSET"].labels.assert_called_with(stream_id="topic", partition=0) metrics["PRODUCER_TOPIC_OFFSET"].labels().set.assert_called_with(0) metrics["PUBLISHED_MESSAGES_TIME"].labels.assert_called_with(stream_id="topic") assert metrics["PUBLISHED_MESSAGES_TIME"].labels().observe.mock_calls[0].args[ 0 ] == pytest.approx(1, 0.1)
def published_callback(topic: str, start_time: float, fut: Future) -> None: # Record the metrics finish_time = time.time() exception = fut.exception() if exception: error = str(exception.__class__.__name__) PUBLISHED_MESSAGES.labels(stream_id=topic, partition=-1, error=error).inc() else: metadata = fut.result() PUBLISHED_MESSAGES.labels(stream_id=topic, partition=metadata.partition, error=NOERROR).inc() PRODUCER_TOPIC_OFFSET.labels(stream_id=topic, partition=metadata.partition).set( metadata.offset) PUBLISHED_MESSAGES_TIME.labels(stream_id=topic).observe(finish_time - start_time)
async def test_threadpool_await_in_thread(self): """Test that attempting to await in a thread results in a RuntimeError.""" future = Future() with pytest.raises(RuntimeError) as exc: async with threadpool(): await future assert str(exc.value) == 'attempted to "await" in a worker thread'
def _execute_http_client_method(self, host, uri, client_method, waited, callback): if waited and (self.is_finished() or self.finish_group.is_finished()): handler_logger.info( 'attempted to make waited http request to %s %s in finished handler, ignoring', host, uri ) future = Future() future.set_exception(AbortAsyncGroup()) return future if waited and callable(callback): callback = self.check_finished(callback) future = client_method(callback) if waited: self.finish_group.add_future(future) return future
def receive(self, var): if var.pc in self.protocol.buffer: payload = self.protocol.buffer[var.pc] data = pickle.loads(payload) if data is not None: var.set_result(data) else: logging.debug('not a variable') else: fut = Future(loop=var.runtime.loop) self.protocol.buffer[var.pc] = fut def when_finished(_fut): d = _fut.result() d = pickle.loads(d) if d is not None and not var.done(): var.set_result(d) fut.add_done_callback(when_finished)
async def m_search(self, address: str, timeout: float, datagrams: typing.List[OrderedDict]) -> SSDPDatagram: fut: Future = Future() packets: typing.List[SSDPDatagram] = [] for datagram in datagrams: packet = SSDPDatagram(SSDPDatagram._M_SEARCH, datagram) assert packet.st is not None # h = asyncio.get_running_loop().call_later(timeout, fut.cancel) self._pending_searches.append((address, packet.st, fut)) packets.append(packet) self.send_many_m_searches(address, packets), return await fut
async def test_publish_resource(self, context): """Test that a resource is properly published in the context and listeners are notified.""" future = Future() context.resource_published.connect(future.set_result) context.publish_resource(6, 'foo', 'foo.bar', types=(int, float)) value = await context.request_resource(int, 'foo') assert value == 6 event = await future assert event.resource.types == ('int', 'float') assert event.resource.alias == 'foo'
async def _http_handler(self, http_request: BaseRequest) -> Response: """ Verify the request then send the request to Agent as an envelope. :param request: the request object :return: a tuple of response code and response description """ request = await Request.create(http_request) if self._in_queue is None: # pragma: nocover raise ValueError("Channel not connected!") is_valid_request = self.api_spec.verify(request) if not is_valid_request: self.logger.warning(f"request is not valid: {request}") return Response(status=NOT_FOUND, reason="Request Not Found") try: # turn request into envelope envelope = request.to_envelope_and_set_id( self.connection_id, self.address, dialogues=self._dialogues, ) self.pending_requests[request.id] = Future() # send the envelope to the agent's inbox (via self.in_queue) await self._in_queue.put(envelope) # wait for response envelope within given timeout window (self.timeout_window) to appear in dispatch_ready_envelopes response_message = await asyncio.wait_for( self.pending_requests[request.id], timeout=self.RESPONSE_TIMEOUT, ) return Response.from_message(response_message) except asyncio.TimeoutError: return Response(status=REQUEST_TIMEOUT, reason="Request Timeout") except FuturesCancelledError: return Response( # pragma: nocover status=SERVER_ERROR, reason="Server terminated unexpectedly.") except BaseException: # pragma: nocover # pylint: disable=broad-except self.logger.exception("Error during handling incoming request") return Response(status=SERVER_ERROR, reason="Server Error", text=format_exc()) finally: if request.is_id_set: self.pending_requests.pop(request.id, None)
async def test_remove_resource(self, context): """Test that resources can be removed and that the listeners are notified.""" future = Future() context.resource_removed.connect(future.set_result) resource = context.publish_resource(4) context.remove_resource(resource) event = await future assert event.resource.types == ('int',) with pytest.raises(ResourceNotFound): await context.request_resource(int, timeout=0)
def _accumulate_distributions(future: Future): """ Saves created distribution parameters to global variable. """ global _name_dists task_data = future.result() # type: _TaskData orbital_elems = task_data.orbiral_elems names = task_data.names variations_matrix = task_data.variations_matrix asteroid_variations = variations_matrix[0] dists = _make_dists(orbital_elems, names, asteroid_variations) _name_dists.append((task_data.name, dists))
async def test_async_mock(): token = 'my-test-token-234' action_id = 1030573508 aut = Automator(token) # stash check = aut._Automator__check_action_status aut._Automator__check_action_status = MagicMock(return_value=Future()) aut._Automator__check_action_status.return_value.set_result('in-progress') result = await aut._Automator__check_action_status(action_id) assert result == 'in-progress' # restore aut._Automator__check_action_status = check
async def _wait_for_data(self, func_name): """Wait until feed_data() or feed_eof() is called.""" # StreamReader uses a future to link the protocol feed_data() method # to a read coroutine. Running two read coroutines at the same time # would have an unexpected behaviour. It would not possible to know # which coroutine would get the next data. if self._waiter is not None: raise RuntimeError('%s() called while another coroutine is ' 'already waiting for incoming data' % func_name) self._waiter = Future(loop=self._loop) try: await self._waiter finally: self._waiter = None
def listen(self, listenPort, applicationProtocolFactory): self._connectionId += 1 logger.debug("Requesting listenting socket on port {} from vnic (connection ID {})".format(listenPort, self._connectionId)) logger.info("Listen in {}. Has transport {}. For port {}".format(self, self.transport, listenPort)) callbackAddr, callbackPort = self._callbackService.location() openSocketPacket = VNICSocketOpenPacket(ConnectionId=self._connectionId, callbackAddress=callbackAddr, callbackPort=callbackPort) openSocketPacket.listenData = openSocketPacket.SocketListenData(sourcePort = listenPort) self.transport.write(openSocketPacket.__serialize__()) future = Future() self._connections[self._connectionId] = applicationProtocolFactory self._futures[self._connectionId] = ("listen", future) return future
def connect(self, destination, destinationPort, applicationProtocolFactory): self._connectionId += 1 logger.debug("Requesting connect to {}:{} from vnic (connection ID {})".format(destination, destinationPort, self._connectionId)) callbackAddr, callbackPort = self._callbackService.location() openSocketPacket = VNICSocketOpenPacket(ConnectionId = self._connectionId, callbackAddress=callbackAddr, callbackPort=callbackPort) openSocketPacket.connectData = openSocketPacket.SocketConnectData(destination=destination, destinationPort=destinationPort) packetBytes = openSocketPacket.__serialize__() self.transport.write(packetBytes) future = Future() self._connections[self._connectionId] = applicationProtocolFactory self._futures[self._connectionId] = ("connect", future) return future
async def wait(self, state_or_states: Union[Any, Sequence[Any]]) -> Tuple[Any, Any]: """Wait state to be set. :param state_or_states: state or list of states. :return: tuple of previous state and new state. """ states = ensure_list(state_or_states) if self._state in states: return (None, self._state) watcher: Future = Future() watcher._states = states # type: ignore # pylint: disable=protected-access self._watchers.add(watcher) try: return await watcher finally: self._remove_watcher(watcher)
async def test_wait_till_complete_should_call_check_action_status_each_5_secs( ): token = 'my-test-token-234' action_id = 1030573508 aut = Automator(token) # stash check = aut._Automator__check_action_status aut._Automator__check_action_status = MagicMock(return_value=Future()) aut._Automator__check_action_status.return_value.set_result('in-progress') try: await asyncio.wait_for( aut._Automator__wait_till_action_complete(action_id), timeout=14.0) except asyncio.TimeoutError: # timeout reached # should call 3 times in 14 sec (0s, 5s, 10s) assert aut._Automator__check_action_status.call_count == 3 finally: # restore from stash aut._Automator__check_action_status = check
def _set_result_unless_cancelled(fut: Future, result: Any) -> None: """Set the result only if the Future was not cancelled.""" if fut.cancelled(): return fut.set_result(result)
def actor_cancelled(self, actor: Actor, future: Future): logger.info("Actor is done! Cancellation or Finished??") if future.cancelled(): logger.info("Future was cancelled everything is cools")
def __init__(self, *, loop=None, gearsSession=None): Future.__init__(self, loop=loop) self.gearsSession = gearsSession