Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. var fs = require('graceful-fs')
  2. var test = require('tap').test
  3. var path = require('path')
  4. var writeStream = require('../index.js')
  5. var rename = fs.rename
  6. fs.rename = function (from, to, cb) {
  7. setTimeout(function () {
  8. rename(from, to, cb)
  9. }, 100)
  10. }
  11. test('basic', function (t) {
  12. // open 10 write streams to the same file.
  13. // then write to each of them, and to the target
  14. // and verify at the end that each of them does their thing
  15. var target = path.resolve(__dirname, 'test.txt')
  16. var n = 10
  17. // We run all of our assertions twice:
  18. // once for finish, once for close
  19. // There are 6 assertions, two fixed, plus 4 lines in the file.
  20. t.plan(n * 2 * 6)
  21. var streams = []
  22. for (var i = 0; i < n; i++) {
  23. var s = writeStream(target)
  24. s.on('finish', verifier('finish', i))
  25. s.on('close', verifier('close', i))
  26. streams.push(s)
  27. }
  28. function verifier (ev, num) {
  29. return function () {
  30. if (ev === 'close') {
  31. t.equal(this.__emittedFinish, true, num + '. closed only after finish')
  32. } else {
  33. this.__emittedFinish = true
  34. t.equal(ev, 'finish', num + '. finished')
  35. }
  36. // make sure that one of the atomic streams won.
  37. var res = fs.readFileSync(target, 'utf8')
  38. var lines = res.trim().split(/\n/)
  39. lines.forEach(function (line, lineno) {
  40. var first = lines[0].match(/\d+$/)[0]
  41. var cur = line.match(/\d+$/)[0]
  42. t.equal(cur, first, num + '. line ' + lineno + ' matches')
  43. })
  44. var resExpr = /^first write \d+\nsecond write \d+\nthird write \d+\nfinal write \d+\n$/
  45. t.similar(res, resExpr, num + '. content matches')
  46. }
  47. }
  48. // now write something to each stream.
  49. streams.forEach(function (stream, i) {
  50. stream.write('first write ' + i + '\n')
  51. })
  52. // wait a sec for those writes to go out.
  53. setTimeout(function () {
  54. // write something else to the target.
  55. fs.writeFileSync(target, 'brutality!\n')
  56. // write some more stuff.
  57. streams.forEach(function (stream, i) {
  58. stream.write('second write ' + i + '\n')
  59. })
  60. setTimeout(function () {
  61. // Oops! Deleted the file!
  62. fs.unlinkSync(target)
  63. // write some more stuff.
  64. streams.forEach(function (stream, i) {
  65. stream.write('third write ' + i + '\n')
  66. })
  67. setTimeout(function () {
  68. fs.writeFileSync(target, 'brutality TWO!\n')
  69. streams.forEach(function (stream, i) {
  70. stream.end('final write ' + i + '\n')
  71. })
  72. }, 50)
  73. }, 50)
  74. }, 50)
  75. })
  76. test('cleanup', function (t) {
  77. fs.readdirSync(__dirname).filter(function (f) {
  78. return f.match(/^test.txt/)
  79. }).forEach(function (file) {
  80. fs.unlinkSync(path.resolve(__dirname, file))
  81. })
  82. t.end()
  83. })