def test_get(self): mp_queue = mp.Queue() request_queue = MultiprocessingRequestQueue(mp_queue) requests = request_queue.get(1, timeout=0.01) self.assertEqual(0, len(requests)) request = TimerRequest(1, "test_scope", 0) mp_queue.put(request) requests = request_queue.get(2, timeout=0.01) self.assertEqual(1, len(requests)) self.assertIn(request, requests)
def test_get_less_than_size(self): """ Tests slow producer. Creates a "producer" process that enqueues ``n`` elements every ``interval`` seconds. Asserts that a ``get(n, timeout=(interval * n/2))`` yields at most ``n/2`` elements. """ mp_queue = mp.Queue() request_queue = MultiprocessingRequestQueue(mp_queue) n = 10 interval = 0.1 sem = mp.Semaphore(0) p = mp.Process(target=_enqueue_on_interval, args=(mp_queue, n, interval, sem)) p.start() sem.acquire() # blocks until the process has started to run the function requests = request_queue.get(n, timeout=(interval * (n / 2))) self.assertLessEqual(n / 2, len(requests))
def test_get_size(self): """ Creates a "producer" process that enqueues ``n`` elements every ``interval`` seconds. Asserts that a ``get(n, timeout=n*interval+delta)`` yields all ``n`` elements. """ mp_queue = mp.Queue() request_queue = MultiprocessingRequestQueue(mp_queue) n = 10 interval = 0.1 sem = mp.Semaphore(0) p = mp.Process(target=_enqueue_on_interval, args=(mp_queue, n, interval, sem)) p.start() sem.acquire() # blocks until the process has started to run the function timeout = interval * (n + 1) start = time.time() requests = request_queue.get(n, timeout=timeout) self.assertLessEqual(time.time() - start, timeout + interval) self.assertEqual(n, len(requests))