Make heartbeat unit tests a bit more robust.

This commit is contained in:
John Chilton
2019-04-04 15:08:52 -04:00
parent eba0eb6f08
commit 55f58cb627
@@ -27,17 +27,38 @@ def test_database_heartbeat(heartbeat_app):
active_processes = heartbeat_app.database_heartbeat.get_active_processes()
assert len(active_processes) == 0
heartbeat_app.database_heartbeat.start()
def one_active_process():
active_processes = heartbeat_app.database_heartbeat.get_active_processes()
assert len(active_processes) == 1
process = active_processes[0]
return process
# thread needs to start
time.sleep(0.5)
active_processes = heartbeat_app.database_heartbeat.get_active_processes()
assert len(active_processes) == 1
process = active_processes[0]
process = wait_for_assertion(one_active_process)
update_time = process.update_time
time.sleep(0.2)
heartbeat_app.model.context.refresh(process)
next_update_time = process.update_time
assert update_time < next_update_time
def process_updated():
heartbeat_app.model.context.refresh(process)
next_update_time = process.update_time
assert update_time < next_update_time
wait_for_assertion(process_updated)
heartbeat_app.database_heartbeat.shutdown()
time.sleep(0.5)
assert len(heartbeat_app.database_heartbeat.get_active_processes(last_seen_seconds=5)) == 1
assert len(heartbeat_app.database_heartbeat.get_active_processes(last_seen_seconds=0.4)) == 0
def wait_for_assertion(assert_f):
assertion_error = None
for i in range(10):
try:
v = assert_f()
return v
except AssertionError as e:
assertion_error = e
time.sleep(.2)
raise assertion_error