tests: Move gc.collect() hack to helper function