You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

slow-close.js 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict'
  2. var fs = require('graceful-fs')
  3. var path = require('path')
  4. var test = require('tap').test
  5. var rimraf = require('rimraf')
  6. var writeStream = require('../index.js')
  7. var target = path.resolve(__dirname, 'test-chown')
  8. test('slow close', function (t) {
  9. t.plan(2)
  10. // The goal here is to simulate the "file close" step happening so slowly
  11. // that the whole close/rename process could finish before the file is
  12. // actually closed (and thus buffers truely flushed to the OS). In
  13. // previous versions of this module, this would result in the module
  14. // emitting finish & close before the file was fully written and in
  15. // turn, could break other layers that tried to read the new file.
  16. var realEmit = fs.WriteStream.prototype.emit
  17. var reallyClosed = false
  18. fs.WriteStream.prototype.emit = function (event) {
  19. if (event !== 'close') return realEmit.apply(this, arguments)
  20. setTimeout(function () {
  21. reallyClosed = true
  22. realEmit.call(this, 'close')
  23. }.bind(this), 200)
  24. }
  25. var stream = writeStream(target)
  26. stream.on('finish', function () {
  27. t.is(reallyClosed, true, "didn't finish before target was closed")
  28. })
  29. stream.on('close', function () {
  30. t.is(reallyClosed, true, "didn't close before target was closed")
  31. })
  32. stream.end()
  33. })
  34. test('cleanup', function (t) {
  35. rimraf.sync(target)
  36. t.end()
  37. })