Backport of test_connect_delay refactor from master