[Checkins] SVN: zc.buildout/trunk/src/zc/buildout/testing.py Improved the way the test web server is handled.

Jim Fulton jim at zope.com
Thu Jun 29 16:37:09 EDT 2006


Log message for revision 68915:
  Improved the way the test web server is handled.
  
  Added an api for registering tearDown handlers.
  

Changed:
  U   zc.buildout/trunk/src/zc/buildout/testing.py

-=-
Modified: zc.buildout/trunk/src/zc/buildout/testing.py
===================================================================
--- zc.buildout/trunk/src/zc/buildout/testing.py	2006-06-29 20:27:17 UTC (rev 68914)
+++ zc.buildout/trunk/src/zc/buildout/testing.py	2006-06-29 20:37:09 UTC (rev 68915)
@@ -105,12 +105,15 @@
         get = get,
         __original_wd__ = os.getcwd(),
         __temporary_directories__ = temporary_directories,
+        __tearDown__ = [],
         mkdtemp = mkdtemp,
         ))
 
 def buildoutTearDown(test):
     for d in test.globs['__temporary_directories__']:
         shutil.rmtree(d)
+    for f in test.globs['__tearDown__']:
+        f()
     os.chdir(test.globs['__original_wd__'])
     if test.globs.get('_oldhome') is not None:
         os.environ['HOME'] = test.globs['_oldhome']
@@ -290,18 +293,32 @@
             s.close()
     raise RuntimeError, "Can't find port"
 
-def start_server(tree):
+def _start_server(tree, name=''):
     port = get_port()
-    threading.Thread(target=_run, args=(tree, port)).start()
+    thread = threading.Thread(target=_run, args=(tree, port), name=name)
+    thread.setDaemon(True)
+    thread.start()
     wait(port, up=True)
-    return port
+    return port, thread
 
-def stop_server(url):
+def start_server(tree):
+    return _start_server(tree)[0]
+
+def stop_server(url, thread=None):
     try:
         urllib2.urlopen(url+'__stop__')
     except Exception:
         pass
+    if thread is not None:
+        thread.join() # wait for thread to stop
 
+def setUpServer(test, tree):
+    port, thread = _start_server(tree, name=test.name)
+    link_server = 'http://localhost:%s/' % port
+    test.globs['link_server'] = link_server
+    test.globs['__tearDown__'].append(lambda: stop_server(link_server, thread))
+        
+
 def wait(port, up):
     addr = 'localhost', port
     for i in range(120):



More information about the Checkins mailing list