Skip to content

Commit

Permalink
Merge pull request ClearcodeHQ#18 from fizyk/tcp_sleep_fix
Browse files Browse the repository at this point in the history
removed leftover sleep TCPExecutor._wait_for_connection
  • Loading branch information
fizyk committed Jul 7, 2014
2 parents 3236f77 + 5e56c50 commit c5e84bf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

unreleased
-------

- [fix] removed leftover sleep from TCPExecutor._wait_for_connection

0.1.1
-------

Expand Down
2 changes: 0 additions & 2 deletions mirakuru/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"""TCP executor definition."""

import socket
import time
from mirakuru.base import Executor


Expand Down Expand Up @@ -70,5 +69,4 @@ def _wait_for_connection(self):
sock.connect((self.host, self.port))
return True
except (socket.error, socket.timeout):
time.sleep(1)
return False
4 changes: 2 additions & 2 deletions tests/executors/test_http_coordinated_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ def test_executor_starts_and_waits():
executor.stop()


def test_slow_server_response():
def test_slow_server_starting():
"""
Test whether or not executor awaits for slow responses.
Test whether or not executor awaits for slow starting servers.
Simple example. You run gunicorn, gunicorn is working
but you have to wait for worker procesess.
Expand Down
26 changes: 21 additions & 5 deletions tests/slow_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,40 @@ class SlowServerHandler(BaseHTTPRequestHandler):

"""Slow server handler."""

wait = 5
timeout = 2
endtime = None

def do_GET(self):
"""Serve GET request."""
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):
"""Serve HEAD request."""
time.sleep(self.wait)
self.send_response(200)
"""
Serve HEAD request.
but count to wait and return 500 response if wait time not exceeded
due to the fact, that HTTPServer will hang waiting for response
to return otherwise if none response will be returned.
"""
if self.count_timeout():
self.send_response(200)
else:
self.send_response(500)
self.end_headers()
return

def count_timeout(self):
"""Count down the timeout time."""
if SlowServerHandler.endtime is None:
SlowServerHandler.endtime = time.time() + SlowServerHandler.timeout
if time.time() < SlowServerHandler.endtime:
return False
else:
return True

server = HTTPServer(
('127.0.0.1', 8000),
Expand Down

0 comments on commit c5e84bf

Please sign in to comment.