예제 #1
0
    def test_cleanup_resolved_hosts_on_500(self, m_request):
        c = HttpClient(
            [('localhost', 56777), ('localhost', 56778)],
            conn_timeout=0.0001, loop=self.loop)

        called = False

        class M:

            def clear(self):
                nonlocal called
                called = True

            def __contains__(self, key):
                return True

            def __getitem__(self, key):
                return 'localhost'

        c._connector._resolved_hosts = M()

        resp = unittest.mock.Mock()
        resp.status = 500

        m_request.return_value = asyncio.Future(loop=self.loop)
        m_request.return_value.set_result(resp)

        self.loop.run_until_complete(c.request('get', path='/'))
        self.assertTrue(called)
예제 #2
0
    def test_failed_request_conn(self):
        c = HttpClient([('localhost', 56777), ('localhost', 56778)],
                       loop=self.loop)

        self.assertRaises(aiohttp.ConnectionError,
                          self.loop.run_until_complete,
                          c.request('get', path='/', conn_timeout=0.0001))
예제 #3
0
    def test_cleanup_resolved_hosts_on_500(self, m_request):
        c = HttpClient(
            [('localhost', 56777), ('localhost', 56778)],
            conn_timeout=0.0001, loop=self.loop)

        called = False

        class m:

            def clear(self):
                nonlocal called
                called = True

            def __contains__(self, key):
                return True

            def __getitem__(self, key):
                return 'localhost'

        c._connector._resolved_hosts = m()

        resp = unittest.mock.Mock()
        resp.status = 500

        m_request.return_value = asyncio.Future(loop=self.loop)
        m_request.return_value.set_result(resp)

        self.loop.run_until_complete(c.request('get', path='/'))
        self.assertTrue(called)
예제 #4
0
    def test_failed_request_conn(self):
        c = HttpClient(
            [('localhost', 56777), ('localhost', 56778)], loop=self.loop)

        self.assertRaises(
            aiohttp.ConnectionError,
            self.loop.run_until_complete,
            c.request('get', path='/', conn_timeout=0.0001))
예제 #5
0
    def test_cleanup_resolved_hosts(self):
        loop = unittest.mock.Mock()
        c = HttpClient('localhost:8080', loop=loop, resolve=True)
        c._resolved_hosts[('localhost', 123)] = object()
        loop.call_later.assert_called_with(
            c._resolve_timeout, c._cleanup_resolved_host)
        loop.reset_mock()

        c._cleanup_resolved_host()
        self.assertFalse(bool(c._resolved_hosts))
        loop.call_later.assert_called_with(
            c._resolve_timeout, c._cleanup_resolved_host)
예제 #6
0
    def test_cleanup_resolved_hosts(self):
        loop = unittest.mock.Mock()
        c = HttpClient('localhost:8080', loop=loop, resolve=True)
        c._connector._resolved_hosts[('localhost', 123)] = object()
        loop.call_later.assert_called_with(
            c._resolve_timeout, c._cleanup_resolved_host)
        loop.reset_mock()

        c._cleanup_resolved_host()
        self.assertFalse(bool(c._connector._resolved_hosts))
        loop.call_later.assert_called_with(
            c._resolve_timeout, c._cleanup_resolved_host)
예제 #7
0
    def test_failed_request_one_failed(self):
        now = int(time.time())

        c = HttpClient([('localhost', 56777), ('localhost', 56778)],
                       loop=self.loop)
        c._hosts = []
        c._failed.append((('localhost', 1000), now - 10))
        c._failed.append((('localhost', 1001), now - 10))

        self.assertRaises(aiohttp.ConnectionError,
                          self.loop.run_until_complete,
                          c.request('get', path='/', timeout=0.0001))
예제 #8
0
    def test_resurrect_failed_all(self, asyncio):
        now = int(time.time())

        c = HttpClient([('localhost', 1000), ('localhost', 1000)],
                       resolve=False)
        c._hosts = []
        c._failed.append((('localhost', 1000), now - 10))
        c._failed.append((('localhost', 1001), now - 10))
        c._resurrect_failed()

        self.assertEqual(c._hosts, [('localhost', 1000), ('localhost', 1001)])
        self.assertFalse(asyncio.get_event_loop.return_value.call_later.called)
예제 #9
0
    def test_failed_request_one_failed(self):
        now = int(time.time())

        c = HttpClient(
            [('localhost', 56777), ('localhost', 56778)], loop=self.loop)
        c._hosts = []
        c._failed.append((('localhost', 1000), now - 10))
        c._failed.append((('localhost', 1001), now - 10))

        self.assertRaises(
            aiohttp.ConnectionError,
            self.loop.run_until_complete,
            c.request('get', path='/', timeout=0.0001))
