def test_sending_messages(self): """ SocketLayer receives a message from higher layer so it sends it """ self.socket_layer.start_server("127.0.0.1", 8080) sender = Peer(33, "33.22.11.22", 9992, False) sender2 = Peer(21, "23.45.67.86", 9393, False) receiver = Peer(11, '127.0.0.1', 8080, False) mess = putils.create_ping_message(sender, receiver) mess2 = putils.create_ping_message(sender2, receiver) address = putils.get_receiver_address(mess) address2 = putils.get_receiver_address(mess2) _run(self.higher[0].put((mess.SerializeToString(), address))) _run(self.higher[0].put((mess2.SerializeToString(), address2))) # 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) self.assertEqual(msg, mess.SerializeToString()) self.assertEqual(msg2, mess2.SerializeToString())
def handle_message_from_higher_layer(self, message): """ Prepare given message to pass it to lower layer :param message: message to prepare :return: Message to pass on to lower layer """ receiver_address = putils.get_receiver_address(message) return (self._serialize_message(message), receiver_address)
def test_connecting_to_wrong_server(self): self.socket_layer.start_server("127.0.0.1", 8080) sender = Peer(33, "33.22.11.22", 9992, False) sender2 = Peer(21, "23.45.67.86", 9393, False) receiver = Peer(11, '127.0.0.1', 88, False) mess = putils.create_ping_message(sender, receiver) mess2 = putils.create_ping_message(sender2, receiver) address = putils.get_receiver_address(mess) address2 = putils.get_receiver_address(mess2) _run(self.higher[0].put((mess.SerializeToString(), address))) _run(self.higher[0].put((mess2.SerializeToString(), address2))) self.assertEqual(self.higher[1].qsize(), 0) # Get the message log.warning("Stop the server") status = _run(self.socket_layer.stop_server()) self.assertIs(status, StatusMessage.SUCCESS)
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))
def test_get_receiver_address(self): mess = putils.create_ping_message(self.sender, self.receiver) self.assertEqual((self.receiver.ip, self.receiver.port), putils.get_receiver_address(mess))