Example #1
0
    def test_default_timeout(self):
        # Testing default timeout
        # The default timeout should initially be None
        self.assertEqual(socket.getdefaulttimeout(), None)
        s = socket.socket()
        self.assertEqual(s.gettimeout(), None)
        s.close()

        # Set the default timeout to 10, and see if it propagates
        socket.setdefaulttimeout(10)
        self.assertEqual(socket.getdefaulttimeout(), 10)
        s = socket.socket()
        self.assertEqual(s.gettimeout(), 10)
        s.close()

        # Reset the default timeout to None, and see if it propagates
        socket.setdefaulttimeout(None)
        self.assertEqual(socket.getdefaulttimeout(), None)
        s = socket.socket()
        self.assertEqual(s.gettimeout(), None)
        s.close()

        # Check that setting it to an invalid value raises ValueError
        self.assertRaises(ValueError, socket.setdefaulttimeout, -1)

        # Check that setting it to an invalid type raises TypeError
        self.assertRaises(TypeError, socket.setdefaulttimeout, "spam")
Example #2
0
    def test_default_timeout (self):
        # Testing default timeout
        # The default timeout should initially be None
        self.assertEqual(socket.getdefaulttimeout(), None)
        s = socket.socket()
        self.assertEqual(s.gettimeout(), None)
        s.close()

        # Set the default timeout to 10, and see if it propagates
        socket.setdefaulttimeout(10)
        self.assertEqual(socket.getdefaulttimeout(), 10)
        s = socket.socket()
        self.assertEqual(s.gettimeout(), 10)
        s.close()

        # Reset the default timeout to None, and see if it propagates
        socket.setdefaulttimeout(None)
        self.assertEqual(socket.getdefaulttimeout(), None)
        s = socket.socket()
        self.assertEqual(s.gettimeout(), None)
        s.close()

        # Check that setting it to an invalid value raises ValueError
        self.assertRaises(ValueError, socket.setdefaulttimeout, -1)

        # Check that setting it to an invalid type raises TypeError
        self.assertRaises(TypeError, socket.setdefaulttimeout, "spam")
Example #3
0
    def testTimeoutAttribute (self):
        '''This will prove that the timeout gets through
        HTTPConnection and into the socket.
        '''
        # default -- use global socket timeout
        self.assertTrue(socket.getdefaulttimeout() is None)
        socket.setdefaulttimeout(30)
        try:
            httpConn = httplib.HTTPConnection(HOST, TimeoutTest.PORT)
            httpConn.connect()
        finally:
            socket.setdefaulttimeout(None)
        self.assertEqual(httpConn.sock.gettimeout(), 30)
        httpConn.close()

        # no timeout -- do not use global socket default
        self.assertTrue(socket.getdefaulttimeout() is None)
        socket.setdefaulttimeout(30)
        try:
            httpConn = httplib.HTTPConnection(HOST, TimeoutTest.PORT,
                                              timeout = None)
            httpConn.connect()
        finally:
            socket.setdefaulttimeout(None)
        self.assertEqual(httpConn.sock.gettimeout(), None)
        httpConn.close()

        # a value
        httpConn = httplib.HTTPConnection(HOST, TimeoutTest.PORT, timeout = 30)
        httpConn.connect()
        self.assertEqual(httpConn.sock.gettimeout(), 30)
        httpConn.close()
Example #4
0
 def _testTimeoutDefault(self):
     # passing no explicit timeout uses socket's global default
     self.assertTrue(socket.getdefaulttimeout() is None)
     socket.setdefaulttimeout(42)
     try:
         self.cli = socket.create_connection((HOST, self.port))
         self.addCleanup(self.cli.close)
     finally:
         socket.setdefaulttimeout(None)
     self.assertEqual(self.cli.gettimeout(), 42)
Example #5
0
 def _testTimeoutNone (self):
     # None timeout means the same as sock.settimeout(None)
     self.assertTrue(socket.getdefaulttimeout() is None)
     socket.setdefaulttimeout(30)
     try:
         self.cli = socket.create_connection((HOST, self.port), timeout = None)
         self.addCleanup(self.cli.close)
     finally:
         socket.setdefaulttimeout(None)
     self.assertEqual(self.cli.gettimeout(), None)
Example #6
0
 def _testTimeoutDefault (self):
     # passing no explicit timeout uses socket's global default
     self.assertTrue(socket.getdefaulttimeout() is None)
     socket.setdefaulttimeout(42)
     try:
         self.cli = socket.create_connection((HOST, self.port))
         self.addCleanup(self.cli.close)
     finally:
         socket.setdefaulttimeout(None)
     self.assertEqual(self.cli.gettimeout(), 42)
Example #7
0
 def _testTimeoutNone(self):
     # None timeout means the same as sock.settimeout(None)
     self.assertTrue(socket.getdefaulttimeout() is None)
     socket.setdefaulttimeout(30)
     try:
         self.cli = socket.create_connection((HOST, self.port),
                                             timeout=None)
         self.addCleanup(self.cli.close)
     finally:
         socket.setdefaulttimeout(None)
     self.assertEqual(self.cli.gettimeout(), None)