summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/retry_network_error_test.js
blob: e8fe4a78047f2706932ba164214e80ab9b08ef09 (plain)
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
/**
 * Unit test to verify that 'retryOnNetworkError' works correctly for common network connection
 * issues.
 */

(function() {
"use strict";
let node = MongoRunner.runMongod();
let hostname = node.host;

jsTestLog("Test connecting to a healthy node.");
let numRetries = 5;
let sleepMs = 50;
let attempts = 0;
retryOnNetworkError(function() {
    attempts++;
    new Mongo(hostname);
}, numRetries, sleepMs);
assert.eq(attempts, 1);

jsTestLog("Test connecting to a node that is down.");
MongoRunner.stopMongod(node);
attempts = 0;
try {
    retryOnNetworkError(function() {
        attempts++;
        new Mongo(hostname);
    }, numRetries, sleepMs);
} catch (e) {
    jsTestLog("Caught exception after exhausting retries: " + e);
}
assert.eq(attempts, numRetries + 1);

jsTestLog("Test connecting to a node with an invalid hostname.");
let invalidHostname = "very-invalid-host-name";
attempts = 0;
try {
    retryOnNetworkError(function() {
        attempts++;
        new Mongo(invalidHostname);
    }, numRetries, sleepMs);
} catch (e) {
    jsTestLog("Caught exception after exhausting retries: " + e);
}
assert.eq(attempts, numRetries + 1);
}());