Merge pull request #12 from j1z0/master

Pull request to allow for setting number of threads in default cherrypy server
This commit is contained in:
Rafael Carício 2013-08-19 00:15:48 -07:00
commit 22fca66b1b
5 changed files with 29 additions and 9 deletions

View file

@ -78,13 +78,13 @@ class DjangoContext(Vows.Context):
class DjangoHTTPContext(DjangoContext): class DjangoHTTPContext(DjangoContext):
def start_server(self, host=None, port=None, settings={}): def start_server(self, host=None, port=None, settings={}, threads=1):
if not port: port = DEFAULT_PORT if not port: port = DEFAULT_PORT
if not host: host = DEFAULT_HOST if not host: host = DEFAULT_HOST
self.address = (host, port) self.address = (host, port)
self.server = DjangoServer(host, port) self.server = DjangoServer(host, port)
self.server.start(settings) self.server.start(settings,threads)
def __init__(self, parent): def __init__(self, parent):
super(DjangoHTTPContext, self).__init__(parent) super(DjangoHTTPContext, self).__init__(parent)

View file

@ -16,12 +16,12 @@ from cherrypy import wsgiserver
from django.core.handlers.wsgi import WSGIHandler from django.core.handlers.wsgi import WSGIHandler
def run_app(host, port): def run_app(host, port, thread_count):
server = wsgiserver.CherryPyWSGIServer( server = wsgiserver.CherryPyWSGIServer(
(host, port), (host, port),
WSGIHandler(), WSGIHandler(),
server_name='tornado-pyvows', server_name='tornado-pyvows',
numthreads = 1 numthreads = thread_count
) )
my_thread = current_thread() my_thread = current_thread()
@ -35,11 +35,11 @@ def run_app(host, port):
class DjangoServer(object): class DjangoServer(object):
def __init__(self, host, port): def __init__(self, host, port):
self.host = host self.host = host
self.port = port self.port = port
def start(self, settings): def start(self, settings, thread_count=1):
self.thr = Thread(target=run_app, args=(self.host, self.port)) self.thr = Thread(target= run_app, args=(self.host, self.port, thread_count))
self.thr.daemon = True self.thr.daemon = True
self.thr.settings = {} self.thr.settings = {}
for k, v in settings.iteritems(): for k, v in settings.iteritems():

View file

@ -1,3 +1,7 @@
django==1.3.1 django==1.3.1
pyvows==0.8.3 pyvows
coverage==3.5.1 coverage==3.5.1
httplib2
lxml
cssselect
cherrypy

View file

@ -57,6 +57,9 @@ class ContextTest(Vows.Context):
def setup(self): def setup(self):
self.start_server(port=8085) self.start_server(port=8085)
def should_default_to_one_thread(self,topic):
expect(self.server.thr.server._get_numthreads()).to_equal(1)
class WithinDjangoHTTPContextTheGetUrlMethod(DjangoHTTPContext): class WithinDjangoHTTPContextTheGetUrlMethod(DjangoHTTPContext):
@ -100,3 +103,15 @@ class ContextTest(Vows.Context):
def the_get_url_method_should_return_a_well_formed_url(self, topic): def the_get_url_method_should_return_a_well_formed_url(self, topic):
expect(topic).to_equal('http://127.0.0.1:8085/') expect(topic).to_equal('http://127.0.0.1:8085/')
class WithinAMultiThreadedServer(DjangoHTTPContext):
def setup(self):
self.start_server(threads=5)
def topic(self):
return self.server
def should_allow_user_to_specify_number_of_threads(self,topic):
expect(topic.thr.server._get_numthreads()).to_equal(5)

View file

@ -80,3 +80,4 @@ class HttpContextVows(DjangoHTTPContext):
def should_be_404(self, (topic, content)): def should_be_404(self, (topic, content)):
expect(topic.status).to_equal(404) expect(topic.status).to_equal(404)