Skip to content

Commit d8267ec

Browse files
committed
Make sure clients are only run on started
1 parent 2360041 commit d8267ec

File tree

1 file changed

+98
-96
lines changed

1 file changed

+98
-96
lines changed

test/plugs.js

Lines changed: 98 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,19 @@ function echo (stream) {
7575
}
7676

7777
tape('combined', function (t) {
78-
var close = combined.server(echo)
79-
80-
combined.client(combined.stringify('device'), function (err, stream) {
81-
if(err) throw err
82-
pull(
83-
pull.values([Buffer.from('hello world')]),
84-
stream,
85-
pull.collect(function (err, ary) {
86-
if(err) throw err
87-
t.equal(Buffer.concat(ary).toString(), 'HELLO WORLD')
88-
close(t.end)
89-
})
90-
)
78+
var close = combined.server(echo, null, () => {
79+
combined.client(combined.stringify('device'), function (err, stream) {
80+
if(err) throw err
81+
pull(
82+
pull.values([Buffer.from('hello world')]),
83+
stream,
84+
pull.collect(function (err, ary) {
85+
if(err) throw err
86+
t.equal(Buffer.concat(ary).toString(), 'HELLO WORLD')
87+
close(t.end)
88+
})
89+
)
90+
})
9191
})
9292
})
9393

@@ -100,22 +100,23 @@ if (has_ipv6)
100100
}),
101101
shs
102102
])
103-
var close = combined.server(echo)
104-
var addr = combined.stringify('device')
105-
console.log('addr', addr)
103+
var close = combined.server(echo, null, () => {
104+
var addr = combined.stringify('device')
105+
console.log('addr', addr)
106106

107-
combined.client(addr, function (err, stream) {
108-
if(err) throw err
109-
t.ok(stream.address, 'has an address')
110-
pull(
111-
pull.values([Buffer.from('hello world')]),
112-
stream,
113-
pull.collect(function (err, ary) {
114-
if(err) throw err
115-
t.equal(Buffer.concat(ary).toString(), 'HELLO WORLD')
116-
close(t.end)
117-
})
118-
)
107+
combined.client(addr, function (err, stream) {
108+
if(err) throw err
109+
t.ok(stream.address, 'has an address')
110+
pull(
111+
pull.values([Buffer.from('hello world')]),
112+
stream,
113+
pull.collect(function (err, ary) {
114+
if(err) throw err
115+
t.equal(Buffer.concat(ary).toString(), 'HELLO WORLD')
116+
close(t.end)
117+
})
118+
)
119+
})
119120
})
120121
})
121122

@@ -148,24 +149,24 @@ tape('net: do not listen on all addresses', function (t) {
148149
}),
149150
shs
150151
])
151-
var close = combined.server(echo)
152-
153-
//fake
154-
var fake_combined = Compose([
155-
Net({
156-
scope: 'local',
157-
port: 4851,
158-
//host: 'localhost',
159-
// external: scopes.host('local') // unroutable IP, but not localhost (e.g. 192.168 ...)
160-
}),
161-
shs
162-
])
152+
var close = combined.server(echo, null, () => {
153+
//fake
154+
var fake_combined = Compose([
155+
Net({
156+
scope: 'local',
157+
port: 4851,
158+
//host: 'localhost',
159+
// external: scopes.host('local') // unroutable IP, but not localhost (e.g. 192.168 ...)
160+
}),
161+
shs
162+
])
163163

164-
var addr = fake_combined.stringify('local') // returns external
165-
console.log('addr local scope', addr)
166-
combined.client(addr, function (err, stream) {
167-
t.ok(err, 'should only listen on localhost')
168-
close(t.end)
164+
var addr = fake_combined.stringify('local') // returns external
165+
console.log('addr local scope', addr)
166+
combined.client(addr, function (err, stream) {
167+
t.ok(err, 'should only listen on localhost')
168+
close(t.end)
169+
})
169170
})
170171
})
171172

