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