def test_put(self): queue = Queue(maxsize=1) self.assertEqual(queue.qsize(), 0) self.assertEqual(queue.full(), False) queue.put("1.txt", {"content": "file content"}, timeout=10) self.assertEqual(queue.qsize(), 1) self.assertEqual(queue.full(), True) with self.assertRaises(Full): queue.put("2.txt", {"content": "file content"}, timeout=1) queue = Queue(maxsize=1) with self.assertRaises(ValueError): queue.put("1.txt", {"content": "file content"}, timeout=-4) queue = Queue(maxsize=2) self.assertEqual(queue.qsize(), 0) self.assertEqual(queue.full(), False) queue.put("1.txt", {"content": "file content"}, timeout=1) self.assertEqual(queue.qsize(), 1) self.assertEqual(queue.full(), False) with self.assertRaises(RepeatedMessage): queue.put("1.txt", {"content": "file content"}, timeout=1) queue = Queue(maxsize=0) self.assertEqual(queue.qsize(), 0) self.assertEqual(queue.full(), False) queue.put("1.txt", {"content": "file content"}, timeout=1) self.assertEqual(queue.qsize(), 1) self.assertEqual(queue.full(), False)
def test_print_empty_queue(capsys): queue = Queue() queue.print() expected = "[]\n" output = capsys.readouterr().out assert output == expected
def test_max(self): queue = Queue(1) queue.put("abc") with pytest.raises(Exception) as execinfo: queue.put("TEST") assert str(execinfo.value) == "QueueFullException"
def test(): q = Queue() assert q.is_empty() is True q.enqueue(4) assert q.items == [4] q.enqueue("dog") assert q.items == ["dog", 4] q.enqueue(True) assert q.items == [True, "dog", 4] assert q.size() == 3 assert q.is_empty() is False q.enqueue(8.4) assert q.items == [8.4, True, "dog", 4] assert q.dequeue() == 4 assert q.dequeue() == "dog" assert q.size() == 2
def test_dequeue_with_full_queue(): queue = Queue([2, 4, 1, 4020]) expected = 2 output = queue.dequeue() assert output == expected assert queue.queue[0] == 4
def test_peek_queue_with_elements(): queue = Queue([1, 2, "Dog"]) expected = 1 output = queue.peek() assert output == expected assert queue.queue[0] == expected
def test_delete(self): queue = Queue(maxsize=1) self.assertEqual(queue.qsize(), 0) self.assertEqual(queue.full(), False) queue.put("1.txt", {"content": "file content"}, timeout=10) self.assertEqual(queue.qsize(), 1) self.assertEqual(queue.full(), True) queue_message = queue.get(timeout=1, acquire_timeout=10) self.assertEqual(queue.qsize(), 1) self.assertEqual(queue.full(), True) self.assertEqual(queue_message.content, {"content": "file content"}) self.assertEqual(queue_message.id, "1.txt") queue.delete(queue_message.id) self.assertEqual(queue.qsize(), 0) self.assertEqual(queue.full(), False) queue.put("1.txt", {"content": "file content"}, timeout=10) queue_message = queue.get(timeout=1, acquire_timeout=10) self.assertEqual(queue.qsize(), 1) self.assertEqual(queue.full(), True) with self.assertRaises(DeleteAttemptToUnknownMessage): queue.delete("random_file.txt")
def test_q_size_3(): from src.queue import Queue test_q = Queue() test_q.enqueue(2) test_q.enqueue("bob") test_q.dequeue() assert test_q.size == 1
def test_acquire_timeout(self): queue = Queue(maxsize=1) self.assertEqual(queue.qsize(), 0) self.assertEqual(queue.full(), False) queue.put("1.txt", {"content": "file content"}, timeout=10) self.assertEqual(queue.qsize(), 1) self.assertEqual(queue.full(), True) queue_message = queue.get(timeout=1, acquire_timeout=0.1) self.assertEqual(queue.qsize(), 1) self.assertEqual(queue.full(), True) self.assertEqual(queue_message.content, {"content": "file content"}) self.assertEqual(queue_message.id, "1.txt") time.sleep(0.5) queue_message = queue.get(timeout=1, acquire_timeout=10) self.assertEqual(queue.qsize(), 1) self.assertEqual(queue.full(), True) self.assertEqual(queue_message.content, {"content": "file content"}) self.assertEqual(queue_message.id, "1.txt") with self.assertRaises(Empty): queue_message = queue.get(timeout=1, acquire_timeout=10)
def test_q_deq(): from src.queue import Queue test_q = Queue() val = 6 test_q.enqueue(val) test_q.enqueue("bob") assert test_q.dequeue() == 6
def levelorderWalk(self): if self.root == None: print('empty tree') else: q = Queue() q.enqueue((self.root, 0)) # (node, depth) depth_hash = {} depth_hash[0] = [(self.root.val, None)] #(node, parent) while not q.isempty(): node_tuple = q.dequeue() n = node_tuple[0] # node object d = node_tuple[1] # depth value if not n.left is None: q.enqueue((n.left, d + 1)) if d+1 not in depth_hash: depth_hash[d+1] = [(n.left.val, n.val)] else: depth_hash[d+1].append((n.left.val, n.val)) if not n.right is None: q.enqueue((n.right, d + 1)) if d+1 not in depth_hash: depth_hash[d+1] = [(n.right.val, n.val)] else: depth_hash[d+1].append((n.right.val, n.val)) for k in depth_hash.keys(): print('Level '+ str(k) + ': ', end='') print(depth_hash[k])
def test_q_peek_3(): from src.queue import Queue test_q = Queue() val = 6 test_q.enqueue(val) test_q.enqueue("bob") assert test_q.peek() == 6
def test_lock_usage(self): global processed_messages queue = Queue(maxsize=1) feeder_threads = [] consumer_threads = [] for i in range(25): feeder_threads.append( MessageFeeder(queue, f"{i}.txt", {"content": "file content"}, timeout=60)) feeder_threads[-1].start() for i in range(25): consumer_threads.append( MessageConsumer(queue, acquire_timeout=10, timeout=90)) consumer_threads[-1].start() consumer_threads[-1].done = True for t in consumer_threads: t: MessageConsumer t.join() self.assertTrue(t.result) self.assertEqual(len(processed_messages), 25) for i in range(25): self.assertIn(f"{i}.txt", processed_messages) for t in feeder_threads: t: MessageFeeder t.join() self.assertTrue(t.result)
def test_is_empty(self): test = Queue() test.enqueue(24) test.enqueue("123") test.enqueue(14.78) message = "test_is_empty is FAIL" self.assertFalse(test.is_empty(), message)
def test_dequeue(self): queue = Queue() queue.enqueue(55) res = queue.dequeue() res_1 = 55 message = "test_dequeue is FAIL" self.assertEqual(res, res_1, message)
def test_dequeue_with_single_element_queue(): queue = Queue([2]) expected = 2 output = queue.dequeue() assert output == expected assert len(queue.queue) == 0
def test_print_queue_with_elements(capsys): queue = Queue([1, 2, 3, 4, 5]) queue.print() expected = "[1, 2, 3, 4, 5]\n" output = capsys.readouterr().out assert output == expected
def test_q_deq_2(): from src.queue import Queue test_q = Queue() val = 6 test_q.enqueue(val) test_q.enqueue("bob") peeked = test_q.peek() assert test_q.dequeue() == peeked
def test_enqueue_empty_queue(): queue = Queue() element = 230 queue.enqueue(element) expected = [230] output = queue.queue assert output == expected
def test_peek(): """Test peek.""" q = Queue() q.enqueue(3) q.enqueue(5) q.enqueue(7) assert q.peek() == 3 assert q.peek() == 3 assert q.peek() == 3
def test_enqueue_dequeue_big(self): queue = Queue() for i in range(10000): queue.enqueue(i + 1) for i in range(10000): queue.enqueue(queue.dequeue()) self.assertEqual(10000, queue.size())
def test_enqueue_with_multiple_elements(): queue = Queue() queue.enqueue([1, 2, 3]) queue.enqueue("Horse") queue.enqueue(9.321) expected = [[1, 2, 3], "Horse", 9.321] output = queue.queue assert output == expected
def test_enqueue_dequeue(self): queue = Queue() values = [10, 20, 30, 40, 50] for value in values: queue.enqueue(value) self.assertEqual(len(values), queue.size()) for value in values: self.assertEqual(value, queue.dequeue()) self.assertEqual(0, queue.size())
def test_dequeue(): """Test dequeue.""" q = Queue() q.enqueue(3) q.enqueue(5) q.enqueue(7) assert q.dequeue() == 3 assert q.dequeue() == 5 assert q.dequeue() == 7 assert q.dequeue() is None
def test_enqueue(self): q = Queue() q.enqueue(1) self.assertEqual(q.head['value'], q.tail['value'], 'tail and head are the same') q.enqueue(2) self.assertEqual(q.head['value'], 2, 'head is now the new value') self.assertIsNone(q.head['prev'], 'head is not linked to the next node') self.assertEqual(q.tail['value'], 1, 'first value is now tail') self.assertEqual(q.tail['prev']['value'], 2, 'tail is linked to head')
def test_get(self): queue = Queue(maxsize=1) self.assertEqual(queue.qsize(), 0) self.assertEqual(queue.full(), False) with self.assertRaises(Empty): queue.get(timeout=1) with self.assertRaises(ValueError): queue.get(timeout=-4) queue.put("1.txt", {"content": "file content"}, timeout=10) self.assertEqual(queue.qsize(), 1) self.assertEqual(queue.full(), True) queue_message = queue.get(timeout=1) self.assertEqual(queue_message.content, {"content": "file content"}) self.assertEqual(queue.qsize(), 1) self.assertEqual(queue.full(), True) queue = Queue(maxsize=2) queue.put("1.txt", {"content": "file content"}, timeout=10) self.assertEqual(queue.qsize(), 1) self.assertEqual(queue.full(), False) queue_message = queue.get() self.assertEqual(queue_message.id, "1.txt") self.assertEqual(queue_message.content, {"content": "file content"}) self.assertEqual(queue.qsize(), 1) self.assertEqual(queue.full(), False) queue.put("2.txt", {"content": "file content"}, timeout=10) self.assertEqual(queue.qsize(), 2) self.assertEqual(queue.full(), True) queue_message = queue.get() self.assertEqual(queue_message.id, "2.txt") self.assertEqual(queue_message.content, {"content": "file content"}) self.assertEqual(queue.qsize(), 2) self.assertEqual(queue.full(), True)
def test_enqueue(self): queue = Queue() queue.enqueue(1) queue.enqueue(2) queue.enqueue(3) res = [] count = 0 while count < 3: res.append(queue.dequeue()) count += 1 res_1 = [1, 2, 3] message = "test_enqueue is FAIL" self.assertEqual(res, res_1, message)
def test_dequeue(self): q = Queue() q.enqueue(1) q.enqueue(2) value = q.dequeue() self.assertEqual(value, 1, 'should return the first inserted item') self.assertEqual(q.head['value'], q.tail['value'], 'tail and head are the same after an dequeue') value = q.dequeue() self.assertEqual(value, 2, 'should return the second inserted item') self.assertIsNone(q.head, 'head is None') self.assertIsNone(q.tail, 'tail is None')
def test_timeout_get(self): queue = Queue(maxsize=1) t1 = MessageConsumer(queue) t1.start() time.sleep(1) t2 = MessageFeeder(queue, "1.txt", {"content": "file content"}) t2.start() t2.join() self.assertTrue(t2.result) t1.done = True t1.join() self.assertTrue(t1.result) self.assertEqual(queue.qsize(), 0)
def testQueue(): ''' Here we test algorithms for queues We test enqueue, dequeue, and checking if a queue is empty Queue is implemented as a python list It's not the best implementation as we'll show that the queue will be empty yet it will be occupying memory with the previously inserted elements ''' print('Create an empty queue') q = Queue() print('Dequeue now') print('Dequeued element: ' + str(q.dequeue())) print('Insert 5') q.enqueue(5) print('Insert 3') q.enqueue(3) print('Insert 4') q.enqueue(4) print('Is queue empty: ' + str(q.isempty())) print('Position of head: ' + str(q.head)) print('Position of tail: ' + str(q.tail)) print('Dequeue now') print('Dequeued element: ' + str(q.dequeue())) print('Dequeue now') print('Dequeued element: ' + str(q.dequeue())) print('Dequeue now') print('Dequeued element: ' + str(q.dequeue())) print('Dequeue now') print('Dequeued element: ' + str(q.dequeue())) print('Queue in the memory') print(q.storage) print('Insert 6') q.enqueue(6) print('Dequeue now') print('Dequeued element: ' + str(q.dequeue())) print('Queue in the memory') print(q.storage)