Beispiel #1
0
    def test_deserializing_multiple_messages_bytes_stream(self):
        mess = putils.create_find_node_message(self.sender, self.receiver, 55)
        mess2 = putils.create_find_node_message(self.sender, self.receiver, 33)
        serialized = putils.serialize_message(mess)
        serialized2 = putils.serialize_message(mess2)

        self.assertEqual(mess2,
                         putils.deserialize_message(serialized + serialized2))
Beispiel #2
0
    def test_that_server_receives_messages(self):
        """
        We tell SocketLayer to start the server.
        It should start it in another thread and listen for incoming connections at given socket.
        We test that by sending some message to the server and if everything works it should handle it and put it into the queue.
        """
        self.socket_layer.start_server("127.0.0.1", 8080)
        log.debug("Create socket")
        sock = socket.socket()
        sock2 = socket.socket()
        try:
            sock.connect(("127.0.0.1", 8080))
            sock2.connect(("127.0.0.1", 8080))
        except socket.error as msg:
            log.warning("Could not connect to the server")
            return

        mess = putils.create_ping_message(self.sender, self.receiver)

        sender2 = Peer(555, '1.123.1.123', 333, True)
        receiver2 = Peer(5434, '64.88.66.22', 444, True)
        mess2 = putils.create_ping_message(sender2, receiver2)

        serialized = putils.serialize_message(mess)
        framed = self.socket_layer._frame_message(serialized)
        serialized2 = putils.serialize_message(mess2)
        framed2 = self.socket_layer._frame_message(serialized2)

        try:
            log.debug("Try to send the messages")
            sock.send(framed)
            log.debug("Try to send next messages")
            sock2.send(framed2)
        except socket.error as msg:
            log.warning("Could not send the message: {}".format(msg))
            sock.close()
            sock2.close()
            return

        sock.close()
        sock2.close()

        #Get the message
        msg = _run(self.higher[1].get())
        msg2 = _run(self.higher[1].get())
        log.warning("Stop the server")
        status = _run(self.socket_layer.stop_server())
        self.assertIs(status, StatusMessage.SUCCESS)
        # And now we check if the message is correct
        self.assertIn(serialized, [msg, msg2])
        self.assertIn(serialized2, [msg, msg2])
Beispiel #3
0
 def _serialize_message(self, message):
     """
     Encode given message to string of bytes
     :param message: Message to encode
     :return: Encoded message
     """
     return putils.serialize_message(message)
Beispiel #4
0
 def test_passing_message(self):
     """
     Message layer got some message from higher layer.
     It serializes it.
     Then it passes it on to the lower layer using queue.
     """
     mess = putils.create_ping_message(self.sender, self.receiver)
     address = putils.get_receiver_address(mess)
     _run(self.higher[0].put(mess))
     # wait a while for message to propagate through the layer
     _run(asyncio.sleep(0.1))
     self.assertEqual(_run(self.lower[1].get()),
                      (putils.serialize_message(mess), address))
Beispiel #5
0
 def test_encoding_and_decoding(self):
     mess = putils.create_ping_message(self.sender, self.receiver)
     self.assertEqual(
         putils.deserialize_message(putils.serialize_message(mess)), mess)