Detect resource leaks
mizosoft opened this issue ยท 0 comments
Hi! First of all, thanks for this library ๐๐พ.
So... I use Jimfs in my tests and I reckon that's what many people do. I think it'd be fruitful if Jimfs could detect open resources that aren't released properly (by resources I mean anything created by the FS that's implementing Closeable
, including channels, directory streams, etc...). This makes it really easy to ensure tested components properly close file resources. I realize it'll be silly if this suddenly became the default behavior, so I'd expect this to be explicitly enabled in the FS's Configuration
. It'd be preferable if this could be checked in FileSystem::close
, so the following pattern can be utilized (depending on the testing framework):
private FileSystem fs;
@BeforeEach
void setUp() {
var config = Configuration.builder(PathType.unix()).setDetectLeaksOnClosure(true).build();
fs = Jimfs.newFileSystem(config);
}
@AfterEach
void tearDown() throws IOException {
if (fs != null) {
fs.close(); // Throws in case of resource leaks
}
}
...
I notice resource registration is handled by the FileSystemState
class. Currently, it seems to only close registered resources. I think most behavior could be implemented there, but you guys know better. The class seems to receive possibly custom Closeable
instances, so it may need to check the classes of registered resources before complaining. Perhaps, specific resource types could explicitly opt-in for the checks by the configuration (e.g. only check FileChannels).
Thanks in advance!