Notes posted by jimmybob
RSS feed
To use with factory_girl and prevent leaking file handles
As insane-dreamer noted, to use with factory_girl:
Factory :video_file do file { fixture_file_upload 'test.png', 'image/png' } end
However, I ran into an issue where one of our spec’s was creating a few hundred files and would then crash with:
Errno::EMFILE: Too many open files
If you look at the source code for fixture_file_upload, it creates a new file. I don’t know if these files were being leaked, or just not getting garbage collected, but they never got closed. Eventually they just piled up and the process hit the max open file limit.
So, you need to explicitly call close on the file handle returned from fixture_file_upload. However, you can’t call close in the block immediately after fixture_file_upload because the factory needs to be able to read from the stream in order to properly initialize ‘file’.
Instead what I had to do was perform the close on the ‘proxy’ in an after_create block, like so
Factory :video_file do file { fixture_file_upload 'test.png', 'image/png' } after_create do |video, proxy| proxy.file.close end end