Refactor tests to end event-loop