test for eventlet issue #38 and fix
This commit is contained in:
@@ -177,8 +177,8 @@ class GreenSocket(object):
|
|||||||
if socket_connect(fd, address):
|
if socket_connect(fd, address):
|
||||||
return
|
return
|
||||||
if time.time() >= end:
|
if time.time() >= end:
|
||||||
raise socket.timeout
|
raise socket.timeout("timed out")
|
||||||
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'))
|
||||||
|
|
||||||
def connect_ex(self, address):
|
def connect_ex(self, address):
|
||||||
if self.act_non_blocking:
|
if self.act_non_blocking:
|
||||||
@@ -187,7 +187,7 @@ class GreenSocket(object):
|
|||||||
if self.gettimeout() is None:
|
if self.gettimeout() is None:
|
||||||
while not socket_connect(fd, address):
|
while not socket_connect(fd, address):
|
||||||
try:
|
try:
|
||||||
trampoline(fd, write=True, timeout_exc=socket.timeout)
|
trampoline(fd, write=True, timeout_exc=socket.timeout('timed out'))
|
||||||
except socket.error, ex:
|
except socket.error, ex:
|
||||||
return ex[0]
|
return ex[0]
|
||||||
else:
|
else:
|
||||||
@@ -196,9 +196,9 @@ class GreenSocket(object):
|
|||||||
if socket_connect(fd, address):
|
if socket_connect(fd, address):
|
||||||
return 0
|
return 0
|
||||||
if time.time() >= end:
|
if time.time() >= end:
|
||||||
raise socket.timeout
|
raise socket.timeout("timed out")
|
||||||
try:
|
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:
|
except socket.error, ex:
|
||||||
return ex[0]
|
return ex[0]
|
||||||
|
|
||||||
@@ -254,21 +254,21 @@ class GreenSocket(object):
|
|||||||
trampoline(fd,
|
trampoline(fd,
|
||||||
read=True,
|
read=True,
|
||||||
timeout=self.timeout,
|
timeout=self.timeout,
|
||||||
timeout_exc=socket.timeout)
|
timeout_exc=socket.timeout('timed out'))
|
||||||
|
|
||||||
def recvfrom(self, *args):
|
def recvfrom(self, *args):
|
||||||
if not self.act_non_blocking:
|
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)
|
return self.fd.recvfrom(*args)
|
||||||
|
|
||||||
def recvfrom_into(self, *args):
|
def recvfrom_into(self, *args):
|
||||||
if not self.act_non_blocking:
|
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)
|
return self.fd.recvfrom_into(*args)
|
||||||
|
|
||||||
def recv_into(self, *args):
|
def recv_into(self, *args):
|
||||||
if not self.act_non_blocking:
|
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)
|
return self.fd.recv_into(*args)
|
||||||
|
|
||||||
def send(self, data, flags=0):
|
def send(self, data, flags=0):
|
||||||
@@ -290,11 +290,11 @@ class GreenSocket(object):
|
|||||||
trampoline(fd,
|
trampoline(fd,
|
||||||
write=True,
|
write=True,
|
||||||
timeout=self.timeout,
|
timeout=self.timeout,
|
||||||
timeout_exc=socket.timeout)
|
timeout_exc=socket.timeout('timed out'))
|
||||||
tail += self.send(data[tail:], flags)
|
tail += self.send(data[tail:], flags)
|
||||||
|
|
||||||
def sendto(self, *args):
|
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)
|
return self.fd.sendto(*args)
|
||||||
|
|
||||||
def setblocking(self, flag):
|
def setblocking(self, flag):
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
import unittest
|
import unittest
|
||||||
|
import socket as _original_sock
|
||||||
from eventlet import api
|
from eventlet import api
|
||||||
from eventlet.green import socket
|
from eventlet.green import socket
|
||||||
|
|
||||||
@@ -20,5 +21,35 @@ class TestSocketErrors(unittest.TestCase):
|
|||||||
assert code in [111, 61, 10061], (code, text)
|
assert code in [111, 61, 10061], (code, text)
|
||||||
assert 'refused' in text.lower(), (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__':
|
if __name__=='__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Reference in New Issue
Block a user