def test_gossip_core_stats(self): # Test that stats are set and change as different methods are used core = self._setup(8801) packetdic1 = core.PacketStats.get_stats() msgdic1 = str(core.MessageStats.get_stats()) newNode = self._create_node(8859) core.add_node(newNode) msg = self._create_msg() pak = Packet() pak.add_message(msg, newNode, newNode, 0) # Following methods should update PacketStats core._do_write(pak.pack(), newNode) core._send_ack(pak, newNode) data = pak.pack() # Following method should update MessageStats packetdic2 = core.PacketStats.get_stats() core.datagramReceived(data, "localhost:8801") msgdic2 = str(core.MessageStats.get_stats()) msgdic = core.MessageStats.get_stats(["MessageType"])["MessageType"] self.assertEqual(packetdic2["MessagesAcked"], 1) self.assertNotEqual(packetdic2["BytesSent"], [0, 0]) self.assertNotEqual(packetdic1, packetdic2) self.assertNotEqual(msgdic1, msgdic2) self.assertEqual(msgdic['/gossip.Message/MessageBase'], 1)
def test_gossip_core_stats(self): # Test that stats are set and change as different methods are used core = self._setup(8801) packetdic1 = core.PacketStats.get_stats() msgdic1 = str(core.MessageStats.get_stats()) newNode = self._create_node(8859) core.add_node(newNode) msg = self._create_msg() pak = Packet() pak.add_message(msg, newNode, newNode, 0) # Following methods should update PacketStats core._do_write(pak.pack(), newNode) core._send_ack(pak, newNode) data = pak.pack() # Following method should update MessageStats packetdic2 = core.PacketStats.get_stats() core.datagramReceived(data, "localhost:8801") msgdic2 = str(core.MessageStats.get_stats()) msgdic = core.MessageStats.get_stats(["MessageType"])["MessageType"] self.assertEqual(packetdic2["MessagesAcked"], 1) self.assertNotEqual(packetdic2["BytesSent"], [0, 0]) self.assertNotEqual(packetdic1, packetdic2) self.assertNotEqual(msgdic1, msgdic2) self.assertEqual(msgdic['/gossip.Message/MessageBase'], 1)
def test_gossip_datagram_received(self): # Test that datagramReceived behaves as expected core = self._setup(9500) peer = self._create_node(9501) peer2 = self._create_node(9502) core.add_node(peer) core.add_node(peer2) msg = self._create_msg() pak = Packet() pak.add_message(msg, peer, peer2, 0) data = pak.pack() # Test correct use of datagramReceived core.datagramReceived(data, "localhost:9001") msgType = core.MessageStats.get_stats(["MessageType"]) self.assertIn('/gossip.Message/MessageBase', msgType["MessageType"]) pakStats = core.PacketStats.get_stats(["MessagesAcked"]) self.assertEqual(pakStats["MessagesAcked"], 1) # Test handling of duplicate packets msg2 = shutdown_message.ShutdownMessage({'__SIGNATURE__': "test"}) core.MessageHandledMap[msg2.Identifier] = time.time() pak.add_message(msg2, peer, peer2, 1) data2 = pak.pack() core.datagramReceived(data2, "localhost:9001") pakStats = core.PacketStats.get_stats(["DuplicatePackets"]) self.assertEqual(pakStats["DuplicatePackets"], 1)
def test_gossip_datagram_received(self): # Test that datagramReceived behaves as expected core = self._setup(9500) peer = self._create_node(9501) peer2 = self._create_node(9502) core.add_node(peer) core.add_node(peer2) msg = self._create_msg() pak = Packet() pak.add_message(msg, peer, peer2, 0) data = pak.pack() # Test correct use of datagramReceived core.datagramReceived(data, "localhost:9001") msgType = core.MessageStats.get_stats(["MessageType"]) self.assertIn('/gossip.Message/MessageBase', msgType["MessageType"]) pakStats = core.PacketStats.get_stats(["MessagesAcked"]) self.assertEqual(pakStats["MessagesAcked"], 1) # Test handling of duplicate packets msg2 = shutdown_message.ShutdownMessage({'__SIGNATURE__': "test"}) core.MessageHandledMap[msg2.Identifier] = time.time() pak.add_message(msg2, peer, peer2, 1) data2 = pak.pack() core.datagramReceived(data2, "localhost:9001") pakStats = core.PacketStats.get_stats(["DuplicatePackets"]) self.assertEqual(pakStats["DuplicatePackets"], 1)
def test_gossip_datagram_unknown_peer(self): # Test that nothing is done if the nodes are not known core = self._setup(9007) peer = self._create_node(9008) peer2 = self._create_node(9009) msg = self._create_msg() pak = Packet() pak.add_message(msg, peer, peer2, 0) data = pak.pack() status = core.datagramReceived(data, "localhost:9001") self.assertIsNone(status)
def test_gossip_datagram_unknown_peer(self): # Test that nothing is done if the nodes are not known core = self._setup(9007) peer = self._create_node(9008) peer2 = self._create_node(9009) msg = self._create_msg() pak = Packet() pak.add_message(msg, peer, peer2, 0) data = pak.pack() status = core.datagramReceived(data, "localhost:9001") self.assertIsNone(status)
def test_gossip_ack_good(self): # Test sending acknowledgements core = self._setup(8808) pak = Packet() newNode = self._create_node(8809) core.add_node(newNode) newNode.is_peer = True core._send_ack(pak, newNode) # Add pak to PendingAckMap msg = self._create_msg() pak.add_message(msg, newNode, newNode, 0) core.PendingAckMap[pak.SequenceNumber] = pak ack = pak.create_ack(newNode.Identifier) core._handle_ack(ack) stats = core.PacketStats.get_stats(["AcksReceived"]) self.assertEqual(stats["AcksReceived"], 1)
def test_gossip_datagram_recieved_ack(self): # Test that datagramReceived handles acknowledgements correctly core = self._setup(9004) peer = self._create_node(9005) peer2 = self._create_node(9006) core.add_node(peer) core.add_node(peer2) msg = self._create_msg() pak = Packet() pak.add_message(msg, peer, peer2, 0) newPak = pak.create_ack(peer2.Identifier) data = newPak.pack() core.PendingAckMap[0] = pak # send ack core.datagramReceived(data, "localhost:9001") stats = core.PacketStats.get_stats(["AcksReceived"]) self.assertEqual(stats["AcksReceived"], 1)
def test_gossip_ack_good(self): # Test sending acknowledgements core = self._setup(8808) pak = Packet() newNode = self._create_node(8809) core.add_node(newNode) newNode.is_peer = True core._send_ack(pak, newNode) # Add pak to PendingAckMap msg = self._create_msg() pak.add_message(msg, newNode, newNode, 0) core.PendingAckMap[pak.SequenceNumber] = pak ack = pak.create_ack(newNode.Identifier) core._handle_ack(ack) stats = core.PacketStats.get_stats(["AcksReceived"]) self.assertEqual(stats["AcksReceived"], 1)
def test_gossip_datagram_recieved_ack(self): # Test that datagramReceived handles acknowledgements correctly core = self._setup(9004) peer = self._create_node(9005) peer2 = self._create_node(9006) core.add_node(peer) core.add_node(peer2) msg = self._create_msg() pak = Packet() pak.add_message(msg, peer, peer2, 0) newPak = pak.create_ack(peer2.Identifier) data = newPak.pack() core.PendingAckMap[0] = pak # send ack core.datagramReceived(data, "localhost:9001") stats = core.PacketStats.get_stats(["AcksReceived"]) self.assertEqual(stats["AcksReceived"], 1)
def test_add_message(self): # Add a message to a packet, along with source node and destination # Resets the packet attributes with that of the message pak = Packet() # Need signingkey, otherwise throws errors srcNode = Node(identifier="source", signingkey="source") desNode = Node(identifier="destination", signingkey="destination") msg = Message({'__SIGNATURE__': "MsgTestS"}) pak.add_message(msg, srcNode, desNode, 1) # Check that msg and pak have the same attributes self.assertEqual([msg.Identifier, msg.TimeToLive, msg.IsReliable], [pak.Identifier, pak.TimeToLive, pak.IsReliable]) # Check correct SenderID self.assertEqual(srcNode.Identifier, pak.SenderID) # Check correct destinationID and destination RTE self.assertEqual(desNode.Identifier, pak.DestinationID) self.assertEqual(desNode.Estimator.RTO, pak.RoundTripEstimate) # Check correct data and msg self.assertEqual(pak.Message, msg) self.assertEqual(pak.Data, repr(msg))
def test_add_message(self): # Add a message to a packet, along with source node and destination # Resets the packet attributes with that of the message pak = Packet() # Need signingkey, otherwise throws errors srcNode = Node(identifier="source", signingkey="source") desNode = Node(identifier="destination", signingkey="destination") msg = Message({'__SIGNATURE__': "MsgTestS"}) pak.add_message(msg, srcNode, desNode, 1) # Check that msg and pak have the same attributes self.assertEquals([msg.Identifier, msg.TimeToLive, msg.IsReliable], [pak.Identifier, pak.TimeToLive, pak.IsReliable]) # Check correct SenderID self.assertEquals(srcNode.Identifier, pak.SenderID) # Check correct destinationID and destination RTE self.assertEquals(desNode.Identifier, pak.DestinationID) self.assertEquals(desNode.Estimator.RTO, pak.RoundTripEstimate) # Check correct data and msg self.assertEquals(pak.Message, msg) self.assertEquals(pak.Data, repr(msg))
def test_gossip_ack_unknown_packet(self): # Test behavior of sending a packet incorrectly core = self._setup(8810) pak = Packet() newNode = self._create_node(8811) core.add_node(newNode) # Ignore acks from unexpected packets not in PendingAckMap core._handle_ack(pak) stats = core.PacketStats.get_stats(["AcksReceived"]) self.assertEqual(stats["AcksReceived"], 0) badNode = self._create_node(8812) core.add_node(badNode) # Test behavior of receiving an ack with bad Identifier # Will print out two warnings msg = self._create_msg() pak.add_message(msg, newNode, newNode, 0) core.PendingAckMap[pak.SequenceNumber] = pak ack = pak.create_ack(newNode.Identifier) pak.DestinationID = badNode.Identifier core._handle_ack(pak) stats = core.PacketStats.get_stats(["AcksReceived"]) self.assertEqual(stats["AcksReceived"], 0)
def test_gossip_ack_unknown_packet(self): # Test behavior of sending a packet incorrectly core = self._setup(8810) pak = Packet() newNode = self._create_node(8811) core.add_node(newNode) # Ignore acks from unexpected packets not in PendingAckMap core._handle_ack(pak) stats = core.PacketStats.get_stats(["AcksReceived"]) self.assertEqual(stats["AcksReceived"], 0) badNode = self._create_node(8812) core.add_node(badNode) # Test behavior of receiving an ack with bad Identifier # Will print out two warnings msg = self._create_msg() pak.add_message(msg, newNode, newNode, 0) core.PendingAckMap[pak.SequenceNumber] = pak ack = pak.create_ack(newNode.Identifier) pak.DestinationID = badNode.Identifier core._handle_ack(pak) stats = core.PacketStats.get_stats(["AcksReceived"]) self.assertEqual(stats["AcksReceived"], 0)