示例#1
0
    def test_close(self):
        comm1 = UDPCommunicator(9887)
        self.assertEqual(comm1.is_listening, False)

        comm1.close()
        self.assertEqual(comm1.is_listening, False)
        self.assertEqual(comm1.listen_sock, None)
        self.assertEqual(comm1.send_sock, None)
示例#2
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()
示例#3
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()
示例#4
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()
示例#5
0
 def test_big_mock_send(self, mock1):
     list_bytes = str(list(range(1000))).encode('utf-8')
     comm1 = UDPCommunicator(10001)
     self.assertEqual(
         comm1.send('abcomm1213', list_bytes, 'big_'.encode('utf-8')), True)
     mock1.return_value = -1
     self.assertEqual(
         comm1.send('abcomm1213', list_bytes, 'big_'.encode('utf-8')),
         False)
     comm1.close()
示例#6
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()
示例#7
0
 def test_mocked_send(self, mock1):
     comm1 = UDPCommunicator(10001)
     msg = 'ayyyy'.encode('utf-8')
     self.assertEqual(
         comm1.send('192.168.1.1', msg, 'noice'.encode('utf-8')), True)
     self.assertEqual(
         comm1.send('192.168.1.1', msg, 'noice'.encode('utf-8')), False)
     self.assertEqual(
         comm1.send('192.168.1.1', msg, 'noice'.encode('utf-8')), True)
     self.assertEqual(
         comm1.send('192.168.1.1', msg, 'noice'.encode('utf-8')), False)
     comm1.close()
示例#8
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()
示例#9
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()
示例#10
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()
示例#11
0
    def test_register_callback(self):
        def cbk(a, b, c):
            return "callback"

        def bck(a, b):
            return "bad"

        comm1 = UDPCommunicator(9071)
        comm1.register_recv_callback(cbk)

        # Should raise error on non-function
        with self.assertRaises(TypeError):
            comm1.register_recv_callback("a")

        # Should raise error on bad function signature
        with self.assertRaises(ValueError):
            comm1.register_recv_callback(bck)

        comm1.close()
示例#12
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()
示例#13
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')
示例#14
0
 def test_double_close(self):
     comm1 = UDPCommunicator(9090)
     comm1.listen()
     comm1.close()
     comm1.close()
示例#15
0
 def test_init(self):
     comm1 = UDPCommunicator(9887)
     self.assertEqual(comm1.port, 9887)
     self.assertEqual(comm1.is_listening, False)
     self.assertEqual(comm1.listen_thread, None)
     comm1.close()
示例#16
0
 def test_close_ops(self):
     comm1 = UDPCommunicator(80)
     comm1.close()
     comm1.close()