@@ -178,7 +179,7 @@ tape('net: do not crash if listen() fails', function(t) {
178179
}),
179180
shs
180181
])
181-
var close = combined.server(echo, function() {}, function(err) {
182+
var close = combined.server(echo, null, function(err) {
182183
t.ok(err, 'should propagate listen error up')
183184
t.match(err.code, /^(ENOTFOUND|EAI_AGAIN)$/, 'the error is expected')
184185
close(() => t.end())
@@ -207,22 +208,23 @@ tape('combined, unix', function (t) {
207208
}),
208209
shs
209210
])
210-
var close = combined.server(echo)
211-
var addr = combined.stringify('device')
212-
console.log('unix addr', addr)
211+
var close = combined.server(echo, null, () => {
212+
var addr = combined.stringify('device')
213+
console.log('unix addr', addr)
213214

214-
combined.client(addr, function (err, stream) {
215-
if(err) throw err
216-
t.ok(stream.address, 'has an address')
217-
pull(
218-
pull.values([Buffer.from('hello world')]),
219-
stream,
220-
pull.collect(function (err, ary) {
221-
if(err) throw err
222-
t.equal(Buffer.concat(ary).toString(), 'HELLO WORLD')
223-
close(t.end)
224-
})
225-
)
215+
combined.client(addr, function (err, stream) {
216+
if(err) throw err
217+
t.ok(stream.address, 'has an address')
218+
pull(
219+
pull.values([Buffer.from('hello world')]),
220+
stream,
221+
pull.collect(function (err, ary) {
222+
if(err) throw err
223+
t.equal(Buffer.concat(ary).toString(), 'HELLO WORLD')
224+
close(t.end)
225+
})
226+
)
227+
})
226228
})
227229
})
228230

@@ -263,27 +265,28 @@ tape('error if try to connect on wrong protocol', function (t) {
263265
})
264266

265267
tape('shs with seed', function (t) {
266-
var close = combined.server(echo)
267-
268-
var seed = cl.crypto_hash_sha256(Buffer.from('TEST SEED'))
269-
var bob = cl.crypto_sign_seed_keypair(seed)
270-
271-
var checked
272-
check = function (id, cb) {
273-
checked = id
274-
if(id.toString('base64') === bob.publicKey.toString('base64'))
275-
cb(null, true)
276-
else
277-
cb(null, false)
278-
}
268+
var close = combined.server(echo, null, () => {
269+
270+
var seed = cl.crypto_hash_sha256(Buffer.from('TEST SEED'))
271+
var bob = cl.crypto_sign_seed_keypair(seed)
272+
273+
var checked
274+
check = function (id, cb) {
275+
checked = id
276+
if(id.toString('base64') === bob.publicKey.toString('base64'))
277+
cb(null, true)
278+
else
279+
cb(null, false)
280+
}
279281

280-
var addr_with_seed = combined.stringify()+':'+seed.toString('base64')
282+
var addr_with_seed = combined.stringify()+':'+seed.toString('base64')
281283

282-
combined.client(addr_with_seed, function (err, stream) {
283-
t.notOk(err)
284-
t.deepEqual(checked, bob.publicKey)
285-
stream.source(true, function () {})
286-
close(t.end)
284+
combined.client(addr_with_seed, function (err, stream) {
285+
t.notOk(err)
286+
t.deepEqual(checked, bob.publicKey)
287+
stream.source(true, function () {})
288+
close(t.end)
289+
})
287290
})
288291
})
289292

@@ -386,23 +389,23 @@ function testAbort (name, combined) {
386389
tape(name+', aborted', function (t) {
387390
var close = combined.server(function onConnection() {
388391
throw new Error('should never happen')
389-
})
390-
391-
var abort = combined.client(combined.stringify(), function (err, stream) {
392-
t.ok(err, 'the error is expected')
392+
}, null, () => {
393+
var abort = combined.client(combined.stringify(), function (err, stream) {
394+
t.ok(err, 'the error is expected')
395+
396+
// NOTE: without the timeout, we try to close the server
397+
// before it actually started listening, which fails and then
398+
// the server keeps runnung, causing the next test to fail with EADDRINUSE
399+
//
400+
// This is messy, combined.server should be a proper async call
401+
setTimeout( function() {
402+
//console.log('Calling close')
403+
close(t.end)
404+
}, 500)
405+
})
393406

394-
// NOTE: without the timeout, we try to close the server
395-
// before it actually started listening, which fails and then
396-
// the server keeps runnung, causing the next test to fail with EADDRINUSE
397-
//
398-
// This is messy, combined.server should be a proper async call
399-
setTimeout( function() {
400-
//console.log('Calling close')
401-
close(t.end)
402-
}, 500)
407+
abort()
403408
})
404-
405-
abort()
406409
})
407410
}
408411

@@ -422,7 +425,6 @@ tape('error should have client address on it', function (t) {
422425
//the shs address won't actually parse, because it doesn't have the key in it
423426
//because the key is not known in a wrong number.
424427
}, function () {
425-
426428
//very unlikely this is the address, which will give a wrong number at the server.
427429
var addr = combined.stringify().replace(/shs:......../, 'shs:XXXXXXXX')
428430
combined.client(addr, function (err, stream) {

0 commit comments

Comments
 (0)