diff --git a/doc/testing.rst b/doc/testing.rst index 5e427dd..3ed0f36 100644 --- a/doc/testing.rst +++ b/doc/testing.rst @@ -57,3 +57,16 @@ If you wish to run tests against a particular Twisted reactor, use ``--reactor=R * poll * selects * libevent (requires pyevent) + +Writing Tests +------------- + +What follows are some notes on writing tests, in no particular order. + +The filename convention when writing a test for module `foo` is to name the test `foo_test.py`. We don't yet have a convention for tests that are of finer granularity, but a sensible one might be `foo_class_test.py`. + +If you are writing a test that involves a client connecting to a spawned server, it is best to not use a hardcoded port because that makes it harder to parallelize tests. Instead bind the server to 0, and then look up its port when connecting the client, like this:: + + server_sock = api.tcp_listener(('127.0.0.1', 0)) + client_sock = api.connect_tcp(('localhost', server_sock.getsockname()[1])) + diff --git a/tests/greenio_test.py b/tests/greenio_test.py index 9ce9acb..0d914ae 100644 --- a/tests/greenio_test.py +++ b/tests/greenio_test.py @@ -257,10 +257,10 @@ class SSLTest(LimitedTestCase): stuff = sock.read(8192) sock.write('response') - sock = api.ssl_listener(('', 4201), self.certificate_file, self.private_key_file) + sock = api.ssl_listener(('127.0.0.1', 0), self.certificate_file, self.private_key_file) server_coro = coros.execute(serve, sock) - client = util.wrap_ssl(api.connect_tcp(('localhost', 4201))) + client = util.wrap_ssl(api.connect_tcp(('127.0.0.1', sock.getsockname()[1]))) client.write('line 1\r\nline 2\r\n\r\n') self.assertEquals(client.read(8192), 'response') server_coro.wait() @@ -271,11 +271,11 @@ class SSLTest(LimitedTestCase): sock.write('content') sock.shutdown() sock.close() - listener = api.ssl_listener(('', 4209), + listener = api.ssl_listener(('', 0), self.certificate_file, self.private_key_file) killer = api.spawn(serve, listener) - client = util.wrap_ssl(api.connect_tcp(('localhost', 4209))) + client = util.wrap_ssl(api.connect_tcp(('localhost', listener.getsockname()[1]))) client = greenio.GreenSSLObject(client) self.assertEquals(client.read(1024), 'content') self.assertEquals(client.read(1024), '') diff --git a/tests/test__refcount.py b/tests/test__refcount.py index 1df1fb3..365e20b 100644 --- a/tests/test__refcount.py +++ b/tests/test__refcount.py @@ -31,17 +31,15 @@ from eventlet.green.time import sleep import weakref import gc -address = ('0.0.0.0', 7878) - SOCKET_TIMEOUT = 0.1 def init_server(): s = socket.socket() s.settimeout(SOCKET_TIMEOUT) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - s.bind(address) + s.bind(('localhost', 0)) s.listen(5) - return s + return s, s.getsockname()[1] def handle_request(s, raise_on_timeout): try: @@ -60,10 +58,10 @@ def handle_request(s, raise_on_timeout): #print 'handle_request - conn refcount: %s' % sys.getrefcount(conn) #conn.close() -def make_request(): +def make_request(port): #print 'make_request' s = socket.socket() - s.connect(address) + s.connect(('localhost', port)) #print 'make_request - connected' res = s.send('hello') #print 'make_request - sent %s' % res @@ -73,10 +71,10 @@ def make_request(): #s.close() def run_interaction(run_client): - s = init_server() + s, port = init_server() start_new_thread(handle_request, (s, run_client)) if run_client: - start_new_thread(make_request, ()) + start_new_thread(make_request, (port,)) sleep(0.1+SOCKET_TIMEOUT) #print sys.getrefcount(s.fd) #s.close()