Skip to content
Browse files

make flaky test more robust.

  • Loading branch information...
1 parent 3785ab2 commit 098c6954a795e71154102b3625cae0fedc1fb97a schettino72 committed
Showing with 39 additions and 7 deletions.
  1. +12 −0 tests/conftest.py
  2. +27 −7 tests/test_cmd_auto.py
View
12 tests/conftest.py
@@ -35,6 +35,18 @@ def remove_dependency():
return path
+# fixture to create a sample file to be used as file_dep
+@pytest.fixture
+def target1(request):
+ path = get_abspath("data/target1")
+ if os.path.exists(path):
+ os.remove(path)
+ def remove_path():
+ if os.path.exists(path):
+ os.remove(path)
+ request.addfinalizer(remove_path)
+ return path
+
# fixture for "doit.db". create/remove for every test
def remove_db(filename):
View
34 tests/test_cmd_auto.py
@@ -52,26 +52,46 @@ def test_invalid_args(self, dependency1, depfile_name):
pytest.raises(InvalidCommand, cmd.execute, None, 't2')
- def test_run_wait(self, dependency1, depfile_name):
- t1 = Task("t1", [""], file_dep=[dependency1])
+ def test_run_wait(self, dependency1, target1, depfile_name):
+ def ok():
+ with open(target1, 'w') as fp:
+ fp.write('ok')
+ t1 = Task("t1", [ok], file_dep=[dependency1])
cmd = cmd_auto.Auto(task_loader=FakeLoader([t1], depfile_name))
run_wait_proc = Process(target=cmd.run_watch,
args=(DefaultUpdate(), []))
run_wait_proc.start()
- # wait for task to run
- time.sleep(.5)
+ # wait until task is executed
+ for x in range(5):
+ try:
+ got = open(target1, 'r').read()
+ if got == 'ok':
+ break
+ except:
+ pass
+ time.sleep(0.1)
+ else: # pragma: no cover
+ raise Exception("target not created")
# write on file to terminate process
fd = open(dependency1, 'w')
fd.write("hi" + str(time.asctime()))
fd.close()
- run_wait_proc.join(1)
+ run_wait_proc.join(.2)
if run_wait_proc.is_alive(): # pragma: no cover
- run_wait_proc.terminate()
- raise Exception("process not terminated")
+ # this test is very flaky so we give it one more chance...
+ # write on file to terminate process
+ fd = open(dependency1, 'w')
+ fd.write("hi" + str(time.asctime()))
+ fd.close()
+
+ run_wait_proc.join(.2)
+ if run_wait_proc.is_alive(): # pragma: no cover
+ run_wait_proc.terminate()
+ raise Exception("process not terminated")
assert 0 == run_wait_proc.exitcode

0 comments on commit 098c695

Please sign in to comment.
Something went wrong with that request. Please try again.