def test_block(bus, log_tracker):
    """Test that bus block waits for exiting."""

    def f():  # pylint: disable=invalid-name
        time.sleep(0.2)
        bus.exit()

    def g():  # pylint: disable=invalid-name
        time.sleep(0.4)

    threading.Thread(target=f).start()
    threading.Thread(target=g).start()
    threads = [t for t in threading.enumerate() if not t.daemon]
    assert len(threads) == 3

    bus.block()

    # The block method MUST wait for the EXITING state.
    assert bus.state == bus.states.EXITING

    # The block method MUST wait for ALL non-main, non-daemon threads to
    # finish.
    threads = [t for t in threading.enumerate() if not t.daemon]
    assert len(threads) == 1

    # The last message will mention an indeterminable thread name; ignore
    # it
    expected_bus_messages = [
        'Bus STOPPING',
        'Bus STOPPED',
        'Bus EXITING',
        'Bus EXITED',
        'Waiting for child threads to terminate...',
    ]
    bus_msg_num = len(expected_bus_messages)

    # If the last message mentions an indeterminable thread name then ignore it
    assert log_tracker.log_entries[:bus_msg_num] == expected_bus_messages
    assert len(log_tracker.log_entries[bus_msg_num:]) <= 1, (
        'No more than one extra log line with the thread name expected'
    )
Test that bus block waits for exiting.
cherrypy/test/test_bus.py