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 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
    @remoulade.actor(notify_shutdown=actor_opt)
    def do_work():
        pass

    # And this actor is declared
    stub_broker.declare_actor(do_work)

    # 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