Exemple #1
0
    def test_send(self):
        self.sock.send.side_effect = [7, 7]
        tend = tcp.TCPTendril('manager', self.sock)
        self.sock.reset_mock()
        tend._sendbuf = 'frame 1frame 2'
        tend._send_lock = mock.Mock()
        tend._sendbuf_event = mock.Mock(
            **{
                'clear.side_effect': gevent.GreenletExit,
            })

        with self.assertRaises(gevent.GreenletExit):
            tend._send()

        self.assertEqual(tend._send_lock.method_calls, [
            mock.call.release(),
            mock.call.acquire(),
        ])
        self.assertEqual(tend._sendbuf_event.method_calls, [
            mock.call.wait(),
            mock.call.clear(),
        ])
        self.assertEqual(tend._sendbuf, '')
        self.sock.send.assert_has_calls(
            [mock.call('frame 1frame 2'),
             mock.call('frame 2')])
Exemple #2
0
    def test_close_nothreads_nosock(self, mock_close):
        tend = tcp.TCPTendril('manager', self.sock)
        tend._sock = None

        tend.close()

        mock_close.assert_called_once_with()
Exemple #3
0
    def test_recv_altbufsize(self, mock_sleep, mock_closed, mock_recv_frameify,
                             mock_close):
        self.sock.recv.side_effect = ['frame 1', 'frame 2', '']
        tend = tcp.TCPTendril('manager', self.sock)
        tend.recv_bufsize = 1024
        tend._recv_lock = mock.Mock()
        send_thread = mock.Mock()
        tend._send_thread = send_thread

        with self.assertRaises(gevent.GreenletExit):
            tend._recv()

        self.assertEqual(tend._recv_lock.method_calls, [
            mock.call.release(),
            mock.call.acquire(),
            mock.call.release(),
            mock.call.acquire(),
            mock.call.release(),
            mock.call.acquire(),
        ])
        mock_sleep.assert_has_calls([mock.call(), mock.call(), mock.call()])
        self.sock.recv.assert_has_calls(
            [mock.call(1024),
             mock.call(1024),
             mock.call(1024)])
        send_thread.kill.assert_called_once_with()
        self.assertEqual(tend._send_thread, None)
        self.sock.close.assert_called_once_with()
        self.assertEqual(tend._sock, None)
        mock_close.assert_called_once_with()
        mock_closed.assert_called_once_with()
        mock_recv_frameify.assert_has_calls(
            [mock.call('frame 1'), mock.call('frame 2')])
Exemple #4
0
    def test_close_nothreads(self, mock_close):
        tend = tcp.TCPTendril('manager', self.sock)

        tend.close()

        mock_close.assert_called_once_with()
        self.assertEqual(tend._sock, None)
        self.sock.close.assert_called_once_with()
Exemple #5
0
    def test_send_frame(self, mock_send_streamify):
        tend = tcp.TCPTendril('manager', self.sock)
        tend._sendbuf_event = mock.Mock()

        tend.send_frame('a frame')

        mock_send_streamify.assert_called_once_with('a frame')
        tend._sendbuf_event.assert_has_calls([mock.call.set()])
Exemple #6
0
    def test_thread_error_recvthread(self, _mock_closed, _mock_close):
        thread = mock.Mock(**{'successful.return_value': True})
        tend = tcp.TCPTendril('manager', self.sock)
        tend._send_thread = mock.Mock()
        tend._recv_thread = thread

        tend._thread_error(thread)

        self.assertNotEqual(tend._send_thread, None)
        self.assertEqual(tend._recv_thread, None)
Exemple #7
0
    def test_wrap_nolock(self):
        wrapper = mock.Mock()
        tend = tcp.TCPTendril('manager', self.sock)
        tend._recv_lock = mock.Mock()
        tend._send_lock = mock.Mock()

        tend.wrap(wrapper)

        self.assertNotEqual(id(tend._sock), id(self.sock))
        wrapper.assert_called_once_with(self.sock)
        self.assertEqual(tend._recv_lock.mock_calls, [])
        self.assertEqual(tend._send_lock.mock_calls, [])
Exemple #8
0
    def test_init_noremote(self):
        tend = tcp.TCPTendril('manager', self.sock)

        self.assertEqual(tend.local_addr, ('127.0.0.1', 8080))
        self.assertEqual(tend.remote_addr, ('127.0.0.2', 8880))
        self.assertIsInstance(tend._recv_framer, framers.LineFramer)
        self.assertIsInstance(tend._send_framer, framers.LineFramer)
        self.assertEqual(id(tend._sock), id(self.sock))
        self.assertIsInstance(tend._sendbuf_event, event.Event)
        self.assertEqual(tend._sendbuf, '')
        self.assertEqual(tend._recv_thread, None)
        self.assertEqual(tend._send_thread, None)
        self.assertEqual(tend._recv_lock, None)
        self.assertEqual(tend._send_lock, None)
        self.sock.getsockname.assert_called_once_with()
        self.sock.getpeername.assert_called_once_with()
