Ejemplo n.º 1
0
    def testPrefersIPv6(self):
        # This will fail if the testing system doesn't have IPv6 support.
        # TODO Detect this with socket.AF_INET6 existance and... how do you
        # mark a test as not applicable/didn't run? Hmm.

        # At the time of test writing, example.com has A and AAAA records.
        # I'm assuming that isn't likely to change.
        sc = socketcache.UDPSocketCache("example.com", 80)
        (sock, addr) = sc.get()
        self.assertEqual(len(addr), 4)
Ejemplo n.º 2
0
    def testCaching(self):
        sc = socketcache.UDPSocketCache("example.com", 80, ttl=0.1)
        sc.get()

        # Reach into the UDPSocketCache instance and break the host, so if
        # it tries to resolve anything again it'll blow up.
        sc._host = "doesnotexist.example.com"

        # This should not raise an exception because we're calling it within
        # 0.1s of the previous call.
        sc.get()

        # Wait a moment and try again - it should blow up.
        time.sleep(0.2)
        self.assertRaises(Exception, sc.get)
Ejemplo n.º 3
0
 def testWorksWithIPv6Addresses(self):
     sc = socketcache.UDPSocketCache("::1", 80)
     (sock, addr) = sc.get()
     (host, port, flow, scope) = addr
     self.assertEqual(host, "::1")
Ejemplo n.º 4
0
 def testWorksWithIPv4Addresses(self):
     sc = socketcache.UDPSocketCache("127.0.0.1", 80)
     (sock, addr) = sc.get()
     (host, port) = addr
     self.assertEqual(host, "127.0.0.1")
Ejemplo n.º 5
0
 def testPreservesPort(self):
     sc = socketcache.UDPSocketCache("127.0.0.1", 80)
     (sock, addr) = sc.get()
     (host, port) = addr
     self.assertEqual(port, 80)
Ejemplo n.º 6
0
 def testGetsAddrTuple(self):
     sc = socketcache.UDPSocketCache("example.com", 80)
     (sock, addr) = sc.get()
     self.assertIsInstance(addr, tuple)
Ejemplo n.º 7
0
 def testGetsSocket(self):
     sc = socketcache.UDPSocketCache("example.com", 80)
     (sock, addr) = sc.get()
     self.assertIsInstance(sock, socket.socket)
Ejemplo n.º 8
0
 def __init__(self, host="localhost", port=2003, prefix=None, debug=False):
     self._host = socketcache.UDPSocketCache(host, port)
     self._addr = (host, port)
     self._prefix = prefix
     self._debug = debug