async def test_add_two_fire_early(self): fake_clock = clock.FakeClock(time.time()) q = _DelayingType(fake_clock) first = "foo" second = "bar" third = "baz" await q.add_after(first, 1) await q.add_after(second, 0.05) await wait_for_waiting_queue_to_fill(q) self.assertEqual(len(q), 0) await fake_clock.step(0.06) await wait_for_added(q, 1) item, _ = await q.get() self.assertEqual(item, second) await q.add_after(third, 2) await fake_clock.step(1) await wait_for_added(q, 1) item, _ = await q.get() self.assertEqual(item, first) await fake_clock.step(2) await wait_for_added(q, 1) item, _ = await q.get() self.assertEqual(item, third)
async def test_deduping(self): fake_clock = clock.FakeClock(time.time()) q = _DelayingType(fake_clock) first = "foo" await q.add_after(first, 0.05) await wait_for_waiting_queue_to_fill(q) await q.add_after(first, 0.07) await wait_for_waiting_queue_to_fill(q) self.assertEqual(len(q), 0) await fake_clock.step(0.06) await wait_for_added(q, 1) item, _ = await q.get() await q.done(item) await fake_clock.step(0.02) self.assertEqual(len(q), 0) await q.add_after(first, 0.05) await q.add_after(first, 0.03) await wait_for_waiting_queue_to_fill(q) self.assertEqual(len(q), 0) await fake_clock.step(0.04) await wait_for_added(q, 1) item, _ = await q.get() await q.done(item) await fake_clock.step(0.02) self.assertEqual(len(q), 0)
async def test_simple_queue(self): fake_clock = clock.FakeClock(time.time()) q = _DelayingType(fake_clock) first = "foo" await q.add_after(first, 0.05) await wait_for_waiting_queue_to_fill(q) self.assertEqual(len(q), 0) await fake_clock.step(0.06) await wait_for_added(q, 1) item, _ = await q.get() await q.done(item) await fake_clock.step(10) async def condition(): if len(q): self.fail("added to queue") return False with self.assertRaises(wait.WaitTimeoutError): await wait.poll(0.001, 0.03, condition) self.assertEqual(len(q), 0)
async def test_listener_resync_periods(self): source = fake_controller_source.FakeControllerSource() await source.add(V1Pod(metadata=V1ObjectMeta(name="pod1"))) await source.add(V1Pod(metadata=V1ObjectMeta(name="pod2"))) informer = new_shared_informer(source, V1Pod, 1) clock_ = clock.FakeClock(time.time()) informer._clock = clock_ informer._processor._clock = clock_ listener1 = TestListener("listener1", 0, "pod1", "pod2") await informer.add_event_handler( listener1, resync_period=listener1._resync_period) listener2 = TestListener("listener2", 2, "pod1", "pod2") await informer.add_event_handler( listener2, resync_period=listener2._resync_period) listener3 = TestListener("listener3", 3, "pod1", "pod2") await informer.add_event_handler( listener3, resync_period=listener3._resync_period) listeners = [listener1, listener2, listener3] try: task = asyncio.ensure_future(informer.run()) for listener in listeners: self.assertTrue(await listener._ok()) for listener in listeners: listener._received_item_names = [] await clock_.step(2) self.assertTrue(await listener2._ok()) await asyncio.sleep(1) self.assertEqual(len(listener1._received_item_names), 0) self.assertEqual(len(listener3._received_item_names), 0) for listener in listeners: listener._received_item_names = [] await clock_.step(1) self.assertTrue(await listener3._ok()) await asyncio.sleep(1) self.assertEqual(len(listener1._received_item_names), 0) self.assertEqual(len(listener2._received_item_names), 0) finally: task.cancel() await asyncio.gather(task, return_exceptions=True)
async def test(self): limiter = default_rate_limiters.ItemExponentialFailureRateLimiter( 0.001, 1) fake_clock = clock.FakeClock(time.time()) with unittest.mock.patch.object(delaying_queue._DelayingType, "_waiting_loop", _null_loop): queue = _RateLimitingType(limiter, fake_clock) await queue.add_rate_limited("one") wait_entry = await queue._waiting_for_add_queue.get() self.assertAlmostEqual(wait_entry.ready_at - fake_clock.now(), 0.001, places=6) await queue.add_rate_limited("one") wait_entry = await queue._waiting_for_add_queue.get() self.assertAlmostEqual(wait_entry.ready_at - fake_clock.now(), 0.002, places=6) self.assertEqual(queue.num_requeues("one"), 2) await queue.add_rate_limited("two") wait_entry = await queue._waiting_for_add_queue.get() self.assertAlmostEqual(wait_entry.ready_at - fake_clock.now(), 0.001, places=6) await queue.add_rate_limited("two") wait_entry = await queue._waiting_for_add_queue.get() self.assertAlmostEqual(wait_entry.ready_at - fake_clock.now(), 0.002, places=6) queue.forget("one") self.assertEqual(queue.num_requeues("one"), 0) await queue.add_rate_limited("one") wait_entry = await queue._waiting_for_add_queue.get() self.assertAlmostEqual(wait_entry.ready_at - fake_clock.now(), 0.001, places=6)
async def test_copy_shifting(self): fake_clock = clock.FakeClock(time.time()) q = _DelayingType(fake_clock) first = "foo" second = "bar" third = "baz" await q.add_after(first, 1) await q.add_after(second, 0.5) await q.add_after(third, 0.25) await wait_for_waiting_queue_to_fill(q) self.assertEqual(len(q), 0) await fake_clock.step(2) await wait_for_added(q, 3) actual_first, _ = await q.get() self.assertEqual(actual_first, third) actual_second, _ = await q.get() self.assertEqual(actual_second, second) actual_third, _ = await q.get() self.assertEqual(actual_third, first)
async def test_resync_check_period(self): source = fake_controller_source.FakeControllerSource() informer = new_shared_informer(source, V1Pod, 12 * 3600) clock_ = clock.FakeClock(time.time()) informer._clock = clock_ informer._processor._clock = clock_ listener1 = TestListener("listener1", 0) await informer.add_event_handler( listener1, resync_period=listener1._resync_period) self.assertEqual(informer._resync_check_period, 12 * 3600) self.assertEqual(informer._processor._listeners[0]._resync_period, 0) listener2 = TestListener("listener2", 60) await informer.add_event_handler( listener2, resync_period=listener2._resync_period) self.assertEqual(informer._resync_check_period, 60) self.assertEqual(informer._processor._listeners[0]._resync_period, 0) self.assertEqual(informer._processor._listeners[1]._resync_period, 60) listener3 = TestListener("listener3", 55) await informer.add_event_handler( listener3, resync_period=listener3._resync_period) self.assertEqual(informer._resync_check_period, 55) self.assertEqual(informer._processor._listeners[0]._resync_period, 0) self.assertEqual(informer._processor._listeners[1]._resync_period, 60) self.assertEqual(informer._processor._listeners[2]._resync_period, 55) listener4 = TestListener("listener4", 5) await informer.add_event_handler( listener4, resync_period=listener4._resync_period) self.assertEqual(informer._resync_check_period, 5) self.assertEqual(informer._processor._listeners[0]._resync_period, 0) self.assertEqual(informer._processor._listeners[1]._resync_period, 60) self.assertEqual(informer._processor._listeners[2]._resync_period, 55) self.assertEqual(informer._processor._listeners[3]._resync_period, 5)