def test_shutdown_notifications_gevent_worker_shutdown_messages(caplog): # capture all messages caplog.set_level(logging.NOTSET) # Given a middleware with two threads middleware = shutdown.ShutdownNotifications() greenlet_1 = gevent.spawn() greenlet_2 = gevent.spawn() middleware.manager.notification_greenlets = [(1, greenlet_1), (2, greenlet_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 assert isinstance(greenlet_1.exception, shutdown.Shutdown) assert isinstance(greenlet_2.exception, shutdown.Shutdown) # And shutdown notifications are logged assert len(caplog.record_tuples) == 3 assert caplog.record_tuples == [ ("dramatiq.middleware.shutdown.ShutdownNotifications", logging.DEBUG, ( "Sending shutdown notification to worker threads..." )), ("dramatiq.middleware.shutdown.ShutdownNotifications", logging.INFO, ( "Worker shutdown notification. Raising exception in worker thread 1." )), ("dramatiq.middleware.shutdown.ShutdownNotifications", logging.INFO, ( "Worker shutdown notification. Raising exception in worker thread 2." )), ]
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.manager.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 == [ ("dramatiq.middleware.shutdown.ShutdownNotifications", logging.DEBUG, ( "Sending shutdown notification to worker threads..." )), ("dramatiq.middleware.shutdown.ShutdownNotifications", logging.INFO, ( "Worker shutdown notification. Raising exception in worker thread 1." )), ("dramatiq.middleware.shutdown.ShutdownNotifications", logging.INFO, ( "Worker shutdown notification. Raising exception in worker thread 2." )), ]
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_shutdown_notifications_options(stub_broker, actor_opt, message_opt, should_notify): # Given the shutdown notifications middleware middleware = shutdown.ShutdownNotifications() # And an actor @dramatiq.actor(notify_shutdown=actor_opt) def do_work(): pass # And a message message = do_work.message_with_options(notify_shutdown=message_opt) # The notification should only be set when expected assert middleware.should_notify(do_work, message) == should_notify