Skip to content

Commit

Permalink
Add a file.close method
Browse files Browse the repository at this point in the history
The method closes the file and returns a promise that will resolve to
null once the operation is complete.

Closing a file is a no-op in k6, as we don't have to worry about file
descriptors. However, we still expose this method to the user to be
consistent with the existing APIs such as Node's or Deno's.

The promise will resolve to null, regardless of whether the file was
previously opened or not.

One of the reasons this method is currently is a no-op is that as of
today (v0.46), k6 does not support opening files in the VU context. As
a result, the file is always opened in the init context, and thus
closed when the init context is closed. Any attempt of clever strategies
attempting to limit long-lived files' content in memory (e.g reference
counting the VU instances of a file, and releasing the memory once the
count reaches zero) would thus be premature.
  • Loading branch information
oleiade committed Oct 10, 2023
1 parent 835b48f commit afdef39
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/experimental/fs/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ export default async function () {
// Seek back to the beginning of the file
await file.seek(0, SeekMode.Start);
}

export async function teardown() {
file.close();
}
30 changes: 30 additions & 0 deletions js/modules/k6/experimental/fs/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,33 @@ func (f *File) Seek(offset goja.Value, whence goja.Value) *goja.Promise {

return promise
}

// Close closes the file and returns a promise that will resolve to null
// once the operation is complete.
//
// Closing a file is a no-op in k6, as we don't have to worry about file
// descriptors. However, we still expose this method to the user to be
// consistent with the existing APIs such as Node's or Deno's.
//
// The promise will resolve to null, regardless of whether the file was
// previously opened or not.
//
// One of the reasons this method is currently is a no-op is that as of
// today (v0.46), k6 does not support opening files in the VU context. As
// a result, the file is always opened in the init context, and thus
// closed when the init context is closed. Any attempt of clever strategies
// attempting to limit long-lived files' content in memory (e.g reference
// counting the VU instances of a file, and releasing the memory once the
// count reaches zero) would thus be premature.
//
// TODO: reevaluate a more sophisticated strategy once we support opening
// files in the VU context.
func (f *File) Close() *goja.Promise {
promise, resolve, _ := promises.New(f.vu)

go func() {
resolve(goja.Null())
}()

return promise
}

0 comments on commit afdef39

Please sign in to comment.