Example #1
0
def test_gevent_class():
    if os.getenv("DD_PROFILE_TEST_GEVENT", False):
        assert isinstance(periodic.PeriodicRealThreadClass()(1, sum),
                          periodic._GeventPeriodicThread)
    else:
        assert isinstance(periodic.PeriodicRealThreadClass()(1, sum),
                          periodic.PeriodicThread)
Example #2
0
def test_periodic():
    x = {"OK": False}

    thread_started = Event()
    thread_continue = Event()

    def _run_periodic():
        thread_started.set()
        x["OK"] = True
        thread_continue.wait()

    def _on_shutdown():
        x["DOWN"] = True

    t = periodic.PeriodicRealThreadClass()(0.001,
                                           _run_periodic,
                                           on_shutdown=_on_shutdown)
    t.start()
    thread_started.wait()
    thread_continue.set()
    assert t.is_alive()
    t.stop()
    t.join()
    assert not t.is_alive()
    assert x["OK"]
    assert x["DOWN"]
    if hasattr(threading, "get_native_id"):
        assert t.native_id is not None
Example #3
0
def test_periodic_double_start():
    def _run_periodic():
        pass

    t = periodic.PeriodicRealThreadClass()(0.1, _run_periodic)
    t.start()
    with pytest.raises(RuntimeError):
        t.start()
Example #4
0
def test_periodic_error():
    x = {"OK": False}

    thread_started = Event()
    thread_continue = Event()

    def _run_periodic():
        thread_started.set()
        thread_continue.wait()
        raise ValueError

    def _on_shutdown():
        x["DOWN"] = True

    t = periodic.PeriodicRealThreadClass()(0.001,
                                           _run_periodic,
                                           on_shutdown=_on_shutdown)
    t.start()
    thread_started.wait()
    thread_continue.set()
    t.stop()
    t.join()
    assert "DOWN" not in x
Example #5
0
def test_is_alive_before_start():
    def x():
        pass

    t = periodic.PeriodicRealThreadClass()(1, x)
    assert not t.is_alive()