Esempio n. 1
0
class TestKiddieConnection(unittest.TestCase):
    def setUp(self):
        super(TestKiddieConnection, self).setUp()
        self.conn = KiddieConnection()

    def _patch_recv(self):
        recv_patch = patch.object(self.conn, 'recv')
        self.addCleanup(recv_patch.stop)

        return recv_patch.start()

    def test_simple_recvall(self):
        recv_mock = self._patch_recv()
        recv_mock.return_value = b'123'

        data = self.conn.recvall(3)
        self.assertEqual(b'123', data)
        recv_mock.assert_called_with(3)

    def test_multi_read_recvall(self):
        recv_mock = self._patch_recv()
        recv_mock.side_effect = [
            b'123',
            b'456',
            b'789',
            b'0',
        ]

        data = self.conn.recvall(10)
        self.assertEqual(b'1234567890', data)
        self.assertEqual(recv_mock.call_args_list,
                         [call(10), call(7), call(4), call(1)])

    def test_failed_recvall(self):
        recv_mock = self._patch_recv()
        recv_mock.side_effect = [
            b'123',
            b'456',
            b'789',
            b'',
        ]

        with self.assertRaises(KiddieConnectionRecvFailure):
            self.conn.recvall(10)

    def test_broken_pipe(self):
        recv_mock = self._patch_recv()
        recv_mock.side_effect = socket.error(socket.errno.EPIPE,
                                             'Broken pipe')

        with self.assertRaises(KiddieConnectionRecvFailure):
            self.conn.recvall(10)

    def test_socket_error_conversion_to_kiddiepool_socket_error(self):
        arbitrary_size = 10
        arbitrary_flags = 0

        with patch.object(self.conn, 'socket') as socket_mock:
            socket_mock.recv = Mock(side_effect=socket.error)

            with self.assertRaises(KiddieConnectionRecvFailure):
                self.conn.recv(arbitrary_size, arbitrary_flags)

    @patch.object(socket, 'create_connection')
    def test_connection_valid(self, mock_conn):
        # Set max idle time to absurd number and remove lifetime, if any
        self.conn = KiddieConnection(max_idle=999, lifetime=None)

        self.conn.connect('lol', 643)

        # Make sure connection is valid
        self.assertTrue(self.conn.validate())

        self.assertEqual(mock_conn.call_count, 1)
        args, kwargs = mock_conn.call_args
        self.assertEqual(args, (('lol', 643),))

    @patch.object(socket, 'create_connection')
    def test_max_idle(self, _):
        # Set max idle time to 0 and remove lifetime, if any
        self.conn = KiddieConnection(max_idle=0, lifetime=None)

        self.conn.connect('foo', 123)

        # Make sure we invalidate a connection immediately
        self.assertFalse(self.conn.validate())

    @patch.object(socket, 'create_connection')
    def test_connection_end_of_life(self, _):
        # Set lifetime to 0 and make max_idle absurdly large
        self.conn = KiddieConnection(max_idle=999, lifetime=0)

        self.conn.connect('bar', 321)

        # Make sure we invalidate a connection immediately
        self.assertFalse(self.conn.validate())

    @patch.object(socket, 'create_connection')
    def test_timeout(self, mock_conn):
        # Set timeout to 987 in instantiation of KiddieConnection
        self.conn = KiddieConnection(timeout=987)

        self.conn.connect('baz', 222)

        self.assertEqual(mock_conn.call_count, 1)
        self.assertEqual(mock_conn.call_args, call(('baz', 222), timeout=987))
