Example #1
0
    def accept_new_connection(self, server_sock: socket.socket) -> None:
        sock, _ = server_sock.accept()
        with bind_remote_address_to_logger(sock):
            sock.setblocking(False)

            disconnector = Disconnector()
            send_buffer = SendBuffer()
            msg_framer = MessageFramer(send_buffer)
            serializer = Serializer(msg_framer)
            msg_sender = MessageSender(serializer)
            client = Client(msg_sender, disconnector)
            msg_router = MessageRouter(self._server, client)
            parsed_msg_handler = ParsedMessageHandler(msg_router)
            deserializer = Deserializer(parsed_msg_handler)
            msg_splitter = MessageSplitter(deserializer)

            self._clients[sock] = ClientConnection(sock, send_buffer,
                                                   msg_splitter, client)
            self._server.on_client_connected(client)

            self._sel.register(
                sock,
                selectors.EVENT_READ | selectors.EVENT_WRITE,
                self._process_sock_event  # type: ignore
            )
            _logger.debug("New client connected")
Example #2
0
def test_join_creates_room_when_first_user(sut_with_room, chat_room_factory,
                                           chat_room):
    client2 = Client(MagicMock(spec_set=MessageSender),
                     MagicMock(spec_set=Disconnector), "username2")
    sut_with_room.join("#room", client2)

    chat_room_factory.assert_not_called()
    chat_room.join.assert_called_once_with(client2)
Example #3
0
def test_send_message(sut_with_client, client):
    client2 = Client(
        MagicMock(spec_set=MessageSender), MagicMock(spec_set=Disconnector), "username2"
    )
    sut_with_client.join(client2)

    sut_with_client.send_message(
        ChatFromClient("#doesnt-matter", "message text"), client
    )
    client.msg_sender.send.assert_not_called()
    client2.msg_sender.send.assert_called_with(
        ChatToClient("username", "message text", "#room")
    )
Example #4
0
def client():
    return Client(MagicMock(spec_set=MessageSender),
                  MagicMock(spec_set=Disconnector), "username")