soajs.controller 4.3.0 → 4.3.2
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/.github/workflows/ci.yml +7 -2
- package/.github/workflows/release.yml +11 -4
- package/mw/gotoService/redirectToService.js +124 -2
- package/mw/lastSeen/index.js +133 -42
- package/package.json +1 -1
- package/test/unit/mw/gotoService/monitorBuffer.js +306 -0
- package/test/unit/mw/lastSeen/index.js +946 -2
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @license
|
|
5
|
+
* Copyright SOAJS All Rights Reserved.
|
|
6
|
+
*
|
|
7
|
+
* Use of this source code is governed by an Apache license that can be
|
|
8
|
+
* found in the LICENSE file at the root of this repository
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const assert = require('assert');
|
|
12
|
+
|
|
13
|
+
describe("Unit test for: mw - gotoService monitor buffer", function () {
|
|
14
|
+
|
|
15
|
+
// We need to test the buffer module functions directly
|
|
16
|
+
// Since they are module-level in redirectToService.js, we'll test the logic patterns
|
|
17
|
+
|
|
18
|
+
describe("Buffer configuration parsing", function () {
|
|
19
|
+
|
|
20
|
+
it("should recognize buffer config with time only", function (done) {
|
|
21
|
+
let monitor = {
|
|
22
|
+
"buffer": {
|
|
23
|
+
"time": 5000
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
let hasBuffer = monitor && monitor.buffer && (monitor.buffer.time || monitor.buffer.limit);
|
|
27
|
+
assert.strictEqual(hasBuffer, true);
|
|
28
|
+
done();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("should recognize buffer config with limit only", function (done) {
|
|
32
|
+
let monitor = {
|
|
33
|
+
"buffer": {
|
|
34
|
+
"limit": 100
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
let hasBuffer = monitor && monitor.buffer && (monitor.buffer.time || monitor.buffer.limit);
|
|
38
|
+
assert.strictEqual(hasBuffer, true);
|
|
39
|
+
done();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("should recognize buffer config with both time and limit", function (done) {
|
|
43
|
+
let monitor = {
|
|
44
|
+
"buffer": {
|
|
45
|
+
"time": 5000,
|
|
46
|
+
"limit": 100
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
let hasBuffer = monitor && monitor.buffer && (monitor.buffer.time || monitor.buffer.limit);
|
|
50
|
+
assert.strictEqual(hasBuffer, true);
|
|
51
|
+
done();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("should not recognize buffer config when buffer is not set", function (done) {
|
|
55
|
+
let monitor = {
|
|
56
|
+
"req_info": true
|
|
57
|
+
};
|
|
58
|
+
let hasBuffer = monitor && monitor.buffer && (monitor.buffer.time || monitor.buffer.limit);
|
|
59
|
+
assert.strictEqual(!!hasBuffer, false);
|
|
60
|
+
done();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("should not recognize buffer config when buffer is empty", function (done) {
|
|
64
|
+
let monitor = {
|
|
65
|
+
"buffer": {}
|
|
66
|
+
};
|
|
67
|
+
let hasBuffer = monitor && monitor.buffer && (monitor.buffer.time || monitor.buffer.limit);
|
|
68
|
+
assert.strictEqual(!!hasBuffer, false);
|
|
69
|
+
done();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("should not recognize buffer when monitor is null", function (done) {
|
|
73
|
+
let monitor = null;
|
|
74
|
+
let hasBuffer = monitor && monitor.buffer && (monitor.buffer.time || monitor.buffer.limit);
|
|
75
|
+
assert.strictEqual(!!hasBuffer, false);
|
|
76
|
+
done();
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe("Buffer limit threshold logic", function () {
|
|
81
|
+
|
|
82
|
+
it("should trigger flush when buffer reaches limit", function (done) {
|
|
83
|
+
let monitorBuffer = [];
|
|
84
|
+
let bufferLimit = 3;
|
|
85
|
+
|
|
86
|
+
// Simulate adding items
|
|
87
|
+
for (let i = 0; i < 3; i++) {
|
|
88
|
+
monitorBuffer.push({ name: "test", api: "/test" + i });
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
let shouldFlush = bufferLimit && monitorBuffer.length >= bufferLimit;
|
|
92
|
+
assert.strictEqual(shouldFlush, true);
|
|
93
|
+
done();
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("should not trigger flush when buffer is below limit", function (done) {
|
|
97
|
+
let monitorBuffer = [];
|
|
98
|
+
let bufferLimit = 10;
|
|
99
|
+
|
|
100
|
+
// Simulate adding items
|
|
101
|
+
for (let i = 0; i < 5; i++) {
|
|
102
|
+
monitorBuffer.push({ name: "test", api: "/test" + i });
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let shouldFlush = bufferLimit && monitorBuffer.length >= bufferLimit;
|
|
106
|
+
assert.strictEqual(shouldFlush, false);
|
|
107
|
+
done();
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("should not trigger flush when no limit is set", function (done) {
|
|
111
|
+
let monitorBuffer = [];
|
|
112
|
+
let bufferLimit = null;
|
|
113
|
+
|
|
114
|
+
// Simulate adding items
|
|
115
|
+
for (let i = 0; i < 100; i++) {
|
|
116
|
+
monitorBuffer.push({ name: "test", api: "/test" + i });
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
let shouldFlush = bufferLimit && monitorBuffer.length >= bufferLimit;
|
|
120
|
+
assert.strictEqual(shouldFlush, false);
|
|
121
|
+
done();
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
describe("Buffer time threshold logic", function () {
|
|
126
|
+
|
|
127
|
+
it("should start timer when time threshold is set and no timer exists", function (done) {
|
|
128
|
+
let monitorFlushTimer = null;
|
|
129
|
+
let bufferTime = 5000;
|
|
130
|
+
|
|
131
|
+
let shouldStartTimer = bufferTime && !monitorFlushTimer;
|
|
132
|
+
assert.strictEqual(shouldStartTimer, true);
|
|
133
|
+
done();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("should not start timer when timer already exists", function (done) {
|
|
137
|
+
let monitorFlushTimer = setTimeout(() => {}, 5000);
|
|
138
|
+
let bufferTime = 5000;
|
|
139
|
+
|
|
140
|
+
let shouldStartTimer = bufferTime && !monitorFlushTimer;
|
|
141
|
+
assert.strictEqual(shouldStartTimer, false);
|
|
142
|
+
|
|
143
|
+
clearTimeout(monitorFlushTimer);
|
|
144
|
+
done();
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("should not start timer when no time threshold is set", function (done) {
|
|
148
|
+
let monitorFlushTimer = null;
|
|
149
|
+
let bufferTime = null;
|
|
150
|
+
|
|
151
|
+
let shouldStartTimer = bufferTime && !monitorFlushTimer;
|
|
152
|
+
assert.strictEqual(!!shouldStartTimer, false);
|
|
153
|
+
done();
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
describe("Buffer flush conditions", function () {
|
|
158
|
+
|
|
159
|
+
it("should not flush when buffer is empty", function (done) {
|
|
160
|
+
let monitorBuffer = [];
|
|
161
|
+
let monitorFlushInProgress = false;
|
|
162
|
+
|
|
163
|
+
let shouldFlush = !(monitorFlushInProgress || monitorBuffer.length === 0);
|
|
164
|
+
assert.strictEqual(shouldFlush, false);
|
|
165
|
+
done();
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("should not flush when flush is already in progress", function (done) {
|
|
169
|
+
let monitorBuffer = [{ name: "test" }];
|
|
170
|
+
let monitorFlushInProgress = true;
|
|
171
|
+
|
|
172
|
+
let shouldFlush = !(monitorFlushInProgress || monitorBuffer.length === 0);
|
|
173
|
+
assert.strictEqual(shouldFlush, false);
|
|
174
|
+
done();
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("should flush when buffer has items and no flush in progress", function (done) {
|
|
178
|
+
let monitorBuffer = [{ name: "test" }];
|
|
179
|
+
let monitorFlushInProgress = false;
|
|
180
|
+
|
|
181
|
+
let shouldFlush = !(monitorFlushInProgress || monitorBuffer.length === 0);
|
|
182
|
+
assert.strictEqual(shouldFlush, true);
|
|
183
|
+
done();
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
describe("Monitor document transformation", function () {
|
|
188
|
+
|
|
189
|
+
it("should convert body to string before sending", function (done) {
|
|
190
|
+
let doc = {
|
|
191
|
+
name: "test",
|
|
192
|
+
body: Buffer.from("test body")
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
if (doc.body) {
|
|
196
|
+
doc.body = doc.body.toString();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
assert.strictEqual(typeof doc.body, "string");
|
|
200
|
+
assert.strictEqual(doc.body, "test body");
|
|
201
|
+
done();
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("should convert response to string before sending", function (done) {
|
|
205
|
+
let doc = {
|
|
206
|
+
name: "test",
|
|
207
|
+
response: Buffer.from('{"result": true}')
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
if (doc.response) {
|
|
211
|
+
doc.response = doc.response.toString();
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
assert.strictEqual(typeof doc.response, "string");
|
|
215
|
+
assert.strictEqual(doc.response, '{"result": true}');
|
|
216
|
+
done();
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("should handle doc without body or response", function (done) {
|
|
220
|
+
let doc = {
|
|
221
|
+
name: "test",
|
|
222
|
+
api: "/test"
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
if (doc.body) {
|
|
226
|
+
doc.body = doc.body.toString();
|
|
227
|
+
}
|
|
228
|
+
if (doc.response) {
|
|
229
|
+
doc.response = doc.response.toString();
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
assert.strictEqual(doc.body, undefined);
|
|
233
|
+
assert.strictEqual(doc.response, undefined);
|
|
234
|
+
done();
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
describe("Backward compatibility", function () {
|
|
239
|
+
|
|
240
|
+
it("should send immediately when no buffer config (backward compatible)", function (done) {
|
|
241
|
+
let monitor = {
|
|
242
|
+
"req_info": true,
|
|
243
|
+
"req_response": true
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
let hasBuffer = monitor && monitor.buffer && (monitor.buffer.time || monitor.buffer.limit);
|
|
247
|
+
let shouldSendImmediately = !hasBuffer;
|
|
248
|
+
|
|
249
|
+
assert.strictEqual(shouldSendImmediately, true);
|
|
250
|
+
done();
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it("should use buffer when buffer config is present", function (done) {
|
|
254
|
+
let monitor = {
|
|
255
|
+
"req_info": true,
|
|
256
|
+
"req_response": true,
|
|
257
|
+
"buffer": {
|
|
258
|
+
"time": 5000,
|
|
259
|
+
"limit": 100
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
let hasBuffer = monitor && monitor.buffer && (monitor.buffer.time || monitor.buffer.limit);
|
|
264
|
+
let shouldSendImmediately = !hasBuffer;
|
|
265
|
+
|
|
266
|
+
assert.strictEqual(shouldSendImmediately, false);
|
|
267
|
+
done();
|
|
268
|
+
});
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
describe("Buffer retry logic", function () {
|
|
272
|
+
|
|
273
|
+
it("should put items back in buffer when host not found", function (done) {
|
|
274
|
+
let monitorBuffer = [];
|
|
275
|
+
let itemsToSend = [{ name: "test1" }, { name: "test2" }];
|
|
276
|
+
let host = null;
|
|
277
|
+
|
|
278
|
+
// Simulate host not found - items should go back to buffer
|
|
279
|
+
if (!host) {
|
|
280
|
+
monitorBuffer = itemsToSend.concat(monitorBuffer);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
assert.strictEqual(monitorBuffer.length, 2);
|
|
284
|
+
assert.strictEqual(monitorBuffer[0].name, "test1");
|
|
285
|
+
done();
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it("should preserve order when putting items back", function (done) {
|
|
289
|
+
let monitorBuffer = [{ name: "new1" }];
|
|
290
|
+
let itemsToSend = [{ name: "old1" }, { name: "old2" }];
|
|
291
|
+
let host = null;
|
|
292
|
+
|
|
293
|
+
// Simulate host not found - items should go back to buffer
|
|
294
|
+
if (!host) {
|
|
295
|
+
monitorBuffer = itemsToSend.concat(monitorBuffer);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
assert.strictEqual(monitorBuffer.length, 3);
|
|
299
|
+
assert.strictEqual(monitorBuffer[0].name, "old1");
|
|
300
|
+
assert.strictEqual(monitorBuffer[1].name, "old2");
|
|
301
|
+
assert.strictEqual(monitorBuffer[2].name, "new1");
|
|
302
|
+
done();
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
});
|