def run(self): try: # acquire a socket list(self.collection.find()) pool = get_pool(self.collection.database.connection) socket_info = pool._get_request_state() assert isinstance(socket_info, SocketInfo) self.request_sock = socket_info.sock assert not _closed(self.request_sock) # Dereference socket_info so it can potentially return to the pool del socket_info finally: self.rendezvous() # all threads have passed the rendezvous, wait for # test_server_disconnect() to disconnect the connection self.state.ev_resume.wait() # test_server_disconnect() has closed this socket, but that's ok # because it's not our request socket anymore assert _closed(self.request_sock) # if disconnect() properly closed all threads' request sockets, then # this won't raise AutoReconnect because it will acquire a new socket assert self.request_sock == pool._get_request_state().sock list(self.collection.find()) assert self.collection.database.connection.in_request() assert self.request_sock != pool._get_request_state().sock self.passed = True
def alive(self): """Return ``False`` if there has been an error communicating with the server, else ``True``. This method attempts to check the status of the server with minimal I/O. The current thread / greenlet retrieves a socket from the pool (its request socket if it's in a request, or a random idle socket if it's not in a request) and checks whether calling `select`_ on it raises an error. If there are currently no idle sockets, :meth:`alive` will attempt to actually connect to the server. A more certain way to determine server availability is:: client.admin.command('ping') .. _select: http://docs.python.org/2/library/select.html#select.select """ # In the common case, a socket is available and was used recently, so # calling select() on it is a reasonable attempt to see if the OS has # reported an error. Note this can be wasteful: __socket implicitly # calls select() if the socket hasn't been checked in the last second, # or it may create a new socket, in which case calling select() is # redundant. try: sock_info = self.__socket() return not pool._closed(sock_info.sock) except (socket.error, ConnectionFailure): return False
def before_rendezvous(self): # acquire a socket list(self.collection.find()) self.pool = get_pool(self.collection.database.connection) socket_info = self.pool._get_request_state() assert isinstance(socket_info, SocketInfo) self.request_sock = socket_info.sock assert not _closed(self.request_sock)
def before_rendezvous(self): # acquire a socket list(self.collection.find()) pool = get_pool(self.collection.database.connection) socket_info = pool._get_request_state() assert isinstance(socket_info, SocketInfo) self.request_sock = socket_info.sock assert not _closed(self.request_sock)
def after_rendezvous(self): # test_server_disconnect() has closed this socket, but that's ok # because it's not our request socket anymore assert _closed(self.request_sock) # if disconnect() properly closed all threads' request sockets, then # this won't raise AutoReconnect because it will acquire a new socket assert self.request_sock == self.pool._get_request_state().sock list(self.collection.find()) assert self.collection.database.connection.in_request() assert self.request_sock != self.pool._get_request_state().sock
def after_rendezvous(self): # test_server_disconnect() has closed this socket, but that's ok # because it's not our request socket anymore assert _closed(self.request_sock) # if disconnect() properly replaced the pool, then this won't raise # AutoReconnect because it will acquire a new socket list(self.collection.find()) assert self.collection.database.connection.in_request() pool = get_pool(self.collection.database.connection) assert self.request_sock != pool._get_request_state().sock