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_create_ack(self): # Test creating an acknowledgement packet for a packet pak = Packet() # Fake nodeid for acknowledgement nodeID = "testNodeId" newAck = pak.create_ack(nodeID) self.assertIsNotNone(newAck) self.assertTrue(newAck.IsAcknowledgement) # Should have the same SequenceNumber self.assertEqual(newAck.SequenceNumber, pak.SequenceNumber) self.assertEqual(newAck.SenderID, nodeID)
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_create_ack(self): # Test creating an acknowledgement packet for a packet pak = Packet() # Fake nodeid for acknowledgement nodeID = "testNodeId" newAck = pak.create_ack(nodeID) self.assertIsNotNone(newAck) self.assertTrue(newAck.IsAcknowledgement) # Should have the same SequenceNumber self.assertEquals(newAck.SequenceNumber, pak.SequenceNumber) self.assertEquals(newAck.SenderID, nodeID)
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_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_dowrite(self): # Test _dowrite puts a message on the wire core = self._setup(8804) newNode = self._create_node(8805) newNode.is_peer = True core.add_node(newNode) core2 = Gossip pak = Packet() data = "test the pack" pak.Data = data # Test correct sending of bytes status = core._do_write(pak.pack(), newNode) self.assertTrue(status) stats = core.PacketStats.get_stats(["BytesSent"]) self.assertNotEqual(stats["BytesSent"], [0, 0]) # Test failure of message that is too big # This will print out an error core.MaximumPacketSize = 0 status = core._do_write(pak.pack(), newNode) self.assertFalse(status)
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_packet_init(self): # Test trival intialization of a packet # Check each value defualts correctly pak = Packet() self.assertEqual(pak.TimeToLive, 255) self.assertEqual(pak.SequenceNumber, 0) self.assertEqual(pak.IsAcknowledgement, False) self.assertEqual(pak.IsReliable, True) self.assertEqual(pak.SenderID, '========================') self.assertEqual(pak.Message, None) self.assertEqual(pak.Data, b'') self.assertEqual(pak.TransmitTime, 0.0) self.assertEqual(pak.RoundTripEstimate, 0.0) self.assertEqual(pak.DestinationID, '========================') self.assertEqual(pak.Identifier, None)
def test_pack_unpack(self): # Test packing a paket and a packed packet can be unpacked correctly pak = Packet() data = "test the pack" pak.Data = data # Store original attributes original = [pak.PackedFormat, pak.TimeToLive, pak.SequenceNumber, pak.IsAcknowledgement, pak.IsReliable, str(pak.SenderID)] packed = pak.pack() # Change the packet to see if it will be returned to original pak.SequenceNumber = 1234 pak.unpack(packed) # Store the attrubites of the unpacked pack new = [pak.PackedFormat, pak.TimeToLive, pak.SequenceNumber, pak.IsAcknowledgement, pak.IsReliable, str(pak.SenderID)] self.assertEquals(original, new) self.assertEquals("test the pack", pak.Data)
def test_pack_unpack(self): # Test packing a paket and a packed packet can be unpacked correctly pak = Packet() data = "test the pack" pak.Data = data # Store original attributes original = [ pak.PackedFormat, pak.TimeToLive, pak.SequenceNumber, pak.IsAcknowledgement, pak.IsReliable, str(pak.SenderID) ] packed = pak.pack() # Change the packet to see if it will be returned to original pak.SequenceNumber = 1234 pak.unpack(packed) # Store the attrubites of the unpacked pack new = [ pak.PackedFormat, pak.TimeToLive, pak.SequenceNumber, pak.IsAcknowledgement, pak.IsReliable, str(pak.SenderID) ] self.assertEqual(original, new) self.assertEqual("test the pack", pak.Data.decode())
def test_packet_str(self): # Test string method pak = Packet() string = pak.__str__() self.assertEquals(string, "PKT:{0}:{1}".format(pak.SenderID[:8], pak.SequenceNumber))
def test_packet_str(self): # Test string method pak = Packet() string = pak.__str__() self.assertEqual( string, "PKT:{0}:{1}".format(pak.SenderID[:8], pak.SequenceNumber))