def test_client_method(self): port = self.get_available_port() self.create_server(port) socksocket = SocketFactory.client('127.0.0.1', port) self.assertEqual(socksocket.recv(len(CONNECTED)), CONNECTED) self.assertIsInstance(socksocket, socket.socket) socksocket.close()
def create_server(self, port): """Create a server on localhost to listen for connections on localhost.""" def serve(socksocket): sock = socksocket.accept()[0] sock.send(CONNECTED) sock.close() socksocket.close() threading.Thread(target=serve, args=(SocketFactory.server(port),)).start()
def test_close_method(self): # Create the socket object. server = self.create_server() client = SocketApi(SocketFactory.client('127.0.0.1', SERVER_PORT)) client.close() self.assertFalse(client.is_alive()) server.close()
def test_recv_method(self): def serve(server): server = SocketApi(server.accept()[0]) server.send(MESSAGE) server.close() # Create the socket object. server = self.create_server(serve) client = SocketApi(SocketFactory.client('127.0.0.1', SERVER_PORT)) self.assertEqual(client.recv(), MESSAGE) client.close() server.close()
def create_server(self, callback = None, args = None): # Create the socket object. server = SocketFactory.server(SERVER_PORT) if not callback: # Bug #6 def passable(server, **kwargs): import time server = server.accept()[0] # Waits 2 seconds to test server. time.sleep(2) # Close connection. server.close() callback = passable # Listen connections on a coroutine. threading.Thread(target=callback, args=(server, args)).start() return server
def test_server_method(self): socksocket = SocketFactory.server(port=self.get_available_port()) self.assertIsInstance(socksocket, socket.socket) socksocket.close()