Fix resource leaks in the tests