connect-ready 1.0.13 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test/index.js DELETED
@@ -1,225 +0,0 @@
1
- 'use strict';
2
- var ready = require('../index.js');
3
- var assert = require('assert');
4
- var events = require('events');
5
-
6
- function tightWork(duration) {
7
- var start = Date.now();
8
- while ((Date.now() - start) < duration) {
9
- for (var i = 0; i < 1e5;) i++;
10
- }
11
- }
12
-
13
- describe("ready", function() {
14
-
15
- describe('setStatus', function() {
16
- it('throws if no params', function() {
17
- assert.throws(
18
- ready.setStatus,
19
- /status should be an integer between 1 and 999/
20
- );
21
- });
22
- it('throws if non number', function() {
23
- assert.throws(
24
- function(){ ready.setStatus({consecutiveFailures:101}); },
25
- /status should be an integer between 1 and 999/
26
- );
27
- assert.throws(
28
- function(){ ready.setStatus('hello'); },
29
- /status should be an integer between 1 and 999/
30
- );
31
- assert.throws(
32
- function(){ ready.setStatus([1,2]); },
33
- /status should be an integer between 1 and 999/
34
- );
35
- assert.throws(
36
- function(){ ready.setStatus(0); },
37
- /status should be an integer between 1 and 999/
38
- );
39
- });
40
-
41
- it('accepts valid params', function() {
42
- assert.equal(ready.getStatus(), 500);
43
- assert.doesNotThrow(function() {
44
- ready.setStatus(500);
45
- });
46
- assert.equal(ready.getStatus(), 500);
47
- assert.doesNotThrow(function() {
48
- ready.setStatus(1);
49
- });
50
- assert.equal(ready.getStatus(), 1);
51
- assert.doesNotThrow(function() {
52
- ready.setStatus(200);
53
- });
54
- assert.equal(ready.getStatus(), 200);
55
- });
56
- });
57
-
58
- describe('route', function() {
59
- it('returns the status', function(done) {
60
- ready.setStatus(200);
61
- ready.route({}, {sendStatus:function(code) {
62
- assert.equal(code, 200);
63
- return this;
64
- }, end: function() { done(); }});
65
- });
66
-
67
- it('returns the status', function(done) {
68
- ready.setStatus(500);
69
- ready.route({}, {sendStatus:function(code) {
70
- assert.equal(code, 500);
71
- return this;
72
- }, end: function() { done(); }});
73
- });
74
- });
75
-
76
- describe('toobusy', function() {
77
- it('rejects invalid lag', function() {
78
- assert.throws(
79
- function(){ ready.enableTooBusy({consecutiveFailures:101}); },
80
- /lag should be an integer greater than 10/
81
- );
82
- assert.throws(
83
- function(){ ready.enableTooBusy('hello'); },
84
- /lag should be an integer greater than 10/
85
- );
86
- assert.throws(
87
- function(){ ready.enableTooBusy(-20); },
88
- /lag should be an integer greater than 10/
89
- );
90
- assert.throws(
91
- function(){ ready.enableTooBusy(0); },
92
- /lag should be an integer greater than 10/
93
- );
94
- });
95
- it('accepts a valid lag', function() {
96
- assert.doesNotThrow(function() {
97
- ready.enableTooBusy(100);
98
- });
99
- assert.doesNotThrow(function() {
100
- ready.enableTooBusy();
101
- });
102
- });
103
- it('returns the status', function(done) {
104
- ready.enableTooBusy(10);
105
- ready.setStatus(200);
106
- tightWork(600);
107
- setTimeout(function() {
108
- ready.route({}, {sendStatus:function(code) {
109
- assert.equal(code, 503);
110
- return this;
111
- }, end: function() { done(); }});
112
- }, 0);
113
- });
114
- });
115
-
116
- describe('shutdown', function() {
117
- it('no signal', function(done) {
118
- ready.shutdown(undefined, undefined, done, '/tmp/termination-log', undefined);
119
- });
120
- it('no signal with logger', function(done) {
121
- ready.shutdown(undefined, undefined, function() {}, '/tmp/termination-log', {info:function(gelf, msg) {
122
- assert.equal(msg, 'shutdown');
123
- done();
124
- }});
125
- });
126
- it('while stoping already', function(done) {
127
- ready.shutdown(undefined,
128
- undefined,
129
- function() {
130
- done(new Error('should not be called'));
131
- clearTimeout(timer);
132
- },
133
- '/tmp/termination-log',
134
- undefined
135
- );
136
- var timer = setTimeout(function() {
137
- done();
138
- }, 200);
139
- });
140
-
141
- it('signal', function(done) {
142
- //reload the module to ensure stopping is reset to false
143
- delete require.cache[require.resolve('../index.js')];
144
- ready = require('../index.js');
145
- ready.shutdown(
146
- 'SIGTERM',
147
- new Error('test'),
148
- function(status) {
149
- assert.equal(status, 1);
150
- done();
151
- },
152
- '/tmp/termination-log',
153
- undefined
154
- );
155
- });
156
-
157
- it('signal no error', function(done) {
158
- //reload the module to ensure stopping is reset to false
159
- delete require.cache[require.resolve('../index.js')];
160
- ready = require('../index.js');
161
- ready.shutdown(
162
- 'SIGTERM',
163
- undefined,
164
- function(status) {
165
- assert.equal(status, 1);
166
- done();
167
- },
168
- '/tmp/termination-log',
169
- undefined
170
- );
171
- });
172
-
173
- it('signal and logger', function(done) {
174
- //reload the module to ensure stopping is reset to false
175
- delete require.cache[require.resolve('../index.js')];
176
- ready = require('../index.js');
177
- ready.shutdown(
178
- 'SIGTERM',
179
- new Error('test'),
180
- function() { },
181
- '/tmp/termination-log',
182
- { fatal:function(gelf, msg) {
183
- assert.equal(msg, 'test');
184
- done();
185
- }
186
- }
187
- );
188
- });
189
-
190
- it('no signal', function(done) {
191
- delete require.cache[require.resolve('../index.js')];
192
- ready = require('../index.js');
193
- ready.shutdown(undefined, undefined, done, '/not a valid path/termination-log', undefined);
194
- });
195
- });
196
-
197
- describe('gracefulShutdownKeepaliveConnections', function() {
198
-
199
- it('sets "connection: close" header if stopping', function(done) {
200
- delete require.cache[require.resolve('../index.js')];
201
- ready = require('../index.js');
202
- ready.shutdown('SIGTERM');
203
-
204
- var res = {
205
- set: function(name, value) {
206
- assert.equal(name.toLowerCase(), 'connection');
207
- assert.equal(value.toLowerCase(), 'close');
208
- }
209
- }
210
- ready.gracefulShutdownKeepaliveConnections({}, res, done);
211
- });
212
-
213
- it('calls next if not stopping', function(done) {
214
- delete require.cache[require.resolve('../index.js')];
215
- ready = require('../index.js');
216
-
217
- var res = {
218
- set: function(header) {
219
- throw new Error('should not call res.set');
220
- }
221
- }
222
- ready.gracefulShutdownKeepaliveConnections({}, res, done);
223
- });
224
- });
225
- });