예제 #1
0
    def test_init(self, mock_init):
        manager = udp.UDPTendrilManager()

        mock_init.assert_called_once_with(None)
        self.assertEqual(manager._sock, None)
        self.assertIsInstance(manager._sock_event, event.Event)
        self.assertEqual(manager._sock_event.is_set(), False)
예제 #2
0
    def test_listener_creator_recv_bufsize(self, mock_UDPTendril,
                                           mock_track_tendril, mock_socket):
        acceptor = mock.Mock()
        manager = udp.UDPTendrilManager()
        manager.running = True

        with self.assertRaises(TestException):
            manager.listener(acceptor, None)

        mock_socket.assert_called_once_with(socket.AF_INET, socket.SOCK_DGRAM)
        mock_socket.return_value.assert_has_calls([
            mock.call.bind(('', 0)),
            mock.call.getsockname(),
            mock.call.recvfrom(8192),
            mock.call.recvfrom(8192),
            mock.call.recvfrom(8192),
            mock.call.recvfrom(8192),
            mock.call.recvfrom(8192),
            mock.call.recvfrom(8192),
            mock.call.recvfrom(8192),
            mock.call.recvfrom(8192),
            mock.call.recvfrom(8192),
            mock.call.recvfrom(8192),
            mock.call.recvfrom(8192),
            mock.call.close(),
        ])
        self.assertEqual(manager.local_addr, ('127.0.0.1', 8080))
        self.assertFalse(mock_UDPTendril.called)
        self.assertFalse(acceptor.called)
        self.assertFalse(mock_track_tendril.called)
예제 #3
0
    def test_sock_getter_notrunning(self):
        manager = udp.UDPTendrilManager()
        manager._sock = 'socket'
        manager._sock_event = mock.Mock()

        result = manager.sock

        self.assertEqual(result, None)
        self.assertFalse(manager._sock_event.wait.called)
예제 #4
0
    def test_start(self, mock_start):
        manager = udp.UDPTendrilManager()
        manager._sock = 'sock'
        manager._sock_event = mock.Mock()

        manager.start('acceptor', 'wrapper')

        mock_start.assert_called_once_with('acceptor', 'wrapper')
        self.assertEqual(manager._sock, None)
        manager._sock_event.clear.assert_called_once_with()
예제 #5
0
    def test_sock_getter(self):
        manager = udp.UDPTendrilManager()
        manager.running = True
        manager._sock = 'socket'
        manager._sock_event = mock.Mock()

        result = manager.sock

        self.assertEqual(result, 'socket')
        manager._sock_event.wait.assert_called_once_with()
예제 #6
0
    def test_shutdown(self, mock_shutdown):
        manager = udp.UDPTendrilManager()
        manager._sock = 'sock'
        manager._sock_event = mock.Mock()

        manager.shutdown()

        mock_shutdown.assert_called_once_with()
        self.assertEqual(manager._sock, None)
        manager._sock_event.clear.assert_called_once_with()
예제 #7
0
    def test_stop(self, mock_stop):
        manager = udp.UDPTendrilManager()
        manager._sock = 'sock'
        manager._sock_event = mock.Mock()

        manager.stop('thread')

        mock_stop.assert_called_once_with('thread')
        self.assertEqual(manager._sock, None)
        manager._sock_event.clear.assert_called_once_with()
예제 #8
0
    def test_listener_recv_frameify_error(self, mock_UDPTendril,
                                          mock_track_tendril, mock_socket):
        mock_socket.return_value.recvfrom.side_effect = [
            ('frame', ('127.0.0.3', 8083)),
            TestException(),
            TestException(),
            TestException(),
            TestException(),
            TestException(),
            TestException(),
            TestException(),
            TestException(),
            TestException(),
            TestException(),
            TestException(),
        ]
        tend = mock.Mock(**{'_recv_frameify.side_effect': TestException()})
        manager = udp.UDPTendrilManager()
        manager.running = True
        manager.tendrils[(('127.0.0.1', 8080), ('127.0.0.3', 8083))] = tend

        with self.assertRaises(TestException):
            manager.listener(None, None)

        mock_socket.assert_called_once_with(socket.AF_INET, socket.SOCK_DGRAM)
        mock_socket.return_value.assert_has_calls([
            mock.call.bind(('', 0)),
            mock.call.getsockname(),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.close(),
        ])
        self.assertEqual(manager.local_addr, ('127.0.0.1', 8080))
        self.assertFalse(mock_UDPTendril.called)
        self.assertFalse(mock_track_tendril.called)
        tend._recv_frameify.assert_called_once_with('frame')
        tend.close.assert_called_once_with()
        self.assertEqual(tend.closed.call_count, 1)

        args = tend.closed.call_args[0]
        self.assertEqual(len(args), 1)
        self.assertIsInstance(args[0], TestException)
