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
times = [0.05, 2] def next_time(s): return times.pop(0) async def doit(): start = time.time() with mock.patch.object(RetryOptions, "next_time", property(next_time)): res = await V.waiter return res, time.time() - start t = V.loop.create_task(doit()) await asyncio.sleep(0.005) assert V.writer.mock_calls == [mock.call()] result.add_ack() await asyncio.sleep(0.08) assert V.writer.mock_calls == [mock.call()] res = mock.Mock(name="res") result.set_result(res) r, took = await t assert took < 0.15 assert r is res assert V.writer.mock_calls == [mock.call()] async it "two writings with partial write for first writing", V: request = mock.Mock(name="request", ack_required=True, res_required=True) request.Meta.multi = None