-
Notifications
You must be signed in to change notification settings - Fork 437
/
connection-retry-test.js
105 lines (80 loc) · 2.6 KB
/
connection-retry-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
const Connection = require('../../src/tedious').Connection;
const fs = require('fs');
const Sinon = require('sinon');
const TransientErrorLookup = require('../../src/transient-error-lookup').TransientErrorLookup;
const getConfig = function() {
const config = JSON.parse(fs.readFileSync(process.env.HOME + '/.tedious/test-connection.json', 'utf8')).config;
config.password = 'InvalidPassword';
config.options.maxRetriesOnTransientErrors = 5;
config.options.connectionRetryInterval = 25;
return config;
};
exports['connection retry tests'] = {
setUp: function(done) {
this.invalidLoginError = 18456;
this.sinon = Sinon.sandbox.create();
done();
},
tearDown: function(done) {
this.sinon.restore();
done();
},
'retry specified number of times on transient errors': function(test) {
const config = getConfig();
test.expect(config.options.maxRetriesOnTransientErrors + 1);
this.sinon.stub(TransientErrorLookup.prototype, 'isTransientError', (error) => {
return error === this.invalidLoginError;
});
const connection = new Connection(config);
connection.on('retry', () => {
test.ok(true);
});
connection.on('connect', (err) => {
test.ok(err);
});
connection.on('end', (info) => {
test.done();
});
},
'no retries on non-transient errors': function(test) {
const config = getConfig();
test.expect(1);
this.sinon.stub(TransientErrorLookup.prototype, 'isTransientError', (error) => {
return error !== this.invalidLoginError;
});
const connection = new Connection(config);
connection.on('retry', () => {
test.ok(false);
});
connection.on('connect', (err) => {
test.ok(err);
});
connection.on('end', (info) => {
test.done();
});
},
'no retries if connection timeout fires': function(test) {
const config = getConfig();
config.options.connectTimeout = config.options.connectionRetryInterval / 2;
const clock = this.sinon.useFakeTimers();
test.expect(1);
this.sinon.stub(TransientErrorLookup.prototype, 'isTransientError', (error) => {
return error === this.invalidLoginError;
});
const connection = new Connection(config);
connection.on('retry', () => {
test.ok(false);
});
connection.on('errorMessage', () => {
// Forward clock past connectTimeout which is less than retry interval.
clock.tick(config.options.connectTimeout + 1);
});
connection.on('connect', (err) => {
test.ok(err);
});
connection.on('end', (info) => {
clock.restore();
test.done();
});
},
};