modinfo in the translate toolkit is now a 2-tuple containing the mtime and
[pootle.git] / test_cmdlineserver.py
blobd2ae939c6d535106ef7170fa055c06d4e54b8ce4
1 #!/usr/bin/env python
3 """Server environment for testing simplewebserver (command line web server)
4 This uses the basic environment from test_create and runs a web server in a separate thread
5 The top-level tests are then in test_client / test_ie / test_mozilla etc
6 These can then be layered on top of this server"""
8 from Pootle import test_create
9 from jToolkit.web import simplewebserver
10 from jToolkit import errors
11 import urllib2
12 import time
14 class TestCmdlineServer(test_create.NoReuse):
15 # this is called from setup_class in test_create.TestCreate
16 def setup_webserver(self):
17 """setup the webserver that will be used for the tests"""
18 print "setting up web server"
19 webserverclass = simplewebserver.jToolkitHTTPServer(simplewebserver.ThreadedHTTPServer)
20 options = simplewebserver.WebOptionParser().parse_args([])[0]
21 options.port = 0
22 options.servertype = 'standard'
23 errorhandler = errors.ConsoleErrorHandler()
24 webserver = webserverclass(options, errorhandler)
25 return webserver
27 def setup_method(self, method):
28 """starts a new simplewebserver in a separate thread"""
29 try:
30 test_create.NoReuse.setup_method(self, method)
31 except Exception, e:
32 print "exception in test_create setup_method:", e
33 test_create.NoReuse.teardown_method(self, method)
34 raise
35 print "finished setup_method phase 1"
36 # self.webserver.options.port = self.port
37 self.baseaddress = "http://%s:%d/" % (self.webserver.hostname, self.webserver.port)
38 ThreadClass = self.webserver.ThreadClass
39 self.webserverthread = ThreadClass(target = simplewebserver.run, name="webserver", args=(self.server, self.webserver.options))
40 self.webserverthread.start()
41 # wait until it actually is started
42 waited = 0.0
43 sleep = 0.001
44 maxwait = 3.0
45 while not hasattr(self.webserver, "stop"):
46 time.sleep(sleep)
47 waited += sleep
48 if sleep > maxwait:
49 raise RuntimeError("webserver failed to start in %0.1f seconds" % maxwait)
51 def teardown_method(self, method):
52 """close the web server for this method"""
53 try:
54 self.server.sessioncache.clear()
55 self.webserver.setstop(True)
56 # ping the webserver to make it stop
57 waited = 0.0
58 sleep = 0.05
59 max_wait = 5.0
60 while self.webserverthread.isAlive():
61 time.sleep(sleep)
62 if not self.webserverthread.isAlive():
63 break
64 # if the webserver is sleeping a request should wake it up...
65 # try:
66 # x = urllib2.urlopen(self.baseaddress)
67 # except Exception, e:
68 # pass
69 waited += sleep
70 sleep += 0.05
71 if waited >= max_wait:
72 raise RuntimeError("webserver failed to stop in %0.1f seconds" % (waited))
73 finally:
74 test_create.NoReuse.teardown_method(self, method)