-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
126 lines (97 loc) · 2.78 KB
/
test.js
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
/* global Bare */
const test = require('brittle')
const Pipe = require('.')
const isWindows = Bare.platform === 'win32'
test('server + client', async (t) => {
t.plan(2)
const n = name()
const lc = t.test('lifecycle')
lc.plan(6)
const server = Pipe.createServer()
server
.on('close', () => t.pass('server closed'))
.on('connection', (pipe) => {
pipe
.on('close', () => lc.pass('server socket closed'))
.on('data', (data) => lc.alike(data, Buffer.from('hello server')))
.on('end', () => lc.pass('server ended'))
.end('hello client')
})
.listen(n)
const client = new Pipe(n)
client
.on('close', () => lc.pass('client socket closed'))
.on('data', (data) => lc.alike(data, Buffer.from('hello client')))
.on('end', () => lc.pass('client ended'))
.end('hello server')
await lc
server.close()
})
test('server + client, only server writes', async (t) => {
t.plan(2)
const n = name()
const lc = t.test('lifecycle')
lc.plan(5)
const server = Pipe.createServer()
server
.on('close', () => t.pass('server closed'))
.on('connection', (pipe) => {
pipe
.on('close', () => lc.pass('server socket closed'))
.on('data', (data) => lc.alike(data, Buffer.from('hello server')))
.on('end', () => lc.pass('server ended'))
.end('hello client')
})
.listen(n)
const client = new Pipe(n)
client
.on('close', () => lc.pass('client socket closed'))
.on('data', (data) => lc.alike(data, Buffer.from('hello client')))
.on('end', () => {
lc.pass('client ended')
client.end()
})
await lc
server.close()
})
test('server + client, only client writes', async (t) => {
t.plan(2)
const n = name()
const lc = t.test('lifecycle')
lc.plan(5)
const server = Pipe.createServer()
server
.on('close', () => t.pass('server closed'))
.on('connection', (pipe) => {
pipe
.on('close', () => lc.pass('server socket closed'))
.on('data', (data) => lc.alike(data, Buffer.from('hello server')))
.on('end', () => {
lc.pass('server ended')
pipe.end()
})
})
.listen(n)
const client = new Pipe(n)
client
.on('close', () => lc.pass('client socket closed'))
.on('end', () => lc.pass('client ended'))
.end('hello server')
await lc
server.close()
})
test('pipe', async (t) => {
t.plan(1)
const fds = Pipe.pipe()
const read = new Pipe(fds[0])
read.on('data', (data) => t.alike(data, Buffer.from('hello pipe')))
const write = new Pipe(fds[1])
write.end('hello pipe')
})
function name() {
const name =
'bare-pipe-' +
Math.random().toString(16).slice(2) +
Math.random().toString(16).slice(2)
return isWindows ? '\\\\.\\pipe\\' + name : '/tmp/' + name + '.sock'
}