Esempio n. 2
0
class TestKiddieConnection(mimic.MimicTestBase):
    def setUp(self):
        super(TestKiddieConnection, self).setUp()
        self.conn = KiddieConnection()

    def test_simple_recvall(self):
        self.mimic.stub_out_with_mock(self.conn, 'recv')
        self.conn.recv(3).and_return('123')

        self.mimic.replay_all()

        data = self.conn.recvall(3)
        self.assertEqual('123', data)

    def test_multi_read_recvall(self):
        self.mimic.stub_out_with_mock(self.conn, 'recv')
        self.conn.recv(10).and_return('123')
        self.conn.recv(7).and_return('456')
        self.conn.recv(4).and_return('789')
        self.conn.recv(1).and_return('0')

        self.mimic.replay_all()

        data = self.conn.recvall(10)
        self.assertEqual('1234567890', data)

    def test_failed_recvall(self):
        self.mimic.stub_out_with_mock(self.conn, 'recv')
        self.conn.recv(10).and_return('123')
        self.conn.recv(7).and_return('456')
        self.conn.recv(4).and_return('789')
        self.conn.recv(1).and_return('')

        self.mimic.replay_all()

        self.assertRaises(
            KiddieConnectionRecvFailure,
            self.conn.recvall,
            10
        )

    def test_broken_pipe(self):
        self.mimic.stub_out_with_mock(self.conn, 'recv')
        self.conn.recv(10)\
            .and_raise(socket.error(socket.errno.EPIPE, 'Broken pipe'))

        self.mimic.replay_all()

        self.assertRaises(
            KiddieConnectionRecvFailure,
            self.conn.recvall,
            10
        )

    def test_socket_error_conversion_to_kiddiepool_socket_error(self):
        arbitrary_size = 10
        arbitrary_flags = 0

        self.conn.socket = self.mimic.create_mock_anything()
        self.conn.socket.recv(arbitrary_size, arbitrary_flags).and_raise(
            socket.error
        )

        self.mimic.replay_all()

        self.assertRaises(
            KiddieConnectionRecvFailure,
            self.conn.recv,
            arbitrary_size,
            arbitrary_flags,
        )

    def test_connection_valid(self):
        mock_socket = self.mimic.create_mock_anything()
        mock_socket.setsockopt(
            mimic.IgnoreArg(), mimic.IgnoreArg(), mimic.IgnoreArg()
        )
        self.mimic.stub_out_with_mock(socket, 'create_connection')
        socket.create_connection(
            ('lol', 643), timeout=mimic.IgnoreArg()
        ).AndReturn(mock_socket)

        # Set max idle time to absurd number and remove lifetime, if any
        self.conn = KiddieConnection(max_idle=999, lifetime=None)

        self.mimic.replay_all()

        self.conn.connect('lol', 643)

        # Make sure connection is valid
        self.assertTrue(self.conn.validate())

    def test_max_idle(self):
        mock_socket = self.mimic.create_mock_anything()
        mock_socket.setsockopt(
            mimic.IgnoreArg(), mimic.IgnoreArg(), mimic.IgnoreArg()
        )
        self.mimic.stub_out_with_mock(socket, 'create_connection')
        socket.create_connection(
            ('foo', 123), timeout=mimic.IgnoreArg()
        ).AndReturn(mock_socket)

        # Set max idle time to 0 and remove lifetime, if any
        self.conn = KiddieConnection(max_idle=0, lifetime=None)

        self.mimic.replay_all()

        self.conn.connect('foo', 123)

        # Make sure we invalidate a connection immediately
        self.assertFalse(self.conn.validate())

    def test_connection_end_of_life(self):
        mock_socket = self.mimic.create_mock_anything()
        mock_socket.setsockopt(
            mimic.IgnoreArg(), mimic.IgnoreArg(), mimic.IgnoreArg()
        )
        self.mimic.stub_out_with_mock(socket, 'create_connection')
        socket.create_connection(
            ('bar', 321), timeout=mimic.IgnoreArg()
        ).AndReturn(mock_socket)

        # Set lifetime to 0 and make max_idle absurdly large
        self.conn = KiddieConnection(max_idle=999, lifetime=0)

        self.mimic.replay_all()

        self.conn.connect('bar', 321)

        # Make sure we invalidate a connection immediately
        self.assertFalse(self.conn.validate())

    def test_timeout(self):
        mock_socket = self.mimic.create_mock_anything()
        mock_socket.setsockopt(
            mimic.IgnoreArg(), mimic.IgnoreArg(), mimic.IgnoreArg()
        )
        self.mimic.stub_out_with_mock(socket, 'create_connection')

        # Set expectation that 987 is passed in at socket creation time
        socket.create_connection(
            ('baz', 222), timeout=987
        ).AndReturn(mock_socket)

        # Set timeout to 987 in instantiation of KiddieConnection
        self.conn = KiddieConnection(timeout=987)

        self.mimic.replay_all()

        self.conn.connect('baz', 222)