Esempio n. 1
0
def test_drain_queue(mp_fixture):
    q = MPQueue(ctx=mp_fixture)

    items = list(q.drain())
    assert items == []

    expected = [f"ITEM{idx}" for idx in range(10)]
    for item in expected:
        q.put(item)

    items = list(q.drain())
    assert items == expected

    num_left = q.safe_close()
    assert num_left == 0
Esempio n. 2
0
def _proc_worker_wrapper_helper(
    mp_fixture,
    caplog,
    worker_class,
    args=None,
    kwargs=None,
    expect_shutdown_evt=True,
    alarm_secs=1.0,
):
    startup_evt = mp_fixture.Event()
    shutdown_evt = mp_fixture.Event()
    event_q = MPQueue(ctx=mp_fixture)
    if args is None:
        args = ()
    if kwargs is None:
        kwargs = {}

    def alarm_handler(signal_num, current_stack_frame):
        shutdown_evt.set()

    if alarm_secs:
        signal.signal(signal.SIGALRM, alarm_handler)
        signal.setitimer(signal.ITIMER_REAL, alarm_secs)
    caplog.set_level(logging.DEBUG)
    exitcode = proc_worker_wrapper(worker_class, "TEST", startup_evt,
                                   shutdown_evt, event_q, *args, **kwargs)
    assert startup_evt.is_set()
    assert shutdown_evt.is_set() == expect_shutdown_evt
    items = list(event_q.drain())
    assert items
    last_item = items[-1]
    assert last_item.msg_src == "TEST"
    assert last_item.msg_type == "SHUTDOWN"
    assert last_item.msg == "Normal"
    assert exitcode == 0

    return items[:-1]