Ejemplo n.º 1
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()
Ejemplo n.º 2
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()
Ejemplo n.º 3
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()
Ejemplo n.º 4
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()
Ejemplo n.º 5
0
    def test_mock_listen(self, mock1):
        l = str(list(range(20))).encode('utf-8')
        d = struct.pack('H', 0)
        d += d
        d += 'test'.encode('utf-8')
        d += l
        mock1.return_value = (d, ('127.0.0.1', 9071))
        comm1 = UDPCommunicator(9071)
        comm1.listen()
        self.assertNotEqual(comm1.listen_thread, None)
        self.assertEqual(comm1.is_listening, True)
        comm1.receive = MagicMock()
        comm1.send('127.0.0.1', l, 'test'.encode('utf-8'))

        # Give some time for the other thread to run before checking conditions
        ctr = 0
        while mock1.called != True and comm1.receive.called != True and ctr < 20:
            time.sleep(0.1)

        mock1.assert_called_with(2048)
        comm1.close()
        comm1.receive.assert_called_with(d, '127.0.0.1')