Exemple #9
0
    def test_close_threads(self, mock_close):
        tend = tcp.TCPTendril('manager', self.sock)
        recv_thread = mock.Mock()
        tend._recv_thread = recv_thread
        send_thread = mock.Mock()
        tend._send_thread = send_thread

        tend.close()

        mock_close.assert_called_once_with()
        self.assertEqual(tend._sock, None)
        self.assertEqual(tend._recv_thread, None)
        self.assertEqual(tend._send_thread, None)
        self.sock.close.assert_called_once_with()
        recv_thread.kill.assert_called_once_with()
        send_thread.kill.assert_called_once_with()
Exemple #10
0
    def test_wrap_withlock(self):
        wrapper = mock.Mock()
        tend = tcp.TCPTendril('manager', self.sock)
        tend._recv_thread = mock.Mock()
        tend._send_thread = mock.Mock()
        tend._recv_lock = mock.Mock()
        tend._send_lock = mock.Mock()

        tend.wrap(wrapper)

        self.assertNotEqual(id(tend._sock), id(self.sock))
        wrapper.assert_called_once_with(self.sock)
        tend._recv_lock.assert_has_calls(
            [mock.call.acquire(), mock.call.release()])
        tend._send_lock.assert_has_calls(
            [mock.call.acquire(), mock.call.release()])
Exemple #11
0
    def test_start(self, mock_spawn):
        recv_thread = mock.Mock()
        send_thread = mock.Mock()
        mock_spawn.side_effect = [recv_thread, send_thread]

        tend = tcp.TCPTendril('manager', self.sock)
        tend._start()

        self.assertIsInstance(tend._recv_lock, coros.Semaphore)
        self.assertIsInstance(tend._send_lock, coros.Semaphore)
        mock_spawn.assert_has_calls(
            [mock.call(tend._recv),
             mock.call(tend._send)])
        self.assertEqual(id(tend._recv_thread), id(recv_thread))
        self.assertEqual(id(tend._send_thread), id(send_thread))
        recv_thread.link.assert_called_once_with(tend._thread_error)
        send_thread.link.assert_called_once_with(tend._thread_error)
Exemple #12
0
    def test_thread_error_greenletexit(self, mock_closed, mock_close):
        thread = mock.Mock(**{
            'successful.return_value': False,
            'exception': gevent.GreenletExit(),
        })
        tend = tcp.TCPTendril('manager', self.sock)
        tend._send_thread = mock.Mock()
        tend._recv_thread = mock.Mock()

        tend._thread_error(thread)

        mock_close.assert_called_once_with()
        self.assertFalse(mock_closed.called)

        # Ensure we didn't overwrite the threads
        self.assertNotEqual(tend._send_thread, None)
        self.assertNotEqual(tend._recv_thread, None)
Exemple #13
0
    def test_thread_error_successful(self, mock_closed, mock_close):
        thread = mock.Mock(**{'successful.return_value': True})
        tend = tcp.TCPTendril('manager', self.sock)
        tend._send_thread = mock.Mock()
        tend._recv_thread = mock.Mock()

        tend._thread_error(thread)

        mock_close.assert_called_once_with()
        self.assertEqual(mock_closed.call_count, 1)

        args = mock_closed.call_args[0]
        self.assertEqual(len(args), 1)
        self.assertIsInstance(args[0], socket.error)
        self.assertEqual(args[0][0], 'thread exited prematurely')

        # Ensure we didn't overwrite the threads
        self.assertNotEqual(tend._send_thread, None)
        self.assertNotEqual(tend._recv_thread, None)
Exemple #14
0
    def test_thread_error_exception(self, mock_closed, mock_close):
        thread = mock.Mock(**{
            'successful.return_value': False,
            'exception': TestException(),
        })
        tend = tcp.TCPTendril('manager', self.sock)
        tend._send_thread = mock.Mock()
        tend._recv_thread = mock.Mock()

        tend._thread_error(thread)

        mock_close.assert_called_once_with()
        self.assertEqual(mock_closed.call_count, 1)

        args = mock_closed.call_args[0]
        self.assertEqual(len(args), 1)
        self.assertEqual(id(args[0]), id(thread.exception))

        # Ensure we didn't overwrite the threads
        self.assertNotEqual(tend._send_thread, None)
        self.assertNotEqual(tend._recv_thread, None)
Exemple #15
0
    def test_sock(self):
        tend = tcp.TCPTendril('manager', self.sock)

        self.assertEqual(id(tend.sock), id(self.sock))