def test_stop_closes_the_websocket(self, make_server, wsgi_app):
        # Given
        controller = WebSocketController()
        controller.start(8080)
        ws = Mock()

        # When
        controller.on_web_socket_opened(ws)
        controller.stop()

        # Then
        ws.close.assert_called_once_with()
    def test_send_async_sends_message_after_the_web_socket_is_opened(self,
                                                                     make_server,
                                                                     wsgi_app):
        # Given
        controller = WebSocketController()
        ws = Mock()
        ws_send_called = threading.Event()

        ws.send.side_effect = lambda msg: ws_send_called.set()

        # When
        controller.start(8080)
        controller.send_async('hi')

        # Then
        assert_false(ws_send_called.wait(0.1))

        # When
        controller.on_web_socket_opened(ws)

        # Then
        assert_true(ws_send_called.wait(1))
        assert_equal(ws.send.call_args[0][0], 'hi')