Recently I put in place a new feature in our app that dealt with uploading file(s). It could we use a simple upload or a multi-part upload to cloud storage.
Testing this feature required the creation of dummy tests files with specific size in order to trigger (or not) the multi-part upload:
- Committing dummy files to our git repository was a definite no-no.
- Files should be thrown away after running the tests.
- Files creation should as fast as possible.
- The actual content of the file is not important.
The other requirements could be met with sparse files:
Sparse files are files stored in a file system where consecutive data blocks consisting of all zero-bytes (null-bytes) are compressed to nothing. There is often no reason to store lots of empty data, so the file system just records how long the sequence of empty data is instead of writing it out on the storage media. This optimization can save significant amounts of storage space for other purposes.
The following code generate a sparse file, open the file for writing, seeks a given position and adds
some bytes at the end. It leverages the
Since we don’t need to actually allocate disk space, large sparse files can be created in a relative short time, making a good fit for quick “unit” testing.
This feature is still experimental ↩︎