File: test.js

package info (click to toggle)
node-end-of-stream 1.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 92 kB
  • sloc: makefile: 2
file content (139 lines) | stat: -rw-r--r-- 2,482 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var tape = require('tape')
var eos = require('./index')

var fs = require('fs')
var cp = require('child_process')
var net = require('net')
var http = require('http')
var stream = require('stream')

tape('fs writestream destory', function (t) {
	var ws = fs.createWriteStream('/dev/null');

	eos(ws, function(err) {
		t.ok(!!err)
		t.ok(this === ws)
		t.end()
	})

	ws.destroy();
})

tape('fs readstream destroy', function (t) {
	var rs1 = fs.createReadStream('/dev/urandom');

	eos(rs1, function(err) {
		t.ok(!!err)
		t.ok(this === rs1)
		t.end()
	})

	rs1.destroy()
})

tape('fs readstream pipe', function (t) {
	var rs2 = fs.createReadStream(__filename)

	eos(rs2, function(err) {
		t.ifError(err)
		t.ok(this === rs2)
		t.end()
	})

	rs2.pipe(fs.createWriteStream('/dev/null'))
})

tape('fs readstream cancel', function (t) {
	var rs3 = fs.createReadStream(__filename)

	eos(rs3, function(err) {
		t.fail('should not enter')
	})()

	rs3.pipe(fs.createWriteStream('/dev/null'))
	rs3.on('end', function () {
		t.end()
	})
})

tape('exec', function (t) {
	var exec = cp.exec('echo hello world')

	eos(exec, function (err) {
		t.ifError(err)
		t.ok(this === exec)
		t.end()
	})
})

tape('spawn', function (t) {
	var spawn = cp.spawn('echo', ['hello world']);
	eos(spawn, function (err) {
		t.ifError(err)
		t.ok(this === spawn)
		t.end()
	})
})

tape('tcp socket', function (t) {
	t.plan(5)

	var socket = net.connect(50000)

	eos(socket, function(err) {
	  t.ok(!!err)
	  t.ok(this === socket)
	})

	var server = net.createServer(function (socket) {
		eos(socket, function(err) {
			t.ok(!!err)
			t.ok(this === socket)
		})
		socket.destroy()
	}).listen(30000, function () {
		var socket = net.connect(30000)
		eos(socket, function() {
			t.ok(this === socket)
			server.close()
		})
	})
})

tape('http', function (t) {
	t.plan(2)

	var server2 = http.createServer(function(req, res) {
		eos(res, function(err) {
			t.ifError(err)
		})
		res.end()
	}).listen(function() {
		var port = server2.address().port
		http.get('http://localhost:' + port, function(res) {
			eos(res, function(err) {
				t.ifError(err)
				server2.close()
			})
			res.resume()
		})
	})
})

tape('end() and emit(close)', function (t) {
  if (!stream.Writable) return t.end()
  var ws = new stream.Writable()
  
  ws._write = function (data, enc, cb) {
    process.nextTick(cb)
  }

  eos(ws, function (err) {
    t.error(err, 'no error')
    t.end()
  })

  ws.write('hi')
  ws.end()
  ws.emit('close')
})