def test_gevent_class(): if os.getenv("DD_PROFILE_TEST_GEVENT", False): assert isinstance(_periodic.PeriodicRealThread(1, sum), _periodic._GeventPeriodicThread) else: assert isinstance(_periodic.PeriodicRealThread(1, sum), _periodic.PeriodicThread)
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.PeriodicRealThread(0.001, _run_periodic, on_shutdown=_on_shutdown) t.start() thread_started.wait() assert t.ident in _periodic.PERIODIC_THREADS thread_continue.set() t.stop() t.join() assert "DOWN" not in x assert t.ident not in _periodic.PERIODIC_THREADS
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.PeriodicRealThread(0.001, _run_periodic, on_shutdown=_on_shutdown) t.start() thread_started.wait() assert t.ident in _periodic.PERIODIC_THREADS thread_continue.set() t.stop() t.join() assert x["OK"] assert x["DOWN"] assert t.ident not in _periodic.PERIODIC_THREADS if hasattr(threading, "get_native_id"): assert t.native_id is not None
def test_periodic_double_start(): def _run_periodic(): pass t = _periodic.PeriodicRealThread(0.1, _run_periodic) t.start() with pytest.raises(RuntimeError): t.start()
def test_periodic_real_thread_name(): def do_nothing(): pass t = _periodic.PeriodicRealThread(interval=1, target=do_nothing) t.start() assert t in threading.enumerate() t.stop() t.join()
def test_periodic(): x = {"OK": False} def _run_periodic(): x["OK"] = True def _on_shutdown(): x["DOWN"] = True t = _periodic.PeriodicRealThread(0.001, _run_periodic, on_shutdown=_on_shutdown) t.start() assert t.ident in _periodic.PERIODIC_THREAD_IDS while not x["OK"]: pass t.stop() t.join() assert x["OK"] assert x["DOWN"] assert t.ident not in _periodic.PERIODIC_THREAD_IDS
def test_periodic_error(): x = {"OK": False} def _run_periodic(): x["OK"] = True raise ValueError def _on_shutdown(): x["DOWN"] = True t = _periodic.PeriodicRealThread(0.001, _run_periodic, on_shutdown=_on_shutdown) t.start() assert t.ident in _periodic.PERIODIC_THREAD_IDS while not x["OK"]: pass t.stop() t.join() assert "DOWN" not in x assert len(_periodic.PERIODIC_THREAD_IDS) == 0
def test_periodic(): x = {"OK": False} def _run_periodic(): x["OK"] = True def _on_shutdown(): x["DOWN"] = True t = _periodic.PeriodicRealThread(0.001, _run_periodic, on_shutdown=_on_shutdown) t.start() assert t.ident in _periodic.PERIODIC_THREAD_IDS while not x["OK"]: pass t.stop() t.join() assert x["OK"] assert x["DOWN"] assert t.ident not in _periodic.PERIODIC_THREAD_IDS if hasattr(threading, "get_native_id"): assert t.native_id is not None
def test_is_alive_before_start(): def x(): pass t = _periodic.PeriodicRealThread(1, x) assert not t.is_alive()