Skip to content

Commit

Permalink
Fix some test issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
ionelmc committed Oct 19, 2015
1 parent e44f1db commit 57676ae
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 36 deletions.
5 changes: 3 additions & 2 deletions src/stampede.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import signalfd

__version__ = '0.0.2'

logger = getLogger(__name__)

SO_PEERCRED = 17
Expand Down Expand Up @@ -112,9 +114,8 @@ def do_work(self, workspace_name):
raise NotImplementedError()

def handle_signal(self, child_signals):
si = signalfd.siginfo()
try:
child_signals.readinto(si)
si = signalfd.read_siginfo(child_signals)
except IOError as exc:
if exc.errno == errno.EAGAIN:
logger.critical("Got %s for child_fd:%s", exc, child_signals)
Expand Down
2 changes: 1 addition & 1 deletion tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


def work_dispatch(self, workspace_name):
test_name = sys.argv[2]
test_name = sys.argv[1]
if test_name == 'test_simple':
logging.critical('JOB %s EXECUTED', workspace_name.decode('ascii'))
elif test_name == 'test_fail':
Expand Down
69 changes: 36 additions & 33 deletions tests/test_stampede.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import socket
import sys
import time
from contextlib import closing, contextmanager
from contextlib import closing
from contextlib import contextmanager

from process_tests import TestProcess
import pytest
from process_tests import TestProcess
from process_tests import dump_on_error
from process_tests import wait_for_strings

UDS_PATH = '/tmp/stampede-tests.sock'
HELPER = os.path.join(os.path.dirname(__file__), "helper.py")
Expand All @@ -15,7 +18,7 @@


@contextmanager
def test_connection(timeout=1):
def connection(timeout=1):
with closing(socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)) as sock:
sock.settimeout(timeout)
sock.connect(UDS_PATH)
Expand All @@ -27,58 +30,58 @@ def test_connection(timeout=1):
yield fh


def test_simple(self):
def test_simple():
with TestProcess(sys.executable, HELPER, 'test_simple') as proc:
with self.dump_on_error(proc.read):
self.wait_for_strings(proc.read, TIMEOUT, 'Queues =>')
with test_connection() as fh:
with dump_on_error(proc.read):
wait_for_strings(proc.read, TIMEOUT, 'Queues =>')
with connection() as fh:
fh.write(b"first")
fh.write(b"-second\n")
line = fh.readline()
assert line.startswith(b'done (job:'), line
self.wait_for_strings(proc.read, TIMEOUT,
wait_for_strings(proc.read, TIMEOUT,
'%s:%s' % (pwd.getpwuid(os.getuid())[0], os.getpid()),
'JOB first-second EXECUTED',
'completed. Passing back results to',
'Queues => 0 workspaces',
)


def test_fail(self):
def test_fail():
with TestProcess(sys.executable, HELPER, 'test_fail') as proc:
with self.dump_on_error(proc.read):
self.wait_for_strings(proc.read, TIMEOUT, 'Queues =>')
with test_connection() as fh:
with dump_on_error(proc.read):
wait_for_strings(proc.read, TIMEOUT, 'Queues =>')
with connection() as fh:
fh.write(b"first")
fh.write(b"-second\n")
line = fh.readline()
assert line.startswith(b'done (job:'), line
self.wait_for_strings(proc.read, TIMEOUT,
wait_for_strings(proc.read, TIMEOUT,
'%s:%s' % (pwd.getpwuid(os.getuid())[0], os.getpid()),
'Failed job',
'Exception: FAIL',
'Queues => 0 workspaces',
)


def test_incomplete_request(self):
def test_incomplete_request():
with TestProcess(sys.executable, HELPER, 'test_simple') as proc:
with self.dump_on_error(proc.read):
self.wait_for_strings(proc.read, TIMEOUT, 'Queues =>')
with test_connection(2) as fh:
with dump_on_error(proc.read):
wait_for_strings(proc.read, TIMEOUT, 'Queues =>')
with connection(2) as fh:
fh.write(b"first")
line = fh.readline()
assert line == b''
self.wait_for_strings(proc.read, TIMEOUT,
wait_for_strings(proc.read, TIMEOUT,
'Failed to read request from client %s:%s' % (
pwd.getpwuid(os.getuid())[0], os.getpid()),
)


def test_queue_collapse(self):
def test_queue_collapse():
with TestProcess(sys.executable, HELPER, 'test_queue_collapse') as proc:
with self.dump_on_error(proc.read):
self.wait_for_strings(proc.read, TIMEOUT, 'Queues =>')
with dump_on_error(proc.read):
wait_for_strings(proc.read, TIMEOUT, 'Queues =>')
clients = []
for _ in range(5):
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
Expand All @@ -95,35 +98,35 @@ def test_queue_collapse(self):
result = [fh.readline() for fh, _ in clients]
delta = time.time() - t1
if delta > TIMEOUT:
self.fail('Jobs took too much time (%0.2f sec)' % delta)
self.wait_for_strings(proc.read, TIMEOUT,
fail('Jobs took too much time (%0.2f sec)' % delta)
wait_for_strings(proc.read, TIMEOUT,
'test_queue_collapse OK',
'%s:%s' % (pwd.getpwuid(os.getuid())[0], os.getpid()),
)
finally:
[(fh.close(), sock.close()) for fh, sock in clients]


def test_timeout(self):
def test_timeout():
with TestProcess(sys.executable, HELPER, 'test_timeout') as proc:
with self.dump_on_error(proc.read):
self.wait_for_strings(proc.read, TIMEOUT, 'Queues =>')
with test_connection(3) as fh:
with dump_on_error(proc.read):
wait_for_strings(proc.read, TIMEOUT, 'Queues =>')
with connection(3) as fh:
fh.write(b"test_timeout\n")
line = fh.readline()
assert line.startswith(b'fail:14 (job:'), line
self.wait_for_strings(proc.read, TIMEOUT,
wait_for_strings(proc.read, TIMEOUT,
'%s:%s' % (pwd.getpwuid(os.getuid())[0], os.getpid()),
'test_timeout STARTED',
'completed. Passing back results to',
)
assert 'test_timeout FAIL' not in proc.read()


def test_bad_client(self):
def test_bad_client():
with TestProcess(sys.executable, HELPER, 'test_simple') as proc:
with self.dump_on_error(proc.read):
self.wait_for_strings(proc.read, TIMEOUT, 'Queues =>')
with dump_on_error(proc.read):
wait_for_strings(proc.read, TIMEOUT, 'Queues =>')
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.settimeout(1)
sock.connect(UDS_PATH)
Expand All @@ -137,15 +140,15 @@ def test_bad_client(self):
sock.close()
time.sleep(0.2)
assert proc.is_alive
self.wait_for_strings(proc.read, TIMEOUT,
wait_for_strings(proc.read, TIMEOUT,
'%s:%s' % (pwd.getpwuid(os.getuid())[0], os.getpid()),
'JOB first-second EXECUTED',
'completed. Passing back results to',
'Failed to send response to ',
)


def test_highlander(self):
def test_highlander():
from stampede import StampedeWorker
man = StampedeWorker()
with pytest.raises(RuntimeError):
Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ passenv =
*
deps =
pytest
process-tests==1.2.0
signalfd==0.4.0
cover: pytest-cov
commands =
nocov: {posargs:py.test -vv --ignore=src}
Expand Down

0 comments on commit 57676ae

Please sign in to comment.