test for eventlet issue #38 and fix
This commit is contained in:
@@ -177,8 +177,8 @@ class GreenSocket(object):
|
||||
if socket_connect(fd, address):
|
||||
return
|
||||
if time.time() >= end:
|
||||
raise socket.timeout
|
||||
trampoline(fd, write=True, timeout=end-time.time(), timeout_exc=socket.timeout)
|
||||
raise socket.timeout("timed out")
|
||||
trampoline(fd, write=True, timeout=end-time.time(), timeout_exc=socket.timeout('timed out'))
|
||||
|
||||
def connect_ex(self, address):
|
||||
if self.act_non_blocking:
|
||||
@@ -187,7 +187,7 @@ class GreenSocket(object):
|
||||
if self.gettimeout() is None:
|
||||
while not socket_connect(fd, address):
|
||||
try:
|
||||
trampoline(fd, write=True, timeout_exc=socket.timeout)
|
||||
trampoline(fd, write=True, timeout_exc=socket.timeout('timed out'))
|
||||
except socket.error, ex:
|
||||
return ex[0]
|
||||
else:
|
||||
@@ -196,9 +196,9 @@ class GreenSocket(object):
|
||||
if socket_connect(fd, address):
|
||||
return 0
|
||||
if time.time() >= end:
|
||||
raise socket.timeout
|
||||
raise socket.timeout("timed out")
|
||||
try:
|
||||
trampoline(fd, write=True, timeout=end-time.time(), timeout_exc=socket.timeout)
|
||||
trampoline(fd, write=True, timeout=end-time.time(), timeout_exc=socket.timeout('timed out'))
|
||||
except socket.error, ex:
|
||||
return ex[0]
|
||||
|
||||
@@ -254,21 +254,21 @@ class GreenSocket(object):
|
||||
trampoline(fd,
|
||||
read=True,
|
||||
timeout=self.timeout,
|
||||
timeout_exc=socket.timeout)
|
||||
timeout_exc=socket.timeout('timed out'))
|
||||
|
||||
def recvfrom(self, *args):
|
||||
if not self.act_non_blocking:
|
||||
trampoline(self.fd, read=True, timeout=self.gettimeout(), timeout_exc=socket.timeout)
|
||||
trampoline(self.fd, read=True, timeout=self.gettimeout(), timeout_exc=socket.timeout('timed out'))
|
||||
return self.fd.recvfrom(*args)
|
||||
|
||||
def recvfrom_into(self, *args):
|
||||
if not self.act_non_blocking:
|
||||
trampoline(self.fd, read=True, timeout=self.gettimeout(), timeout_exc=socket.timeout)
|
||||
trampoline(self.fd, read=True, timeout=self.gettimeout(), timeout_exc=socket.timeout('timed out'))
|
||||
return self.fd.recvfrom_into(*args)
|
||||
|
||||
def recv_into(self, *args):
|
||||
if not self.act_non_blocking:
|
||||
trampoline(self.fd, read=True, timeout=self.gettimeout(), timeout_exc=socket.timeout)
|
||||
trampoline(self.fd, read=True, timeout=self.gettimeout(), timeout_exc=socket.timeout('timed out'))
|
||||
return self.fd.recv_into(*args)
|
||||
|
||||
def send(self, data, flags=0):
|
||||
@@ -290,11 +290,11 @@ class GreenSocket(object):
|
||||
trampoline(fd,
|
||||
write=True,
|
||||
timeout=self.timeout,
|
||||
timeout_exc=socket.timeout)
|
||||
timeout_exc=socket.timeout('timed out'))
|
||||
tail += self.send(data[tail:], flags)
|
||||
|
||||
def sendto(self, *args):
|
||||
trampoline(self.fd, write=True, timeout_exc=socket.timeout)
|
||||
trampoline(self.fd, write=True, timeout_exc=socket.timeout('timed out'))
|
||||
return self.fd.sendto(*args)
|
||||
|
||||
def setblocking(self, flag):
|
||||
|
@@ -1,4 +1,5 @@
|
||||
import unittest
|
||||
import socket as _original_sock
|
||||
from eventlet import api
|
||||
from eventlet.green import socket
|
||||
|
||||
@@ -20,5 +21,35 @@ class TestSocketErrors(unittest.TestCase):
|
||||
assert code in [111, 61, 10061], (code, text)
|
||||
assert 'refused' in text.lower(), (code, text)
|
||||
|
||||
def test_timeout_real_socket(self):
|
||||
""" Test underlying socket behavior to ensure correspondence
|
||||
between green sockets and the underlying socket module. """
|
||||
return self.test_timeout(socket=_original_sock)
|
||||
|
||||
def test_timeout(self, socket=socket):
|
||||
""" Test that the socket timeout exception works correctly. """
|
||||
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
server.bind(('127.0.0.1', 0))
|
||||
server.listen(1)
|
||||
port = server.getsockname()[1]
|
||||
|
||||
s = socket.socket()
|
||||
|
||||
s.connect(('127.0.0.1', port))
|
||||
|
||||
cs, addr = server.accept()
|
||||
cs.settimeout(1)
|
||||
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()
|
||||
|
||||
if __name__=='__main__':
|
||||
unittest.main()
|
||||
|
Reference in New Issue
Block a user