def test_release_empty_queue(self): queue = HashQueue(self.mock_redis_conn, has_proxy=True) qname = md5(self.user_id + "_queue").hexdigest() self.mock_redis_conn.get.return_value = 0 l1, l2 = queue.release(qname, self.uuid) self.assertEqual(0, l1) self.assertEqual(0, l2)
def test_dequeue_pipeline_connection_error(self): queue = HashQueue(self.mock_redis_conn, has_proxy=True) self.mock_redis_conn.pipeline.return_value.execute.side_effect =\ ConnectionError qname = md5(self.user_id + "_queue").hexdigest() with self.assertRaises(ConnectionError) as ar: queue.dequeue(qname, self.uuid) self.assertIsInstance(ar.exception, ConnectionError)
def test_dequeue_not_found(self): queue = HashQueue(self.mock_redis_conn, has_proxy=True) qname = md5("None" + "_queue").hexdigest() self.mock_redis_conn.pipeline.return_value.execute.return_value =\ None, None with self.assertRaises(ERedisKeyNotFound) as ar: k, data = queue.dequeue(qname, self.uuid) self.assertIsInstance(ar.exception, ERedisKeyNotFound)
def test_enqueue(self): queue = HashQueue(self.mock_redis_conn, has_proxy=True) qname = md5(self.user_id + "_queue").hexdigest() self.mock_redis_conn.get.return_value = 10 self.mock_redis_conn.pipeline.return_value.execute.return_value =\ 1, 1, 11, 11 r = queue.enqueue(qname, self.uuid, self.data) self.assertIsNone(r)
def test_dequeue_pipeline_timeout_error(self): queue = HashQueue(self.mock_redis_conn) self.mock_redis_conn.pipeline.return_value.execute.side_effect =\ TimeoutError qname = md5(self.user_id + "_queue").hexdigest() with self.assertRaises(TimeoutError) as ar: queue.dequeue(qname, self.uuid) self.assertIsInstance(ar.exception, TimeoutError)
def test_release_pipeline_connection_error(self): self.mock_redis_conn.get.return_value = 10 self.mock_redis_conn.hget.side_effect = ConnectionError queue = HashQueue(self.mock_redis_conn, has_proxy=True) qname = md5(self.user_id + "_queue").hexdigest() with self.assertRaises(ConnectionError) as ar: queue.release(qname, self.uuid) self.assertIsInstance(ar.exception, ConnectionError)
def test_enqueue_queue_full(self): queue = HashQueue(self.mock_redis_conn) self.mock_redis_conn.get.return_value = '1024' self.assertEqual(len(queue), 1024) qname = md5(self.user_id + "_queue").hexdigest() with self.assertRaises(ERedisQueueFull) as ar: queue.enqueue(qname, self.uuid, self.data) self.assertIsInstance(ar.exception, ERedisQueueFull)
def test_enqueue_pipeline_connection_error(self): self.mock_redis_conn.get.return_value = None queue = HashQueue(self.mock_redis_conn) self.mock_redis_conn.pipeline.side_effect = ConnectionError qname = md5(self.user_id + "_queue").hexdigest() with self.assertRaises(ConnectionError) as ar: queue.enqueue(qname, self.uuid, self.data) self.assertIsInstance(ar.exception, ConnectionError)
def test_dequeue_data_missing(self): queue = HashQueue(self.mock_redis_conn, has_proxy=True) qname = md5(self.user_id + "_queue").hexdigest() job = pickle.dumps(Job(self.uuid)) self.mock_redis_conn.pipeline.return_value.execute.return_value =\ None, job with self.assertRaises(ERedisDataMissing) as ar: k, data = queue.dequeue(qname, self.uuid) self.assertIsInstance(ar.exception, ERedisDataMissing)
def test_dequeue(self): queue = HashQueue(self.mock_redis_conn, has_proxy=True) qname = md5(self.user_id + "_queue").hexdigest() job = pickle.dumps(Job(self.uuid)) self.mock_redis_conn.pipeline.return_value.execute.return_value =\ self.data, job k, data = queue.dequeue(qname, self.uuid) self.assertEqual(k, self.uuid) self.assertEqual(data, self.data)
def test_proxy(self): queue = HashQueue(self.mock_redis_conn, has_proxy=False) self.mock_redis_conn.get.return_value = 10 queue.redis.pipeline.return_value.execute.side_effect = ConnectionError qname = md5(self.user_id + "_queue").hexdigest() try: queue.enqueue(qname, self.uuid, self.data) except Exception as e: self.assertIsInstance(e, ConnectionError)
def test_release_undequeued_job(self): queue = HashQueue(self.mock_redis_conn, has_proxy=True) qname = md5(self.user_id + "_queue").hexdigest() job = pickle.dumps(Job(self.uuid)) self.mock_redis_conn.get.return_value = 10 self.mock_redis_conn.hget.return_value = job with self.assertRaises(ERedisReleaseError) as ar: queue.release(qname, self.uuid) self.assertIsInstance(ar.exception, ERedisReleaseError)
def test_queue_stats(self): self.mock_redis_conn.get.return_value = '20' self.mock_redis_conn.hget.return_value = '10' queue = HashQueue(self.mock_redis_conn) qname = md5(self.user_id + "_queue").hexdigest() stat = queue.stats(qname) self.assertEqual(1024, stat['size']) self.assertEqual(20, stat['length']) self.assertEqual('10', stat[qname]) self.mock_redis_conn.hget.return_value = None stat = queue.stats(qname) self.assertEqual(None, stat[qname])
def test_release(self): queue = HashQueue(self.mock_redis_conn, has_proxy=True) qname = md5(self.user_id + "_queue").hexdigest() job = pickle.dumps(Job(self.uuid)) job = pickle.loads(job) job.change_state(JOB_DEQUEUED) job = pickle.dumps(job) self.mock_redis_conn.get.return_value = 10 self.mock_redis_conn.hget.return_value = job self.mock_redis_conn.pipeline.return_value.execute.return_value =\ 1, 1, 9, 9 l1, l2 = queue.release(qname, self.uuid) self.assertEqual(9, l1) self.assertEqual(9, l2)
def test_queue_length(self): queue = HashQueue(self.mock_redis_conn) self.mock_redis_conn.get.return_value = None self.assertEqual(0, len(queue)) self.mock_redis_conn.reset_mock() self.mock_redis_conn.get.return_value = '1024' self.assertEqual(1024, len(queue))
def test_release_pipeline_timeout_error(self): self.mock_redis_conn.get.return_value = 10 self.mock_redis_conn.hget.side_effect = TimeoutError queue = HashQueue(self.mock_redis_conn) qname = md5(self.user_id + "_queue").hexdigest() with self.assertRaises(TimeoutError) as ar: queue.release(qname, self.uuid) self.assertIsInstance(ar.exception, TimeoutError) self.mock_redis_conn.reset_mock() self.mock_redis_conn.get.side_effect = TimeoutError with self.assertRaises(TimeoutError) as ar: queue.release(qname, self.uuid) self.assertIsInstance(ar.exception, TimeoutError) self.mock_redis_conn.reset_mock() self.mock_redis_conn.get.return_value = 10 self.mock_redis_conn.hget.return_value = pickle.dumps(Job(self.uuid)) self.mock_redis_conn.pipeline.return_value.execute.side_effect =\ TimeoutError with self.assertRaises(TimeoutError) as ar: queue.release(qname, self.uuid) self.assertIsInstance(ar.exception, TimeoutError)