Commit e012980a authored by Jason Madden's avatar Jason Madden

Centralize socket timeouts.

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