def test_job_queue_close_and_forget(): def f(xs, x): gevent.sleep(1) xs.append(x) # close, join. queue1, xs1 = lets.JobQueue(), [] queue1.put(Greenlet(f, xs1, 0)) queue1.put(Greenlet(f, xs1, 1)) queue1.put(Greenlet(f, xs1, 2)) queue1.put(Greenlet(f, xs1, 3)) queue1.close() with pytest.raises(RuntimeError): queue1.put(Greenlet(f, xs1, 4)) queue1.join() assert xs1 == [0, 1, 2, 3] # close, forget, join. queue2, xs2 = lets.JobQueue(), [] queue2.put(Greenlet(f, xs2, 0)) queue2.put(Greenlet(f, xs2, 1)) queue2.put(Greenlet(f, xs2, 2)) queue2.put(Greenlet(f, xs2, 3)) queue2.close() with pytest.raises(RuntimeError): queue2.put(Greenlet(f, xs2, 4)) queue2.join(0) queue2.forget() queue2.join() assert xs2 == [0]
def test_job_queue_join_workers(): ok = Event() q = lets.JobQueue() g = q.put(Greenlet(gevent.sleep, 0.1)) g.link(lambda g: ok.set()) # Before 0.0.24, JobQueue.join() doesn't guarantee finish of all workers. q.join() assert ok.is_set()
def test_job_queue_kill_with_error(): def f(): gevent.sleep(999) g = Greenlet(f) queue = lets.JobQueue() queue.put(g) queue.kill(ExpectedError) with pytest.raises(ExpectedError): g.get(block=False)
def test_job_queue_kill_before_working(): def f(): gevent.sleep(999) g = Greenlet(f) g.done = False g.link(lambda g: setattr(g, 'done', True)) queue = lets.JobQueue() queue.put(g) assert not g.done queue.kill() assert g.done
def test_job_queue(): results = [] def f(x, delay=0): gevent.sleep(delay) results.append(x) queue = lets.JobQueue() with pytest.raises(ValueError): queue.put(Greenlet.spawn()) queue.put(Greenlet(f, 1, 0.1)) queue.put(Greenlet(f, 2, 0)) queue.put(Greenlet(f, 3, 0.2)) queue.put(Greenlet(f, 4, 0.1)) queue.join() assert results == [1, 2, 3, 4]
def test_job_queue_sized(): results = [] def f(x, delay=0): gevent.sleep(delay) results.append(x) queue = lets.JobQueue(2) queue.put(Greenlet(f, 1, 0.1)) queue.put(Greenlet(f, 2, 0.1)) queue.put(Greenlet(f, 3, 0.1)) with pytest.raises(Full): queue.put(Greenlet(f, 4, 0.1), timeout=0.01) with pytest.raises(Full): queue.put(Greenlet(f, 5, 0.1), block=False) queue.join() assert results == [1, 2, 3]
def test_job_queue_with_multiple_workers(): results = [] def f(x, delay=0): gevent.sleep(delay) results.append(x) for workers, expected in [(1, [1, 2, 3, 4]), (2, [1, 3, 4, 2]), (3, [1, 4, 3, 2]), (4, [1, 4, 3, 2])]: queue = lets.JobQueue(workers=workers) queue.put(Greenlet(f, 1, 0.01)) queue.put(Greenlet(f, 2, 0.06)) queue.put(Greenlet(f, 3, 0.03)) queue.put(Greenlet(f, 4, 0.01)) queue.join() assert results == expected, '%d workers' % workers del results[:]
def test_job_queue_guarantees_all_jobs(): queue = lets.JobQueue() xs = [] def f(x): gevent.sleep(0.01) xs.append(x) queue.put(Greenlet(f, 0)) queue.put(Greenlet(f, 1)) g = Greenlet(f, 2) queue.put(g) g.join() gevent.sleep(0) # before 0.0.23, the worker has done but the worker pool is still full. # before 0.0.12, the final job won't be scheduled. queue.put(Greenlet(f, 3)) queue.join() assert xs == [0, 1, 2, 3]
def test_job_queue_exited(): results = [] def f(x, delay=0): gevent.sleep(delay) results.append(x) return x queue = lets.JobQueue() g1 = Greenlet(f, 1, 0.1) g2 = Greenlet(f, 2, 0.1) queue.put(g1) queue.put(g2) g1.join() queue.kill() queue.join() assert results == [1] assert g1.get() == 1 assert isinstance(g2.get(), gevent.GreenletExit)