def test_prepare_socket(self): conf = DummySocketConfig(1) sock_manager = self._makeOne(conf) sock = sock_manager.get_socket() self.assertTrue(sock_manager.is_prepared()) self.assertFalse(sock.bind_called) self.assertTrue(sock.listen_called) self.assertFalse(sock.close_called)
def test_prepare_socket(self): conf = DummySocketConfig(1) sock_manager = self._makeOne(conf) sock = sock_manager.get_socket() self.assertTrue(sock_manager.is_prepared()) self.assertFalse(sock.bind_called) self.assertTrue(sock.listen_called) self.assertEqual(sock.listen_backlog, socket.SOMAXCONN) self.assertFalse(sock.close_called) sock_manager.request_close()
def test_logging(self): conf = DummySocketConfig(1) logger = DummyLogger() sock_manager = self._makeOne(conf, logger=logger) # socket open sock = sock_manager.get_socket() self.assertEqual(len(logger.data), 1) self.assertEqual('Creating socket %s' % repr(conf), logger.data[0]) # socket close del sock self.assertEqual(len(logger.data), 2) self.assertEqual('Closing socket %s' % repr(conf), logger.data[1])
def test_socket_lifecycle(self): conf = DummySocketConfig(2) sock_manager = self._makeOne(conf) #Assert that sockets are created on demand self.assertFalse(sock_manager.is_prepared()) #Get two socket references sock = sock_manager.get_socket() self.assertTrue(sock_manager.is_prepared()) #socket created on demand sock_id = id(sock._get()) sock2 = sock_manager.get_socket() sock2_id = id(sock2._get()) #Assert that they are not the same proxy object self.assertNotEqual(sock, sock2) #Assert that they are the same underlying socket self.assertEqual(sock_id, sock2_id) #Request socket close sock_manager.request_close() #Socket not actually closed yet b/c ref ct is 2 self.assertTrue(sock_manager.is_prepared()) self.assertFalse(sock_manager.socket.close_called) sock = None #Socket not actually closed yet b/c ref ct is 1 self.assertTrue(sock_manager.is_prepared()) self.assertFalse(sock_manager.socket.close_called) sock2 = None #Socket closed self.assertFalse(sock_manager.is_prepared()) self.assertTrue(sock_manager.socket.close_called) #Get a new socket reference sock3 = sock_manager.get_socket() self.assertTrue(sock_manager.is_prepared()) sock3_id = id(sock3._get()) #Assert that it is not the same socket self.assertNotEqual(sock_id, sock3_id) #Drop ref ct to zero del sock3 #Assert that socket is still open until close is requested self.assertTrue(sock_manager.is_prepared()) self.assertFalse(sock_manager.socket.close_called) sock_manager.request_close() #Now assert that socket is closed self.assertFalse(sock_manager.is_prepared()) self.assertTrue(sock_manager.socket.close_called)
def test_get_config(self): conf = DummySocketConfig(2) sock_manager = self._makeOne(conf) self.assertEqual(conf, sock_manager.config())
def test_repr(self): conf = DummySocketConfig(2) sock_manager = self._makeOne(conf) expected = "<%s at %s for %s>" % (sock_manager.__class__, id(sock_manager), conf.url) self.assertEqual(repr(sock_manager), expected)
def test_prepare_socket_uses_somaxconn_if_no_backlog_configured(self): conf = DummySocketConfig(1, backlog=None) sock_manager = self._makeOne(conf) sock = sock_manager.get_socket() self.assertTrue(sock_manager.is_prepared()) self.assertEqual(sock.listen_backlog, socket.SOMAXCONN)
def test_prepare_socket_uses_configured_backlog(self): conf = DummySocketConfig(1, backlog=42) sock_manager = self._makeOne(conf) sock = sock_manager.get_socket() self.assertTrue(sock_manager.is_prepared()) self.assertEqual(sock.listen_backlog, conf.get_backlog())