Skip to content

Commit fd8f02b

Browse files
authored
Fix flaky test (#1158)
* Fix flaky test * Fix #1154
1 parent 203bab2 commit fd8f02b

File tree

4 files changed

+59
-36
lines changed

4 files changed

+59
-36
lines changed

test/behavior/resurrect.test.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ test('Should execute the recurrect API with the ping strategy', t => {
4949
})
5050

5151
q.add((q, done) => {
52-
cluster.kill('node0')
53-
setTimeout(done, 100)
52+
cluster.kill('node0', done)
5453
})
5554

5655
q.add((q, done) => {
@@ -173,8 +172,7 @@ test('Should execute the recurrect API with the optimistic strategy', t => {
173172
})
174173

175174
q.add((q, done) => {
176-
cluster.kill('node0')
177-
setTimeout(done, 100)
175+
cluster.kill('node0', done)
178176
})
179177

180178
q.add((q, done) => {

test/behavior/sniff.test.js

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
const { test } = require('tap')
88
const { URL } = require('url')
9+
const lolex = require('lolex')
10+
const workq = require('workq')
911
const { buildCluster } = require('../utils')
1012
const { Client, Connection, Transport, events, errors } = require('../../index')
1113

@@ -111,8 +113,10 @@ test('Should handle hostnames in publish_address', t => {
111113
})
112114
})
113115

114-
test('Sniff interval', { skip: 'Flaky on CI' }, t => {
115-
t.plan(10)
116+
test('Sniff interval', t => {
117+
t.plan(11)
118+
const clock = lolex.install({ toFake: ['Date'] })
119+
const q = workq()
116120

117121
buildCluster(({ nodes, shutdown, kill }) => {
118122
const client = new Client({
@@ -132,19 +136,47 @@ test('Sniff interval', { skip: 'Flaky on CI' }, t => {
132136
})
133137

134138
t.strictEqual(client.connectionPool.size, 1)
135-
setTimeout(() => client.info(t.error), 60)
136139

137-
setTimeout(() => {
138-
// let's kill a node
139-
kill('node1')
140-
client.info(t.error)
141-
}, 150)
140+
q.add((q, done) => {
141+
clock.tick(51)
142+
client.info(err => {
143+
t.error(err)
144+
waitSniffEnd(() => {
145+
t.strictEqual(client.connectionPool.size, 4)
146+
done()
147+
})
148+
})
149+
})
150+
151+
q.add((q, done) => {
152+
kill('node1', done)
153+
})
142154

143-
setTimeout(() => {
144-
t.strictEqual(client.connectionPool.size, 3)
145-
}, 200)
155+
q.add((q, done) => {
156+
clock.tick(51)
157+
client.info(err => {
158+
t.error(err)
159+
waitSniffEnd(() => {
160+
t.strictEqual(client.connectionPool.size, 3)
161+
done()
162+
})
163+
})
164+
})
146165

147166
t.teardown(shutdown)
167+
168+
// it can happen that the sniff operation resolves
169+
// after the API call that trioggered it, so to
170+
// be sure that we are checking the connectionPool size
171+
// at the right moment, we verify that the transport
172+
// is no longer sniffing
173+
function waitSniffEnd (callback) {
174+
if (client.transport._isSniffing) {
175+
setTimeout(waitSniffEnd, 500, callback)
176+
} else {
177+
callback()
178+
}
179+
}
148180
})
149181
})
150182

test/unit/transport.test.js

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -578,9 +578,8 @@ test('Retry mechanism', t => {
578578
if (count > 0) {
579579
res.end(JSON.stringify({ hello: 'world' }))
580580
} else {
581-
setTimeout(() => {
582-
res.end(JSON.stringify({ hello: 'world' }))
583-
}, 1000)
581+
res.statusCode = 504
582+
res.end(JSON.stringify({ error: true }))
584583
}
585584
count++
586585
}
@@ -603,7 +602,6 @@ test('Retry mechanism', t => {
603602
connectionPool: pool,
604603
serializer: new Serializer(),
605604
maxRetries: 1,
606-
requestTimeout: 250,
607605
sniffInterval: false,
608606
sniffOnStart: false
609607
})
@@ -624,15 +622,10 @@ test('Should not retry if the body is a stream', t => {
624622

625623
var count = 0
626624
function handler (req, res) {
627-
res.setHeader('Content-Type', 'application/json;utf=8')
628-
if (count > 0) {
629-
res.end(JSON.stringify({ hello: 'world' }))
630-
} else {
631-
setTimeout(() => {
632-
res.end(JSON.stringify({ hello: 'world' }))
633-
}, 1000)
634-
}
635625
count++
626+
res.setHeader('Content-Type', 'application/json;utf=8')
627+
res.statusCode = 504
628+
res.end(JSON.stringify({ error: true }))
636629
}
637630

638631
buildServer(handler, ({ port }, server) => {
@@ -653,7 +646,6 @@ test('Should not retry if the body is a stream', t => {
653646
connectionPool: pool,
654647
serializer: new Serializer(),
655648
maxRetries: 1,
656-
requestTimeout: 10,
657649
sniffInterval: false,
658650
sniffOnStart: false
659651
})
@@ -663,7 +655,7 @@ test('Should not retry if the body is a stream', t => {
663655
path: '/hello',
664656
body: intoStream(JSON.stringify({ hello: 'world' }))
665657
}, (err, { body }) => {
666-
t.ok(err instanceof TimeoutError)
658+
t.ok(err instanceof ResponseError)
667659
t.strictEqual(count, 1)
668660
server.stop()
669661
})
@@ -679,9 +671,8 @@ test('Custom retry mechanism', t => {
679671
if (count > 0) {
680672
res.end(JSON.stringify({ hello: 'world' }))
681673
} else {
682-
setTimeout(() => {
683-
res.end(JSON.stringify({ hello: 'world' }))
684-
}, 1000)
674+
res.statusCode = 504
675+
res.end(JSON.stringify({ error: true }))
685676
}
686677
count++
687678
}
@@ -704,7 +695,6 @@ test('Custom retry mechanism', t => {
704695
connectionPool: pool,
705696
serializer: new Serializer(),
706697
maxRetries: 0,
707-
requestTimeout: 250,
708698
sniffInterval: false,
709699
sniffOnStart: false
710700
})

test/utils/buildCluster.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,17 @@ function buildCluster (options, callback) {
5656

5757
function shutdown () {
5858
debug(`Shutting down cluster '${clusterId}'`)
59-
Object.keys(nodes).forEach(kill)
59+
for (const id in nodes) {
60+
kill(id)
61+
}
6062
}
6163

62-
function kill (id) {
64+
function kill (id, callback) {
6365
debug(`Shutting down cluster node '${id}' (cluster id: '${clusterId}')`)
64-
nodes[id].server.stop()
66+
const node = nodes[id]
6567
delete nodes[id]
6668
delete sniffResult.nodes[id]
69+
node.server.stop(callback)
6770
}
6871

6972
function spawn (id, callback) {

0 commit comments

Comments
 (0)