def test_shutdown_notifications_worker_shutdown_messages( raise_thread_exception, caplog): # capture all messages caplog.set_level(logging.NOTSET) # Given a middleware with two "threads" middleware = shutdown.ShutdownNotifications() middleware.notifications = [1, 2] # Given a broker configured with the shutdown notifier broker = StubBroker(middleware=[middleware]) # When the worker is shutdown broker.emit_before("worker_shutdown", None) # Shutdown interrupts are raised in both threads raise_thread_exception.assert_has_calls([ mock.call(1, shutdown.Shutdown), mock.call(2, shutdown.Shutdown), ]) # And shutdown notifications are logged assert len(caplog.record_tuples) == 3 assert caplog.record_tuples == [ ("remoulade.middleware.shutdown.ShutdownNotifications", logging.DEBUG, ("Sending shutdown notification to worker threads...")), ("remoulade.middleware.shutdown.ShutdownNotifications", logging.INFO, ("Worker shutdown notification. Raising exception in worker thread 1." )), ("remoulade.middleware.shutdown.ShutdownNotifications", logging.INFO, ("Worker shutdown notification. Raising exception in worker thread 2." )), ]
def stub_broker(): broker = StubBroker() broker.emit_after("process_boot") remoulade.set_broker(broker) yield broker broker.flush_all() broker.emit_before("process_stop") broker.close()
def test_shutdown_notifications_platform_not_supported(recwarn, monkeypatch): # monkeypatch fake platform to test logging. monkeypatch.setattr(shutdown, "current_platform", "not supported") # Given a broker configured with the shutdown notifier broker = StubBroker(middleware=[shutdown.ShutdownNotifications()]) # When the process boots broker.emit_after("process_boot") # A platform support warning is issued assert len(recwarn) == 1 assert str( recwarn[0].message) == ("ShutdownNotifications cannot kill threads " "on your current platform ('not supported').")
def test_middleware_is_inserted_correctly(): broker = StubBroker(middleware=[]) assert len(broker.middleware) == 0 broker.add_middleware(AgeLimit()) assert len(broker.middleware) == 1 broker.add_middleware(ShutdownNotifications()) assert len(broker.middleware) == 2 broker.add_middleware(TimeLimit()) assert len(broker.middleware) == 3 assert isinstance(broker.middleware[1], TimeLimit)
def test_prometheus_middleware_exposes_metrics(): try: # Given a broker broker = StubBroker() # And an instance of the prometheus middleware prom = prometheus.Prometheus() prom.after_process_boot(broker) # If I wait for the server to start sleep(0.01) # When I request metrics via HTTP with request.urlopen("http://127.0.0.1:9191") as resp: # Then the response should be successful assert resp.getcode() == 200 finally: prom.after_worker_shutdown(broker, None)
def test_change_broker(stub_broker): # Given that some actors @remoulade.actor def add(x, y): return x + y @remoulade.actor def modulo(x, y): return x % y # And these actors are declared stub_broker.declare_actor(add) stub_broker.declare_actor(modulo) # Given a new broker new_broker = StubBroker() remoulade.change_broker(new_broker) # I expect them to have the same actors assert stub_broker.actors == new_broker.actors
def test_no_timer_signal_after_shutdown(): # Given a broker broker = StubBroker() # With a TimeLimit middleware for middleware in broker.middleware: if isinstance(middleware, TimeLimit): # That emit a signal every ms middleware.interval = 1 broker.emit_after("process_boot") handler = Mock() signal.signal(signal.SIGALRM, handler) broker.emit_before("process_stop") # If i wait enough time to get a signal time.sleep(2 / 1000) # No signal should have been sent assert not handler.called
import os import signal import time import remoulade from remoulade.brokers.stub import StubBroker broker = StubBroker() remoulade.set_broker(broker) def remove(filename): try: os.remove(filename) except OSError: pass def test_cli_scrubs_stale_pid_files(start_cli): try: # Given that I have an existing file containing an old pid filename = "test_scrub.pid" with open(filename, "w") as f: f.write("999999") # When I try to start the cli and pass that file as a PID file proc = start_cli("tests.test_pidfile", extra_args=["--pid-file", filename]) # And I wait for it to write the pid file time.sleep(1)