예제 #10
0
    def test_resurrect_failed(self):
        now = int(time.time())
        loop = unittest.mock.Mock()

        c = HttpClient([('localhost', 1000), ('localhost', 1000)], loop=loop)
        c._hosts = []
        c._failed.append((('localhost', 1000), now - 10))
        c._failed.append((('localhost', 1001), now - 10))
        c._failed.append((('localhost', 1002), now + 10))
        c._resurrect_failed()

        self.assertEqual(c._hosts, [('localhost', 1000), ('localhost', 1001)])
        self.assertTrue(loop.call_later.called)
예제 #11
0
    def test_resurrect_failed_all(self, asyncio):
        now = int(time.time())

        c = HttpClient(
            [('localhost', 1000), ('localhost', 1000)], resolve=False)
        c._hosts = []
        c._failed.append((('localhost', 1000), now - 10))
        c._failed.append((('localhost', 1001), now - 10))
        c._resurrect_failed()

        self.assertEqual(
            c._hosts, [('localhost', 1000), ('localhost', 1001)])
        self.assertFalse(
            asyncio.get_event_loop.return_value.call_later.called)
예제 #12
0
    def test_resurrect_failed(self):
        now = int(time.time())
        loop = unittest.mock.Mock()

        c = HttpClient(
            [('localhost', 1000), ('localhost', 1000)], loop=loop)
        c._hosts = []
        c._failed.append((('localhost', 1000), now - 10))
        c._failed.append((('localhost', 1001), now - 10))
        c._failed.append((('localhost', 1002), now + 10))
        c._resurrect_failed()

        self.assertEqual(
            c._hosts, [('localhost', 1000), ('localhost', 1001)])
        self.assertTrue(loop.call_later.called)
예제 #13
0
    def test_ctor(self, asyncio):
        self.assertRaises(ValueError, HttpClient, ())
        self.assertRaises(ValueError, HttpClient, ('test:test', ))

        c = HttpClient('localhost:8080', loop=self.loop)
        self.assertEqual(c._hosts, [('localhost', 8080)])

        c = HttpClient('localhost', loop=self.loop)
        self.assertEqual(c._hosts, [('localhost', 80)])

        c = HttpClient([('localhost', 1000)], loop=self.loop)
        self.assertEqual(c._hosts, [('localhost', 1000)])

        c = HttpClient([('localhost', 1000)])
        self.assertIs(c._loop, asyncio.get_event_loop.return_value)
예제 #14
0
    def test_failed_request_one_failed(self):
        now = int(time.time())

        c = HttpClient(
            [('localhost', 56777), ('localhost', 56778)], loop=self.loop)
        c._hosts = []

        has_http_server = True
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            sock.connect(('127.0.0.1', 80))
        except ConnectionError:
            has_http_server = False
        finally:
            sock.close()

        c._failed.append((('localhost', 1000, has_http_server), now - 10))
        c._failed.append((('localhost', 1001, True), now - 10))

        self.assertRaises(
            aiohttp.ClientConnectionError,
            self.loop.run_until_complete,
            c.request('get', path='/'))
예제 #15
0
    def test_failed_request_one_failed(self):
        now = int(time.time())

        c = HttpClient(
            [('localhost', 56777), ('localhost', 56778)], loop=self.loop)
        c._hosts = []

        has_http_server = True
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            sock.connect(('127.0.0.1', 80))
        except ConnectionError:
            has_http_server = False
        finally:
            sock.close()

        c._failed.append((('localhost', 1000, has_http_server), now - 10))
        c._failed.append((('localhost', 1001, True), now - 10))

        self.assertRaises(
            aiohttp.ConnectionError,
            self.loop.run_until_complete,
            c.request('get', path='/'))
예제 #16
0
    def test_ctor(self, asyncio):
        self.assertRaises(ValueError, HttpClient, ())
        self.assertRaises(ValueError, HttpClient, ('test:test',))

        c = HttpClient('localhost:8080', loop=self.loop)
        self.assertEqual(c._hosts, [('localhost', 8080, True)])

        c = HttpClient('localhost', loop=self.loop)
        self.assertEqual(c._hosts, [('localhost', 80, False)])

        c = HttpClient([('localhost', 1000)], loop=self.loop)
        self.assertEqual(c._hosts, [('localhost', 1000, True)])

        c = HttpClient([('localhost', 1000)])
        self.assertIs(c._loop, asyncio.get_event_loop.return_value)

        c = HttpClient([('localhost', 1000)], ssl=True, verify_ssl=False)
        self.assertFalse(c._connector._verify_ssl)

        c = HttpClient([('localhost', 1000)], conn_pool=False, loop=self.loop)
        self.assertIsNone(c._connector)