예제 #9
0
    def test_listener_noacceptor(self, mock_UDPTendril, mock_track_tendril,
                                 mock_socket):
        msgs = ['msg1', 'msg2', 'msg3']
        mock_socket.return_value.recvfrom.side_effect = [
            (msgs[0], ('127.0.0.2', 8082)),
            (msgs[1], ('127.0.0.3', 8083)),
            (msgs[2], ('127.0.0.4', 8084)),
            TestException(),
            TestException(),
            TestException(),
            TestException(),
            TestException(),
            TestException(),
            TestException(),
            TestException(),
            TestException(),
            TestException(),
            TestException(),
        ]
        tend = mock.Mock()
        manager = udp.UDPTendrilManager()
        manager.running = True
        manager.tendrils[(('127.0.0.1', 8080), ('127.0.0.3', 8083))] = tend

        with self.assertRaises(TestException):
            manager.listener(None, None)

        mock_socket.assert_called_once_with(socket.AF_INET, socket.SOCK_DGRAM)
        mock_socket.return_value.assert_has_calls([
            mock.call.bind(('', 0)),
            mock.call.getsockname(),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.close(),
        ])
        self.assertEqual(manager.local_addr, ('127.0.0.1', 8080))
        self.assertFalse(mock_UDPTendril.called)
        self.assertFalse(mock_track_tendril.called)
        tend._recv_frameify.assert_called_once_with('msg2')
예제 #10
0
    def test_connect_rejected(self, mock_UDPTendril, mock_track_tendril,
                              mock_connect):
        acceptor = mock.Mock(side_effect=application.RejectConnection())
        manager = udp.UDPTendrilManager()

        tend = manager.connect(('127.0.0.1', 8080), acceptor)

        mock_connect.assert_called_once_with(('127.0.0.1', 8080), acceptor,
                                             None)
        mock_UDPTendril.assert_called_once_with(manager, ('0.0.0.0', 8880),
                                                ('127.0.0.1', 8080))
        acceptor.assert_called_once_with(mock_UDPTendril.return_value)
        self.assertFalse(mock_track_tendril.called)
        self.assertEqual(tend, None)
예제 #11
0
    def test_connect(self, mock_UDPTendril, mock_track_tendril, mock_connect):
        acceptor = mock.Mock()
        manager = udp.UDPTendrilManager()

        tend = manager.connect(('127.0.0.1', 8080), acceptor)

        mock_connect.assert_called_once_with(('127.0.0.1', 8080), acceptor,
                                             None)
        mock_UDPTendril.assert_called_once_with(manager, ('0.0.0.0', 8880),
                                                ('127.0.0.1', 8080))
        acceptor.assert_called_once_with(mock_UDPTendril.return_value)
        mock_track_tendril.assert_called_once_with(
            mock_UDPTendril.return_value)
        self.assertEqual(id(tend), id(mock_UDPTendril.return_value))
예제 #12
0
    def test_listener_wrapper(self, mock_UDPTendril, mock_track_tendril,
                              mock_socket):
        wrapped_sock = mock.Mock(**{
            'recvfrom.side_effect': TestException(),
        })
        wrapper = mock.Mock(return_value=wrapped_sock)
        acceptor = mock.Mock()
        manager = udp.UDPTendrilManager()
        manager.running = True

        with self.assertRaises(TestException):
            manager.listener(acceptor, wrapper)

        mock_socket.assert_called_once_with(socket.AF_INET, socket.SOCK_DGRAM)
        mock_socket.return_value.assert_has_calls([
            mock.call.bind(('', 0)),
            mock.call.getsockname(),
        ])
        wrapper.assert_called_once_with(mock_socket.return_value)
        wrapped_sock.assert_has_calls([
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.close(),
        ])
        self.assertEqual(manager.local_addr, ('127.0.0.1', 8080))
        self.assertFalse(mock_UDPTendril.called)
        self.assertFalse(acceptor.called)
        self.assertFalse(mock_track_tendril.called)
예제 #13
0
    def test_listener_rejector(self, mock_UDPTendril, mock_track_tendril,
                               mock_socket):
        mock_socket.return_value.recvfrom.side_effect = [
            ('msg1', ('127.0.0.2', 8082)),
            ('msg2', ('127.0.0.3', 8083)),
            ('msg3', ('127.0.0.4', 8084)),
            TestException(),
            TestException(),
            TestException(),
            TestException(),
            TestException(),
            TestException(),
            TestException(),
            TestException(),
            TestException(),
            TestException(),
            TestException(),
        ]
        tendrils = [mock.Mock(), mock.Mock(), mock.Mock()]
        mock_UDPTendril.side_effect = tendrils[:]
        acceptor = mock.Mock(side_effect=application.RejectConnection())
        manager = udp.UDPTendrilManager()
        manager.running = True

        with self.assertRaises(TestException):
            manager.listener(acceptor, None)

        mock_socket.assert_called_once_with(socket.AF_INET, socket.SOCK_DGRAM)
        mock_socket.return_value.assert_has_calls([
            mock.call.bind(('', 0)),
            mock.call.getsockname(),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.recvfrom(4096),
            mock.call.close(),
        ])
        self.assertEqual(manager.local_addr, ('127.0.0.1', 8080))
        mock_UDPTendril.assert_has_calls([
            mock.call(manager, ('127.0.0.1', 8080), ('127.0.0.2', 8082)),
            mock.call(manager, ('127.0.0.1', 8080), ('127.0.0.3', 8083)),
            mock.call(manager, ('127.0.0.1', 8080), ('127.0.0.4', 8084)),
        ])
        acceptor.assert_has_calls([
            mock.call(tendrils[0]),
            mock.call(tendrils[1]),
            mock.call(tendrils[2]),
        ])
        self.assertFalse(mock_track_tendril.called)
        self.assertFalse(tendrils[0]._recv_frameify.called)
        self.assertFalse(tendrils[1]._recv_frameify.called)
        self.assertFalse(tendrils[2]._recv_frameify.called)