Beispiel #1
0
    def test_create_large(self):
        '''This test helped to fix a bug where we were accidentally appending an extra blank packet
        when creating packets'''
        comm1 = UDPCommunicator(10001)
        l = str(list(range(550))).encode('utf-8')
        packets = comm1.create_packets(l, '_get'.encode('utf-8'))
        self.assertEqual(len(packets), 6)

        comm1.close()
Beispiel #2
0
 def test_packet_create_single(self):
     l = []
     for i in range(100):
         l.append(i)
     comm1 = UDPCommunicator(9090)
     l = str(l).encode('utf-8')
     p1 = comm1.create_packets(l, '9012'.encode('utf-8'))
     self.assertEqual(len(p1), 1)
     p1 = p1[0]
     comm1.close()
Beispiel #3
0
 def test_get(self):
     '''Encodes and decodes a single packet.'''
     comm1 = UDPCommunicator(10001)
     l = str(list(range(100))).encode('utf-8')
     for packet in comm1.create_packets(l, '_get'.encode('utf-8')):
         comm1.receive(packet, 'test')
     r = comm1.get('test', '_get'.encode('utf-8'))
     self.assertNotEqual(r, None)
     self.assertEqual(l, r, 'Reassembled bytes should be the same.')
     comm1.close()
Beispiel #4
0
 def test_large_get(self):
     '''This test assures that packets split into multiple pieces and received are able to be
     reassembled corectly.'''
     comm1 = UDPCommunicator(10001)
     l = str(list(range(1000))).encode('utf-8')
     packets = comm1.create_packets(l, '_get'.encode('utf-8'))
     for packet in packets:
         comm1.receive(packet, 'test')
     r = comm1.get('test', '_get'.encode('utf-8'))
     self.assertNotEqual(r, None)
     self.assertEqual(l, r, 'Reassembled bytes should be the same.')
     comm1.close()
Beispiel #5
0
 def test_single_packet(self):
     comm1 = UDPCommunicator(8080)
     d = []
     for i in range(122):  # Exactly 500 bytes
         d.append(i)
     d = str(d).encode('utf-8')
     d = comm1.create_packets(d, '1111'.encode('utf-8'))
     self.assertEqual(len(d), 1, 'Should have only created a single packet')
     self.assertEqual('1111'.encode('utf-8'), d[0][4:8])
     self.assertEqual(0, struct.unpack('H', d[0][0:2])[0])
     self.assertEqual(0, struct.unpack('H', d[0][2:4])[0])
     comm1.close()
Beispiel #6
0
    def test_multi_get(self):

        comm1 = UDPCommunicator(9071)
        s = bytes(str(range(1000)).encode('utf-8'))
        pkts = comm1.create_packets(s, 'tg11'.encode('utf-8'))
        for pkt in pkts:
            comm1.receive(pkt, 'local')
        s2 = comm1.get('local', 'tg11'.encode('utf-8'))
        self.assertEqual(s, s2, "Bytes should be able to be retrieved")
        s2 = comm1.get('local', 'tg11'.encode('utf-8'))
        self.assertNotEqual(
            s2, s, "Should not be able to retrieve the same data again")
        comm1.close()
Beispiel #7
0
    def test_same_tag_send(self):

        comm1 = UDPCommunicator(9071)
        for i in range(10):
            msg = 'Iteration: {}'.format(i).encode('utf-8')
            packets = comm1.create_packets(msg, 'test'.encode('utf-8'))
            for packet in packets:
                comm1.receive(packet, 'local')
            self.assertEqual(
                comm1.get('local', 'test'.encode('utf-8')).decode('utf-8'),
                msg.decode('utf-8'))

        comm1.close()
Beispiel #8
0
    def test_large_packet(self):
        comm1 = UDPCommunicator(10001)
        d = []
        for i in range(1000):
            d.append(random.random())
        d = str(d).encode('utf-8')
        packs = comm1.create_packets(d, 'tag1'.encode('utf-8'))
        r = bytes()  # total data bytes
        t = bytes()
        for packet in packs:
            r += packet[8:]
            t += packet

        self.assertEqual(len(d), len(r))
        self.assertEqual(len(d), len(t) - 8 * len(packs))
        for i in range(len(packs)):
            seq = struct.unpack('H', packs[i][2:4])[0]
            t = struct.unpack('H', packs[i][0:2])[0]
            self.assertEqual(seq, i)
            self.assertEqual(t, len(packs) - 1)
        comm1.close()