def wait_on_result( ack_required, res_required, retry_gaps, now, last_ack_received, last_res_received, results, num_results, ): V.request.ack_required = ack_required V.request.res_required = res_required retry_gaps = Gaps( **{ "gap_between_ack_and_res": 10, "gap_between_results": 10, "timeouts": [(10, 10)], **retry_gaps, } ).empty_normalise() with mock.patch.object(Result, "num_results", num_results): result = Result(V.request, False, retry_gaps) result.results = results result.last_ack_received = last_ack_received result.last_res_received = last_res_received t = mock.Mock(name="time", return_value=now) with mock.patch("time.time", t): return result.wait_for_result()
def wait_on_result( ack_required, res_required, retry_options, now, last_ack_received, last_res_received, results, num_results, ): V.request.ack_required = ack_required V.request.res_required = res_required retry_options = mock.NonCallableMock( name="retry_options", spec=retry_options.keys(), **retry_options ) with mock.patch.object(Result, "num_results", num_results): result = Result(V.request, False, retry_options) result.results = results result.last_ack_received = last_ack_received result.last_res_received = last_res_received t = mock.Mock(name="time", return_value=now) with mock.patch("time.time", t): return result.wait_for_result()
def add_ack(res_required, did_broadcast, now, already_done=False): V.request.res_required = res_required t = mock.Mock(name="t", return_value=now) existing_result = mock.Mock(name="existing_result") existing_error = ValueError("Nope") schedule_finisher = mock.Mock(name="shcedule_finisher") with mock.patch("time.time", t): result = Result(V.request, did_broadcast, RetryOptions()) if already_done is not False: if already_done is True: result.set_result(existing_result) elif already_done == "exception": result.set_exception(existing_error) elif already_done == "cancelled": result.cancel() else: raise Exception(f"Unexpected already_done value, {already_done}") async def check_result(expect=None): if expect is not None: assert (await result) == expect elif already_done is False: assert not result.done() elif already_done is True: assert (await result) == existing_result elif already_done == "exception": with assertRaises(ValueError, "Nope"): await result elif already_done == "cancelled": assert result.cancelled() with mock.patch.object(result, "schedule_finisher", schedule_finisher): result.add_ack() return result, check_result, schedule_finisher
async def make_messages(self, serial, msgs): for i, msg in enumerate(msgs): msg.update({ "source": self.afr.source, "sequence": self.afr.seq(serial) }) writer = self.writers[serial] t = await writer.t() result = None if i == 0: msg.ack_required = True retry_gaps = self.afr.retry_gaps(msg, t) result = Result(msg, False, retry_gaps) self.afr.receiver.register(msg, result, msg) yield partial(writer.write, msg), result
async def make_messages(self, msgs): ms = [] group = {} for msg in msgs: serial = msg.serial transport, t = await self.get_transport(serial) msg.update( dict(source=self.sender.source, sequence=self.sender.seq(serial))) if serial not in group: msg.ack_required = True retry_options = self.sender.retry_options_for(msg, t) result = Result(msg, False, retry_options) self.sender.receiver.register(msg, result, msg) group[serial] = (time.time(), result) ms.insert(0, (transport, t, msg)) for transport, t, msg in ms: yield transport, t, msg
def register(self): result = Result(self.original, self.did_broadcast, self.retry_gaps) if not result.done(): result.add_done_callback(hp.silent_reporter) self.receiver.register(self.clone, result, self.original) return result
def result(s): return Result(s.request, False, RetryOptions())
@hp.memoized_property def request(s): request = mock.NonCallableMock( name="request", spec=["Meta", "res_required", "ack_required"] ) request.Meta = s.Meta request.ack_required = True request.res_required = True return request return V() describe "Result": async it "is a future", V: result = Result(V.request, False, RetryOptions()) assert isinstance(result, asyncio.Future) assert not result.done() result.set_result([]) assert (await result) == [] async it "sets options", V: did_broadcast = mock.Mock(name="did_broadcast") retry_options = RetryOptions() result = Result(V.request, did_broadcast, retry_options) assert result.request is V.request assert result.did_broadcast is did_broadcast assert result.retry_options is retry_options assert result.results == []
assert getattr(V.waiter, "__writings_cb", None) == cb assert V.waiter._writings_cb == cb del V.waiter._writings_cb assert not hasattr(V.waiter, "__writings_cb") async it "is cancelled if the stop fut gets a result", V: V.stop_fut.set_result(None) with assertRaises(asyncio.CancelledError): await V.waiter describe "without so many mocks": async it "one writings", V: request = mock.Mock(name="request", ack_required=False, res_required=True) request.Meta.multi = None result = Result(request, False, V.retry_options) async def writer(): return result V.writer.side_effect = writer async def doit(): start = time.time() with mock.patch.object(RetryOptions, "next_time", 2): res = await V.waiter return res, time.time() - start t = V.loop.create_task(doit()) await asyncio.sleep(0.01) results = mock.Mock(name="results")
def result(s): return Result(s.request, False, s.gaps)
def add_result(expected_num, results, now, already_done=False): t = mock.Mock(name="t", return_value=now) existing_result = mock.Mock(name="existing_result") existing_error = ValueError("Nope") schedule_finisher = mock.Mock(name="shcedule_finisher") added_res = mock.Mock(name="res") with mock.patch("time.time", t): with mock.patch.object(Result, "num_results", expected_num): result = Result(V.request, False, V.gaps) result.results = list(results) if already_done is not False: if already_done is True: result.set_result(existing_result) elif already_done == "exception": result.set_exception(existing_error) elif already_done == "cancelled": result.cancel() else: raise Exception(f"Unexpected already_done value, {already_done}") async def check_result(expect=None): if expect is not None: assert (await result) == expect elif already_done is False: assert not result.done() elif already_done is True: assert (await result) == existing_result elif already_done == "exception": with assertRaises(ValueError, "Nope"): await result elif already_done == "cancelled": assert result.cancelled() with mock.patch.object(result, "schedule_finisher", schedule_finisher): result.add_result(added_res) return result, check_result, added_res, schedule_finisher