test_crawl: make mock server print a line when ready, and wait for that line to start tests, instead of waiting for an arbitrary time

This commit is contained in:
Pablo Hoffman 2013-04-19 13:06:30 -03:00
parent ce177fa5bc
commit 626331b865
2 changed files with 12 additions and 7 deletions

View File

@ -77,7 +77,12 @@ class Root(Resource):
return 'Scrapy mock HTTP server\n'
root = Root()
factory = Site(root)
reactor.listenTCP(8998, factory)
reactor.run()
if __name__ == "__main__":
root = Root()
factory = Site(root)
port = reactor.listenTCP(8998, factory, interface="127.0.0.1")
def print_listening():
h = port.getHost()
print "Mock server running at http://%s:%d" % (h.host, h.port)
reactor.callWhenRunning(print_listening)
reactor.run()

View File

@ -1,7 +1,7 @@
import sys, time
from twisted.internet import defer
from twisted.trial.unittest import TestCase, SkipTest
from subprocess import Popen
from subprocess import Popen, PIPE
from scrapy.spider import BaseSpider
from scrapy.http import Request
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
@ -32,8 +32,8 @@ def docrawl(spider, settings=None):
class CrawlTestCase(TestCase):
def setUp(self):
self.proc = Popen([sys.executable, '-m', 'scrapy.tests.mockserver'])
time.sleep(0.5)
self.proc = Popen([sys.executable, '-u', '-m', 'scrapy.tests.mockserver'], stdout=PIPE)
self.proc.stdout.readline()
def tearDown(self):
self.proc.kill()