def test_recv_into_timeout(self): buf = buffer(array.array('B')) listener = sockets.GreenSocket() listener.bind(('', 0)) listener.listen(50) address, port = listener.getsockname() self.assertNotEquals(address, 0) accepting = event.Event() accepted = event.Event() def server(): # accept the connection in another greenlet accepting.send() sock, addr = listener.accept() accepted.wait() gt = spawn(server) client = sockets.GreenSocket() client.settimeout(0.1) accepting.wait() client.connect(('127.0.0.1', port)) try: client.recv_into(buf, 100) self.fail("socket.timeout not raised") except socket.timeout, e: self.assert_(hasattr(e, 'args')) self.assertEqual(e.args[0], 'timed out')
def test_sendall_timeout(self): listener = sockets.GreenSocket() listener.bind(('', 0)) listener.listen(50) evt = event.Event() def server(): # accept the connection in another greenlet sock, addr = listener.accept() evt.wait() gt = spawn(server) addr = listener.getsockname() self.assertNotEqual(addr[1], 0) client = sockets.GreenSocket() client.settimeout(0.1) client.connect(addr) try: msg = s2b("A") * (8 * 1024 * 1024) # want to exceed the size of the OS buffer so it'll block client.sendall(msg) self.fail("socket.timeout not raised") except socket.timeout, e: self.assert_(hasattr(e, 'args')) self.assertEqual(e.args[1], 'timed out')
def test_recv_into(self): self.reset_timeout(100000) listener = sockets.GreenSocket() listener.bind(('', 0)) listener.listen(50) address, port = listener.getsockname() self.assertNotEquals(address, 0) accepting = event.Event() received = event.Event() sent_data = '1234567890' def server(): # accept the connection in another greenlet accepting.send() sock, addr = listener.accept() sock.send(sent_data) def client(): buf = buffer(array.array('B')) client = sockets.GreenSocket() accepting.wait() sleep(0.5) client.connect(('127.0.0.1', port)) client.recv_into(buf, 5000) received.send(buf) waitall(spawn(client), spawn(server)) received_data = received.wait() self.assertEquals(sent_data, received_data)
def client(): client = sockets.GreenSocket() client.connect(('127.0.0.1', listener_port)) msg = 'hhheeeeelloooooo' total_sent = 0 client.send(msg) return msg
def test_connect_ex_timeout(self): gs = sockets.GreenSocket(socket.AF_INET, socket.SOCK_STREAM) gs.settimeout(0.1) #e = gs.connect_ex(('192.0.2.1', 80)) e = gs.connect_ex(('255.255.0.1', 80)) self.assertIn(e, (errno.EHOSTUNREACH, errno.ECONNREFUSED, errno.ENETUNREACH, errno.ETIME, errno.EAGAIN))
def client(): client = sockets.GreenSocket() accepting.wait() sleep(0.5) client.connect(('127.0.0.1', port)) received_data = client.recv(5000) received.send(received_data)
def test_timeout(self, socket=socket): """ Test that the socket timeout exception works correctly. """ server = sockets.GreenSocket(socket.AF_INET, socket.SOCK_STREAM) server.bind(('127.0.0.1', 0)) server.listen(1) _, port = server.getsockname() self.assertNotEqual(port, 0) s = socket.socket() s.connect(('127.0.0.1', port)) cs, addr = server.accept() cs.settimeout(1) try: try: cs.recv(1024) self.fail("Should have timed out") except socket.timeout, ex: assert hasattr(ex, 'args') assert len(ex.args) == 1 assert ex.args[0] == 'timed out' finally: s.close() cs.close() server.close()
def client(): buf = buffer(array.array('B')) client = sockets.GreenSocket() accepting.wait() sleep(0.5) client.connect(('127.0.0.1', port)) client.recv_into(buf, 5000) received.send(buf)
def test_connect_timeout(self): gs = sockets.GreenSocket(socket.AF_INET, socket.SOCK_STREAM) gs.settimeout(0.5) try: gs.connect(('192.0.2.2', 80)) except socket.timeout, e: self.assert_(hasattr(e, 'args')) self.assertEqual(e.args[0], 'timed out')
def test_connection_refused(self): # open and close a dummy server to find an unused port server = sockets.GreenSocket(socket.AF_INET, socket.SOCK_STREAM) server.bind(('127.0.0.1', 0)) server.listen(1) _, port = server.getsockname() self.assertNotEqual(port, 0) server.close() del server s = sockets.GreenSocket() try: s.connect(('127.0.0.1', port)) self.fail("Shouldn't have connected") except socket.error, ex: code, text = ex.args assert code in [111, 61, 10061], (code, text) assert 'refused' in text.lower(), (code, text)
def client(): client = sockets.GreenSocket() client.connect(('127.0.0.1', listener_port)) msg = s2b("A") * ( 10000) # large enough number to overwhelm most buffers # want to exceed the size of the OS buffer so it'll block in a single send total_sent = 0 for x in range(10): total_sent += client.send(msg) return total_sent
def test_recvfrom_timeout(self): gs = sockets.GreenSocket(socket.AF_INET, socket.SOCK_DGRAM) gs.settimeout(.1) gs.bind(('', 0)) try: gs.recvfrom(8192) self.fail("socket.timeout not raised") except socket.timeout, e: self.assert_(hasattr(e, 'args')) self.assertEqual(e.args[0], 'timed out')
def test_accept_timeout(self): gs = sockets.GreenSocket(socket.AF_INET, socket.SOCK_STREAM) gs.settimeout(0.1) gs.bind(('', 0)) gs.listen(50) self.assertNotEqual(gs.getsockname()[1], 0) try: gs.accept() self.fail("socket.timeout not raised") except socket.timeout, e: self.assert_(hasattr(e, 'args')) self.assertEqual(e.args[0], 'timed out')
def client(): client = sockets.GreenSocket() client.connect(('127.0.0.1', port)) try: client.settimeout(0.00001) msg = s2b("A") * ( 100000) # large enough number to overwhelm most buffers total_sent = 0 # want to exceed the size of the OS buffer so it'll block in a # single send for x in range(10): total_sent += client.send(msg) self.fail("socket.timeout not raised") except socket.timeout, e: self.assert_(hasattr(e, 'args')) self.assertEqual(e.args[1], 'timed out')
def test_recv_something(self): DATLEN = 5 listener = sockets.GreenSocket() listener.bind(('', 0)) listener.listen(50) address, port = listener.getsockname() self.assertNotEquals(address, 0) accepting = event.Event() sent = event.Event() received = event.Event() def server(): # accept the connection in another greenlet accepting.send() sock, addr = listener.accept() s = '1234567890' sock.send(s) sent.send(s) gt_server = spawn(server) def client(): client = sockets.GreenSocket() accepting.wait() sleep(0.5) client.connect(('127.0.0.1', port)) received_data = client.recv(DATLEN) received.send(received_data) gt_client = spawn(client) sent_data = sent.wait() received_data = received.wait() self.assertEquals(sent_data[:DATLEN], received_data)
def test_bind_wrong_ip(self): listener = sockets.GreenSocket() try: listener.bind(('127.255.255.255', 0)) except socket.error, e: self.assert_(hasattr(e, 'args'))
def test_connect_invalid_ip(self): gs = sockets.GreenSocket(socket.AF_INET, socket.SOCK_STREAM) gs.connect(('0.0.0.0', 80))
def test_listen_without_bind(self): listener = sockets.GreenSocket() listener.listen(50)
def test_getsockname(self): listener = sockets.GreenSocket() listener.bind(('', 0)) addr = listener.getsockname() self.assertNotEquals(addr[1], 0)