def setUp(self): self.queue = LifoQueueWithEvict(10) for i in range(10): self.queue.put((EvictObj(i), current_time_millis())) time.sleep(0.1)
class TestLifoQueueWithEvict(object): def setUp(self): self.queue = LifoQueueWithEvict(10) for i in range(10): self.queue.put((EvictObj(i), current_time_millis())) time.sleep(0.1) def test_evict_none(self): time.sleep(0.1) self.queue.evict(EVICT_TIME, clean_up) nt.assert_equal(10, self.queue.qsize()) def test_evict_5(self): time.sleep(0.95) self.queue.evict(EVICT_TIME) nt.assert_equal(5, self.queue.qsize()) def test_evict_all(self): time.sleep(2) self.queue.evict(EVICT_TIME, clean_up) nt.assert_equal(0, self.queue.qsize()) def evict_thread(self): while True: time.sleep(1) self.queue.evict(EVICT_TIME, clean_up) def get_thread(self): while True: time.sleep(random.randint(200, 400) * 0.001) (obj, t) = self.queue.get() print obj.i, "--",t def test_evict_thread(self): thread = threading.Thread(target=self.evict_thread) thread.setDaemon(True) thread.start() thread = threading.Thread(target=self.get_thread) thread.setDaemon(True) thread.start() for i in range(10, 30): time.sleep(random.randint(100, 200) * 0.001) self.queue.put((EvictObj(i), current_time_millis())) print i, "is put into queue" time.sleep(3) # to ensure all object is evicted. # self.queue.join() # test to see if join function on queue still work nt.assert_equal(0, self.queue.qsize())