def test_exit(bus, listener, log_tracker):
    """Test that bus exit sequence is correct."""
    num = 3

    for index in range(num):
        bus.subscribe('stop', listener.get_listener('stop', index))
        bus.subscribe('exit', listener.get_listener('exit', index))

    bus.exit()

    # The exit method MUST call all 'stop' listeners,
    # and then all 'exit' listeners.
    assert set(listener.responses) == set(
        [msg % (i, 'stop', None) for i in range(num)]
        + [msg % (i, 'exit', None) for i in range(num)],
    )

    # The exit method MUST move the state to EXITING
    assert bus.state == bus.states.EXITING

    # The exit method MUST log its states.
    assert log_tracker.log_entries == [
        'Bus STOPPING',
        'Bus STOPPED',
        'Bus EXITING',
        'Bus EXITED',
    ]
Test that bus exit sequence is correct.
cherrypy/test/test_bus.py