Commit e012980a authored by Jason Madden's avatar Jason Madden

Centralize socket timeouts.

parent 6184dd23
...@@ -490,6 +490,12 @@ if RUNNING_ON_TRAVIS: ...@@ -490,6 +490,12 @@ if RUNNING_ON_TRAVIS:
# connected to with the same error. # connected to with the same error.
DEFAULT_BIND_ADDR = '127.0.0.1' DEFAULT_BIND_ADDR = '127.0.0.1'
# For in-process sockets
DEFAULT_SOCKET_TIMEOUT = 0.1 if not EXPECT_POOR_TIMER_RESOLUTION else 2.0
# For cross-process sockets
DEFAULT_XPC_SOCKET_TIMEOUT = 2.0 if not EXPECT_POOR_TIMER_RESOLUTION else 4.0
class TestCase(TestCaseMetaClass("NewBase", (BaseTestCase,), {})): class TestCase(TestCaseMetaClass("NewBase", (BaseTestCase,), {})):
__timeout__ = LOCAL_TIMEOUT if not RUNNING_ON_CI else CI_TIMEOUT __timeout__ = LOCAL_TIMEOUT if not RUNNING_ON_CI else CI_TIMEOUT
switch_expected = 'default' switch_expected = 'default'
......
import sys import sys
#import time
from unittest import main try:
if sys.version_info[0] == 3:
from urllib import request as urllib2 from urllib import request as urllib2
else: except ImportError:
import urllib2 import urllib2
from unittest import SkipTest
import util import util
import socket import socket
import ssl import ssl
from greentest import DEFAULT_XPC_SOCKET_TIMEOUT
from greentest import main
class Test_wsgiserver(util.TestServer): class Test_wsgiserver(util.TestServer):
server = 'wsgiserver.py' server = 'wsgiserver.py'
...@@ -22,10 +24,13 @@ class Test_wsgiserver(util.TestServer): ...@@ -22,10 +24,13 @@ class Test_wsgiserver(util.TestServer):
def read(self, path='/'): def read(self, path='/'):
url = self.URL + path url = self.URL + path
try: try:
kwargs = {}
if self.ssl_ctx is not None: if self.ssl_ctx is not None:
response = urllib2.urlopen(url, None, 2, context=self.ssl_ctx) kwargs = {'context': self.ssl_ctx}
else:
response = urllib2.urlopen(url, None, 2) response = urllib2.urlopen(url, None,
DEFAULT_XPC_SOCKET_TIMEOUT,
**kwargs)
except urllib2.HTTPError: except urllib2.HTTPError:
response = sys.exc_info()[1] response = sys.exc_info()[1]
result = '%s %s' % (response.code, response.msg), response.read() result = '%s %s' % (response.code, response.msg), response.read()
...@@ -47,7 +52,6 @@ class Test_wsgiserver(util.TestServer): ...@@ -47,7 +52,6 @@ class Test_wsgiserver(util.TestServer):
def _do_test_a_blocking_client(self): def _do_test_a_blocking_client(self):
# We spawn this in a separate server because if it's broken # We spawn this in a separate server because if it's broken
# the whole server hangs # the whole server hangs
print("Checking blocking")
with self.running_server(): with self.running_server():
# First, make sure we can talk to it. # First, make sure we can talk to it.
self._test_hello() self._test_hello()
...@@ -75,10 +79,10 @@ class Test_wsgiserver(util.TestServer): ...@@ -75,10 +79,10 @@ class Test_wsgiserver(util.TestServer):
line = sock_file.readline() line = sock_file.readline()
self.assertEqual(line, b'HTTP/1.1 404 Not Found\r\n') self.assertEqual(line, b'HTTP/1.1 404 Not Found\r\n')
sock.close()
sock_file.close() sock_file.close()
if ssl_sock is not None: if ssl_sock is not None:
ssl_sock.close() ssl_sock.close()
sock.close()
def test_a_blocking_client(self): def test_a_blocking_client(self):
self._do_test_a_blocking_client() self._do_test_a_blocking_client()
...@@ -102,14 +106,14 @@ class Test_webproxy(Test_wsgiserver): ...@@ -102,14 +106,14 @@ class Test_webproxy(Test_wsgiserver):
def _run_all_tests(self): def _run_all_tests(self):
status, data = self.read('/') status, data = self.read('/')
self.assertEqual(status, '200 OK') self.assertEqual(status, '200 OK')
assert b"gevent example" in data, repr(data) self.assertIn(b"gevent example", data)
status, data = self.read('/http://www.google.com') status, data = self.read('/http://www.google.com')
self.assertEqual(status, '200 OK') self.assertEqual(status, '200 OK')
assert b'google' in data.lower(), repr(data) self.assertIn(b'google', data.lower())
def _do_test_a_blocking_client(self): def test_a_blocking_client(self):
# Not applicable # Not applicable
return raise SkipTest("Not applicable")
# class Test_webpy(Test_wsgiserver): # class Test_webpy(Test_wsgiserver):
......
...@@ -4,9 +4,6 @@ import gevent ...@@ -4,9 +4,6 @@ import gevent
import util import util
_DEFAULT_SOCKET_TIMEOUT = 0.1 if not greentest.EXPECT_POOR_TIMER_RESOLUTION else 2.0
class Test(util.TestServer): class Test(util.TestServer):
server = 'echoserver.py' server = 'echoserver.py'
...@@ -18,11 +15,11 @@ class Test(util.TestServer): ...@@ -18,11 +15,11 @@ class Test(util.TestServer):
kwargs = {'bufsize': 1} kwargs = {'bufsize': 1}
kwargs['mode'] = 'rb' kwargs['mode'] = 'rb'
conn = create_connection(('127.0.0.1', 16000)) conn = create_connection(('127.0.0.1', 16000))
conn.settimeout(_DEFAULT_SOCKET_TIMEOUT) conn.settimeout(greentest.DEFAULT_XPC_SOCKET_TIMEOUT)
rfile = conn.makefile(**kwargs) rfile = conn.makefile(**kwargs)
welcome = rfile.readline() welcome = rfile.readline()
assert b'Welcome' in welcome, repr(welcome) self.assertIn(b'Welcome', welcome)
conn.sendall(message) conn.sendall(message)
received = rfile.read(len(message)) received = rfile.read(len(message))
......
...@@ -6,11 +6,11 @@ import os ...@@ -6,11 +6,11 @@ import os
import greentest import greentest
from greentest import PY3 from greentest import PY3
from greentest import DEFAULT_SOCKET_TIMEOUT as _DEFAULT_SOCKET_TIMEOUT
from gevent import socket from gevent import socket
import gevent import gevent
from gevent.server import StreamServer from gevent.server import StreamServer
_DEFAULT_SOCKET_TIMEOUT = 0.1 if not greentest.EXPECT_POOR_TIMER_RESOLUTION else 1.0
class SimpleStreamServer(StreamServer): class SimpleStreamServer(StreamServer):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment