def test_join_memory_and_disk(self): q = OrderedCachedQueue(maxsize=2) for x in range(10): q.put(create_simple_fuzzable_request(x)) def queue_get_after_delay(queue): time.sleep(1) for x in range(2): queue.get() queue.task_done() time.sleep(1) for x in range(8): queue.get() queue.task_done() t = threading.Thread(target=queue_get_after_delay, args=(q,)) t.start() start = time.time() # This should take 3 seconds q.join() spent = time.time() - start self.assertGreater(spent, 2)
def test_put_same_fuzzable_request_twice(self): q = OrderedCachedQueue(maxsize=2) q.put(create_simple_fuzzable_request(1)) q.put(create_simple_fuzzable_request(1)) self.assertEqual(q.get(), q.get())
def test_add_exceed_memory(self): q = OrderedCachedQueue(maxsize=2) # These two go to the in memory queue q.put(create_simple_fuzzable_request(1)) q.put(create_simple_fuzzable_request(2)) self.assertEqual(q.qsize(), 2) self.assertEqual(len(q.memory), 2) # This one goes to the disk queue q.put(create_simple_fuzzable_request(3)) self.assertEqual(q.qsize(), 3) self.assertEqual(len(q.memory), 2) self.assertEqual(len(q.disk), 1) # Get all self.assertEqual(read_fuzzable_request_parameter(q.get()), 1) self.assertEqual(len(q.memory), 1) self.assertEqual(len(q.disk), 1) self.assertEqual(read_fuzzable_request_parameter(q.get()), 2) self.assertEqual(len(q.memory), 0) self.assertEqual(len(q.disk), 1) self.assertEqual(read_fuzzable_request_parameter(q.get()), 3) self.assertEqual(len(q.memory), 0) self.assertEqual(len(q.disk), 0) self.assertEqual(q.qsize(), 0)
def test_exceptions_no_fail_sync_pointer(self): q = OrderedCachedQueue(maxsize=2) q.put(create_simple_fuzzable_request(1)) q.get() self.assertRaises(Exception, q.get, block=False) q.put(create_simple_fuzzable_request(1)) self.assertEquals(read_fuzzable_request_parameter(q.get()), 1)
def test_put_none_after_fuzzable_request(self): q = OrderedCachedQueue(maxsize=2) q.put(create_simple_fuzzable_request(1)) q.put(None) # Reads the fuzzable request q.get() # Reads the None self.assertIsNone(q.get())
def test_read_in_order(self): q = OrderedCachedQueue(maxsize=2) hash_list = [] for i in xrange(5): fr = create_simple_fuzzable_request(i) hash_list.append(fr.get_hash()) q.put(fr) unordered_hash_list = hash_list[:] hash_list.sort() self.assertNotEqual(unordered_hash_list, hash_list) for i in xrange(4): fr = q.get() self.assertEqual(fr.get_hash(), hash_list[i])
def test_join_memory(self): q = OrderedCachedQueue(maxsize=2) q.put(create_simple_fuzzable_request(1)) def queue_get_after_delay(queue): time.sleep(1) queue.get() queue.task_done() t = threading.Thread(target=queue_get_after_delay, args=(q,)) t.start() start = time.time() # This should take 1 second q.join() spent = time.time() - start self.assertGreater(spent, 1)
def test_simple_rpm_speed(self): q = OrderedCachedQueue() self.assertEqual(0.0, q.get_input_rpm()) self.assertEqual(0.0, q.get_output_rpm()) for i in xrange(4): q.put(create_simple_fuzzable_request(i)) # 20 RPM time.sleep(3) self.assertEqual(q.qsize(), 4) self.assertGreater(q.get_input_rpm(), 19) self.assertLess(q.get_input_rpm(), 20) for i in xrange(4): q.get() # 60 RPM time.sleep(1) self.assertGreater(q.get_output_rpm(), 59) self.assertLess(q.get_output_rpm(), 60) self.assertEqual(q.qsize(), 0)
def test_prefer_memory_over_disk(self): q = OrderedCachedQueue(maxsize=2) # These two go to the in memory queue q.put(create_simple_fuzzable_request(1)) q.put(create_simple_fuzzable_request(2)) # This one goes to the disk queue q.put(create_simple_fuzzable_request(3)) # Read one from memory q.get() self.assertEqual(len(q.memory), 1) self.assertEqual(len(q.disk), 1) # Write one to memory q.put(create_simple_fuzzable_request(3)) self.assertEqual(len(q.memory), 2) self.assertEqual(len(q.disk), 1)