Exemplo n.º 1
0
 def test_close(self):
     proc = psutil.Process()
     ip_addresses = {
         info[4][0]
         for info in
         socket.getaddrinfo('example.com', 80, socket.AF_INET,
                            socket.SOCK_DGRAM, socket.IPPROTO_IP)
     }
     self.assertGreater(len(ip_addresses), 0)
     protocol = Protocol(
         config=Configuration(service_endpoint='http://example.com',
                              credentials=Credentials('A', 'B'),
                              auth_type=NOAUTH,
                              version=Version(Build(15, 1)),
                              retry_policy=FailFast()))
     session = protocol.get_session()
     session.get('http://example.com')
     self.assertEqual(
         len({
             p.raddr[0]
             for p in proc.connections() if p.raddr[0] in ip_addresses
         }), 1)
     protocol.release_session(session)
     protocol.close()
     self.assertEqual(
         len({
             p.raddr[0]
             for p in proc.connections() if p.raddr[0] in ip_addresses
         }), 0)
Exemplo n.º 2
0
    def test_close(self):
        # Don't use example.com here - it does not resolve or answer on all ISPs
        proc = psutil.Process()
        ip_addresses = {
            info[4][0]
            for info in
            socket.getaddrinfo('httpbin.org', 80, socket.AF_INET,
                               socket.SOCK_DGRAM, socket.IPPROTO_IP)
        }

        def conn_count():
            return len(
                [p for p in proc.connections() if p.raddr[0] in ip_addresses])

        self.assertGreater(len(ip_addresses), 0)
        protocol = Protocol(config=Configuration(
            service_endpoint='http://httpbin.org',
            credentials=Credentials(get_random_string(8), get_random_string(
                8)),
            auth_type=NOAUTH,
            version=Version(Build(15, 1)),
            retry_policy=FailFast(),
            max_connections=3))
        # Merely getting a session should not create conections
        session = protocol.get_session()
        self.assertEqual(conn_count(), 0)
        # Open one URL - we have 1 connection
        session.get('http://httpbin.org')
        self.assertEqual(conn_count(), 1)
        # Open the same URL - we should still have 1 connection
        session.get('http://httpbin.org')
        self.assertEqual(conn_count(), 1)

        # Open some more connections
        s2 = protocol.get_session()
        s2.get('http://httpbin.org')
        s3 = protocol.get_session()
        s3.get('http://httpbin.org')
        self.assertEqual(conn_count(), 3)

        # Releasing the sessions does not close the connections
        protocol.release_session(session)
        protocol.release_session(s2)
        protocol.release_session(s3)
        self.assertEqual(conn_count(), 3)

        # But closing explicitly does
        protocol.close()
        self.assertEqual(conn_count(), 0)