Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAleksey Lim <alsroot@sugarlabs.org>2012-01-15 01:18:30 (GMT)
committer Aleksey Lim <alsroot@sugarlabs.org>2012-01-15 01:18:30 (GMT)
commit235acae98ad8893c3139038962cb916a847dd368 (patch)
tree12cd4be28b870f701f4ce14ff84fee9cd1454bbd
parentf78bf4a43a6b6e0acacfa55419c3c0e9ec983516 (diff)
Switch tests to gevent server
-rw-r--r--tests/__init__.py35
-rwxr-xr-xtests/units/document.py25
2 files changed, 28 insertions, 32 deletions
diff --git a/tests/__init__.py b/tests/__init__.py
index 010c8c7..eb848b0 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -7,30 +7,19 @@ import signal
import shutil
import logging
import unittest
-import threading
-from wsgiref.simple_server import make_server
from os.path import dirname, join, exists, abspath
-import gobject
-import dbus.glib
-import dbus.mainloop.glib
import restkit
import active_document as ad
-import restful_document as rd
from active_document import env as _env
-gobject.threads_init()
-
root = abspath(dirname(__file__))
tmproot = join(root, '.tmp')
tmpdir = None
-gobject.threads_init()
-dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
-
def main():
shutil.rmtree(tmproot, ignore_errors=True)
@@ -72,6 +61,8 @@ class Test(unittest.TestCase):
ad.index_flush_timeout.value = 0
ad.index_flush_threshold.value = 0
+ self.httpd_seqno = 0
+
def tearDown(self):
while Test.httpd_pids:
self.httpdown(Test.httpd_pids.keys()[0])
@@ -113,6 +104,8 @@ class Test(unittest.TestCase):
if port in Test.httpd_pids:
self.httpdown(port)
+ self.httpd_seqno += 1
+
child_pid = os.fork()
if child_pid:
time.sleep(0.25)
@@ -121,16 +114,16 @@ class Test(unittest.TestCase):
for handler in logging.getLogger().handlers:
logging.getLogger().removeHandler(handler)
- logging.basicConfig(level=logging.DEBUG, filename=tmpdir + '.http.log')
-
- httpd = make_server('', port, rd.Router(classes))
- mainloop = gobject.MainLoop()
-
- httpd_thread = threading.Thread(target=httpd.serve_forever)
- httpd_thread.daemon = True
- httpd_thread.start()
-
- mainloop.run()
+ logging.basicConfig(level=logging.DEBUG, filename=tmpdir + '-%s.http.log' % self.httpd_seqno)
+
+ from gevent.wsgi import WSGIServer
+ import restful_document as rd
+ httpd = WSGIServer(('localhost', port), rd.Router(classes))
+ try:
+ httpd.serve_forever()
+ finally:
+ httpd.stop()
+ ad.close()
def httpdown(self, port):
pid = Test.httpd_pids[port]
diff --git a/tests/units/document.py b/tests/units/document.py
index 9e6cc91..912bb8c 100755
--- a/tests/units/document.py
+++ b/tests/units/document.py
@@ -54,6 +54,9 @@ class DocumentTest(tests.Test):
})).body_string()
)['guid']
+ reply = json.loads(rest.get('/document/' + guid_1).body_string())
+ del reply['ctime']
+ del reply['mtime']
self.assertEqual(
{'stored': 'stored',
'vote': '0',
@@ -61,9 +64,7 @@ class DocumentTest(tests.Test):
'counter': '0',
'guid': guid_1,
},
- json.loads(
- rest.get('/document/' + guid_1).body_string())
- )
+ reply)
guid_2 = json.loads(
rest.post('/document', payload=json.dumps({
@@ -72,6 +73,9 @@ class DocumentTest(tests.Test):
})).body_string()
)['guid']
+ reply = json.loads(rest.get('/document/' + guid_2).body_string())
+ del reply['ctime']
+ del reply['mtime']
self.assertEqual(
{'stored': 'stored2',
'vote': '0',
@@ -79,9 +83,7 @@ class DocumentTest(tests.Test):
'counter': '0',
'guid': guid_2,
},
- json.loads(
- rest.get('/document/' + guid_2).body_string())
- )
+ reply)
self.assertEqual(
{'total': 2,
@@ -97,6 +99,9 @@ class DocumentTest(tests.Test):
'term': 'term3',
}))
+ reply = json.loads(rest.get('/document/' + guid_2).body_string())
+ del reply['ctime']
+ del reply['mtime']
self.assertEqual(
{'stored': 'stored3',
'vote': '1',
@@ -104,9 +109,7 @@ class DocumentTest(tests.Test):
'counter': '1',
'guid': guid_2,
},
- json.loads(
- rest.get('/document/' + guid_2).body_string())
- )
+ reply)
self.assertEqual(
{'total': 2,
@@ -175,9 +178,9 @@ class DocumentTest(tests.Test):
]),
sorted(reply['documents']))
- time.sleep(1)
self.httpdown(8000)
- self.httpd(8000, [Document])
+ self.httpd(8001, [Document])
+ rest = Resource('http://localhost:8001')
reply = json.loads(rest.get('/document', reply='guid,stored,term,vote,counter').body_string())
self.assertEqual(2, reply['total'])