-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from tomislater/fix-tests
Fix tests and HTTPCoordinatedExecutor class
- Loading branch information
Showing
8 changed files
with
151 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from .simple_executor import SimpleExecutor | ||
from .simple_executor import SimpleExecutor, TimeoutExpired | ||
from .output_coordinated_executor import OutputCoordinatedExecutor | ||
from .tcp_coordinated_executor import TCPCoordinatedExecutor | ||
from .http_coordinated_executor import HTTPCoordinatedExecutor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 48 additions & 4 deletions
52
summon_process/tests/executors/test_http_coordinated_executor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,62 @@ | ||
import os | ||
|
||
from unittest import TestCase | ||
from httplib import HTTPConnection, OK | ||
from summon_process.executors import HTTPCoordinatedExecutor | ||
|
||
from summon_process.executors import HTTPCoordinatedExecutor, TimeoutExpired | ||
|
||
|
||
class TestHTTPCoordinatedExecutor(TestCase): | ||
host = "127.0.0.1" | ||
port = "8000" | ||
|
||
def test_it_waits_for_process_to_complete_head_request(self): | ||
command = 'bash -c "sleep 3 && python -m SimpleHTTPServer"' | ||
executor = HTTPCoordinatedExecutor(command, 'http://127.0.0.1:8000/') | ||
command = 'bash -c "sleep 3 && exec python -m SimpleHTTPServer"' | ||
executor = HTTPCoordinatedExecutor( | ||
command, 'http://{0}:{1}/'.format(self.host, self.port) | ||
) | ||
executor.start() | ||
assert executor.running() | ||
|
||
conn = HTTPConnection(self.host, self.port) | ||
conn.request('GET', '/') | ||
assert conn.getresponse().status is OK | ||
conn.close() | ||
|
||
executor.stop() | ||
|
||
def prepare_slow_server_executor(self, timeout=None): | ||
slow_server = os.path.join( | ||
os.path.abspath(os.path.dirname(__file__)), | ||
"../slow_server.py" | ||
) | ||
|
||
command = 'python {0}'.format(slow_server) | ||
|
||
return HTTPCoordinatedExecutor( | ||
command, | ||
'http://{0}:{1}/'.format(self.host, self.port), | ||
timeout=timeout, | ||
) | ||
|
||
def test_slow_server_response(self): | ||
""" | ||
Simple example. You run gunicorn, gunicorn is working | ||
but you have to wait for worker procesess. | ||
""" | ||
executor = self.prepare_slow_server_executor() | ||
executor.start() | ||
assert executor.running() | ||
|
||
conn = HTTPConnection('127.0.0.1', '8000') | ||
conn = HTTPConnection(self.host, self.port) | ||
conn.request('GET', '/') | ||
|
||
assert conn.getresponse().status is OK | ||
|
||
conn.close() | ||
executor.stop() | ||
|
||
def test_slow_server_response_with_timeout(self): | ||
executor = self.prepare_slow_server_executor(timeout=1) | ||
self.assertRaises(TimeoutExpired, executor.start) | ||
executor.stop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import time | ||
|
||
from BaseHTTPServer import ( | ||
HTTPServer, | ||
BaseHTTPRequestHandler, | ||
) | ||
|
||
class SlowServerHandler(BaseHTTPRequestHandler): | ||
|
||
wait = 5 | ||
|
||
def do_GET(self): | ||
time.sleep(self.wait) | ||
self.send_response(200) | ||
self.send_header("Content-type", "text/html") | ||
self.end_headers() | ||
self.wfile.write('Hi. I am very slow.') | ||
return | ||
|
||
def do_HEAD(self): | ||
time.sleep(self.wait) | ||
self.send_response(200) | ||
self.end_headers() | ||
return | ||
|
||
|
||
server = HTTPServer( | ||
('127.0.0.1', 8000), | ||
SlowServerHandler | ||
) | ||
server.serve_forever() |