Debian Codegraph @

pkg/base1/test-series.js

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 import cockpit from "cockpit";
3 import QUnit from "qunit-tests";
4
5 QUnit.test("public api", function (assert) {
6 assert.equal(typeof cockpit.grid, "function", "cockpit.grid is a function");
7 assert.equal(typeof cockpit.series, "function", "cockpit.series is a function");
8
9 const grid1 = cockpit.grid(555, 3, 8);
10 assert.strictEqual(grid1.interval, 555, "grid.interval");
11 assert.strictEqual(grid1.beg, 3, "grid.beg");
12 assert.strictEqual(grid1.end, 8, "grid.end");
13 assert.equal(typeof grid1.add, "function", "grid.add()");
14 assert.equal(typeof grid1.remove, "function", "grid.remove()");
15 assert.equal(typeof grid1.close, "function", "grid.close()");
16 assert.equal(typeof grid1.notify, "function", "grid.notify()");
17 assert.equal(typeof grid1.move, "function", "grid.move()");
18 assert.equal(typeof grid1.sync, "function", "grid.sync()");
19
20 const grid2 = cockpit.grid(555, 3);
21 assert.strictEqual(grid2.beg, 3, "not-null grid.beg");
22 assert.strictEqual(grid2.end, 3, "same grid.end");
23
24 const grid3 = cockpit.grid(555, 0);
25 assert.strictEqual(grid3.end, 0, "zero grid.end");
26
27 const sink = cockpit.series(888);
28 assert.strictEqual(sink.interval, 888, "series.interval");
29 assert.equal(typeof sink.input, "function", "series.input()");
30 assert.equal(typeof sink.load, "function", "series.load()");
31 });
32
33 QUnit.test("calculated row", function (assert) {
34 const grid = cockpit.grid(1000, 3, 8);
35 const calculated = grid.add(function(row, x, n) {
36 for (let i = 0; i < n; i++)
37 row[i + x] = i;
38 });
39
40 grid.notify(1, 4);
41 assert.deepEqual(calculated, [undefined, 0, 1, 2, 3], "array contents");
42 });
43
44 QUnit.test("calculated order", function (assert) {
45 const grid = cockpit.grid(1000, 3, 8);
46
47 const calculated = grid.add(function(row, x, n) {
48 for (let i = 0; i < n; i++)
49 row[i + x] = i;
50 });
51
52 /* Callbacks must be called in the right order for this to work */
53 const dependent = grid.add(function(row, x, n) {
54 for (let i = 0; i < n; i++)
55 row[i + x] = calculated[i + x] + 10;
56 });
57
58 grid.notify(1, 4);
59 assert.deepEqual(dependent, [undefined, 10, 11, 12, 13], "dependent array contents");
60 });
61
62 QUnit.test("calculated early", function (assert) {
63 const grid = cockpit.grid(1000, 3, 8);
64
65 /* Callbacks must be called in the right order for this to work */
66 const dependent = grid.add(function(row, x, n) {
67 for (let i = 0; i < n; i++)
68 row[i + x] = calculated[i + x] + 10;
69 });
70
71 /* Even though this one is added after, run first, due to early flag */
72 const calculated = grid.add(function(row, x, n) {
73 for (let i = 0; i < n; i++)
74 row[i + x] = i;
75 }, true);
76
77 grid.notify(1, 4);
78 assert.deepEqual(dependent, [undefined, 10, 11, 12, 13], "dependent array contents");
79 });
80
81 QUnit.test("notify limit", function (assert) {
82 const grid = cockpit.grid(1000, 5, 15);
83
84 let called = -1;
85 grid.add(function(row, x, n) {
86 called = n;
87 });
88
89 grid.notify(10, 8);
90 assert.strictEqual(called, -1, "not called out of bounds");
91
92 grid.notify(1, 0);
93 assert.strictEqual(called, -1, "not called zero length");
94
95 grid.notify(1, 20);
96 assert.strictEqual(called, 9, "truncated to right limit");
97 });
98
99 QUnit.test("sink row", function (assert) {
100 const grid = cockpit.grid(1000, 5, 15);
101 const sink = cockpit.series(1000);
102
103 const row1 = grid.add(sink, "one.sub.2");
104 const row2 = grid.add(sink, ["one", "sub", 2]);
105 const calc = grid.add(function(row, x, n) {
106 for (let i = 0; i < n; i++)
107 row[x + i] = row1[x + i] + row2[x + i];
108 });
109
110 let notified = null;
111 grid.addEventListener("notify", (ev, n, x) => {
112 notified = [n, x];
113 });
114
115 const items = [
116 {
117 one: { sub: [200, 201, 202], another: [20, 21, 22] },
118 two: { sub: [2000, 2001, 2002], marmalade: [0, 1, 2] }
119 },
120 {
121 one: { sub: [300, 301, 302], another: [30, 31, 32] },
122 two: { sub: [3000, 3001, 3002], marmalade: [0, 1, 2] }
123 },
124 {
125 one: { sub: [400, 401, 402], another: [40, 41, 42] },
126 two: { sub: [4000, 4001, 4002], marmalade: [0, 1, 2] }
127 }
128 ];
129
130 sink.input(7, items);
131
132 assert.deepEqual(notified, [2, 3]);
133
134 assert.deepEqual(row1, [undefined, undefined, 202, 302, 402], "row with string path");
135 assert.deepEqual(row2, [undefined, undefined, 202, 302, 402], "row with array path");
136 assert.deepEqual(calc, [undefined, undefined, 404, 604, 804], "row with calculated");
137
138 grid.close();
139 });
140
141 QUnit.test("sink no path", function (assert) {
142 const grid = cockpit.grid(1000, 5, 15);
143 const sink = cockpit.series(1000);
144
145 const row = grid.add(sink);
146
147 const items = [567, 768, { hello: "scruffy" }];
148
149 sink.input(8, items);
150
151 assert.deepEqual(row, [undefined, undefined, undefined, 567, 768, { hello: "scruffy" }], "row without a path");
152 });
153
154 QUnit.test("sink after close", function (assert) {
155 const grid = cockpit.grid(1000, 5, 15);
156 const sink = cockpit.series(1000);
157
158 const row = grid.add(sink);
159
160 const items = [1, 2, 3];
161
162 sink.input(5, items);
163 assert.deepEqual(row, [1, 2, 3], "row got values");
164
165 sink.input(8, items);
166 assert.deepEqual(row, [1, 2, 3, 1, 2, 3], "row got more values");
167
168 grid.close();
169
170 sink.input(11, items);
171 assert.deepEqual(row, [1, 2, 3, 1, 2, 3], "row got no more values");
172 });
173
174 QUnit.test("sink mapping", function (assert) {
175 const grid = cockpit.grid(1000, 5, 15);
176 const sink = cockpit.series(1000);
177
178 const row1 = grid.add(sink, "two.sub.1");
179 const row2 = grid.add(sink, "one.sub");
180 const row3 = grid.add(sink, "invalid");
181
182 const mapping = {
183 one: { "": 0, sub: { "": 0 }, another: { "": 1 } },
184 two: { "": 1, sub: { "": 0 }, marmalade: { "": 1 } }
185 };
186
187 const items = [
188 [
189 [[200, 201, 202], [20, 21, 22]],
190 [[2000, 2001, 2002], [0, 1, 2]]
191 ],
192 [
193 [[300, 301, 302], [30, 31, 32]],
194 [[3000, 3001, 3002], [0, 1, 2]]
195 ],
196 [
197 [[400, 401, 402], [40, 41, 42]],
198 [[4000, 4001, 4002], [0, 1, 2]]
199 ]
200 ];
201
202 sink.input(5, items, mapping);
203
204 assert.deepEqual(row1, [2001, 3001, 4001], "mapped with trailing");
205 assert.deepEqual(row2, [[200, 201, 202], [300, 301, 302], [400, 401, 402]], "mapped simply");
206 assert.deepEqual(row3, [undefined, undefined, undefined], "mapped undefined");
207
208 grid.close();
209 });
210
211 QUnit.test("cache simple", function (assert) {
212 const fetched = [];
213 function fetch(beg, end) {
214 fetched.push([beg, end]);
215 }
216
217 const sink = cockpit.series(1000, null, fetch);
218
219 sink.input(7, [
220 {
221 one: { sub: [200, 201, 202], another: [20, 21, 22] },
222 two: { sub: [2000, 2001, 2002], marmalade: [0, 1, 2] }
223 },
224 {
225 one: { sub: [300, 301, 302], another: [30, 31, 32] },
226 two: { sub: [3000, 3001, 3002], marmalade: [0, 1, 2] }
227 },
228 {
229 one: { sub: [400, 401, 402], another: [40, 41, 42] },
230 two: { sub: [4000, 4001, 4002], marmalade: [0, 1, 2] }
231 }
232 ]);
233
234 const grid = cockpit.grid(1000, 5, 15);
235
236 let notified = null;
237 grid.addEventListener("notify", (ev, n, x) => {
238 notified = [n, x];
239 });
240
241 const row1 = grid.add(sink, "one.sub.2");
242 const row2 = grid.add(sink, ["one", "sub", 2]);
243 const calc = grid.add(function(row, x, n) {
244 for (let i = 0; i < n; i++)
245 row[x + i] = (row1[x + i] + row2[x + i]) || undefined;
246 });
247
248 grid.sync();
249
250 assert.deepEqual(fetched, [[5, 7], [10, 15]], "fetched right data");
251 assert.deepEqual(notified, [0, 10], "notified about right indexes");
252
253 assert.deepEqual(row1, [undefined, undefined, 202, 302, 402], "row with string path");
254 assert.deepEqual(row2, [undefined, undefined, 202, 302, 402], "row with array path");
255 assert.deepEqual(calc, [undefined, undefined, 404, 604, 804, undefined,
256 undefined, undefined, undefined, undefined], "row with calculated");
257
258 grid.close();
259 });
260
261 QUnit.test("cache multiple", function (assert) {
262 const fetched = [];
263 function fetch(beg, end) {
264 fetched.push([beg, end]);
265 }
266
267 const sink = cockpit.series(1000, null, fetch);
268
269 sink.input(7, [{
270 one: { sub: [200, 201, 202], another: [20, 21, 22] },
271 two: { sub: [2000, 2001, 2002], marmalade: [0, 1, 2] }
272 }]);
273
274 sink.input(8, [{
275 one: { sub: [300, 301, 302], another: [30, 31, 32] },
276 two: { sub: [3000, 3001, 3002], marmalade: [0, 1, 2] }
277 }]);
278
279 sink.input(9, [{
280 one: { sub: [400, 401, 402], another: [40, 41, 42] },
281 two: { sub: [4000, 4001, 4002], marmalade: [0, 1, 2] }
282 }]);
283
284 const grid = cockpit.grid(1000, 5, 15);
285
286 let notified = null;
287 grid.addEventListener("notify", (ev, n, x) => {
288 notified = [n, x];
289 });
290
291 const row1 = grid.add(sink, "one.sub.2");
292 const row2 = grid.add(sink, ["one", "sub", 2]);
293 const calc = grid.add(function(row, x, n) {
294 for (let i = 0; i < n; i++)
295 row[x + i] = (row1[x + i] + row2[x + i]) || undefined;
296 });
297
298 grid.sync();
299
300 assert.deepEqual(fetched, [[5, 7], [10, 15]], "fetched right data");
301 assert.deepEqual(notified, [0, 10], "notified about right indexes");
302
303 assert.deepEqual(row1, [undefined, undefined, 202, 302, 402], "row with string path");
304 assert.deepEqual(row2, [undefined, undefined, 202, 302, 402], "row with array path");
305 assert.deepEqual(calc, [undefined, undefined, 404, 604, 804, undefined,
306 undefined, undefined, undefined, undefined], "row with calculated");
307
308 grid.close();
309 });
310
311 QUnit.test("cache overlap", function (assert) {
312 const fetched = [];
313 function fetch(beg, end) {
314 fetched.push([beg, end]);
315 }
316
317 const sink = cockpit.series(1000, null, fetch);
318 const grid = cockpit.grid(1000, 5, 15);
319 const row1 = grid.add(sink, "one.sub.2");
320
321 /* Initial state of the cache */
322 sink.input(6, [{
323 one: { sub: [200, 201, 202], another: [20, 21, 22] },
324 two: { sub: [2000, 2001, 2002], marmalade: [0, 1, 2] }
325 }, {
326 one: { sub: [200, 201, 202], another: [20, 21, 22] },
327 two: { sub: [2000, 2001, 2002], marmalade: [0, 1, 2] }
328 }]);
329
330 sink.input(8, [{
331 one: { sub: [300, 301, 302], another: [30, 31, 32] },
332 two: { sub: [3000, 3001, 3002], marmalade: [0, 1, 2] }
333 }, {
334 one: { sub: [300, 301, 302], another: [30, 31, 32] },
335 two: { sub: [3000, 3001, 3002], marmalade: [0, 1, 2] }
336 }]);
337 sink.input(10, [{
338 one: { sub: [900, 901, 902], another: [90, 91, 92] },
339 two: { sub: [9000, 9001, 9002], marmalade: [0, 1, 2] }
340 }]);
341
342 assert.deepEqual(row1, [undefined, 202, 202, 302, 302, 902], "row with initial data");
343
344 /* Overlaying the data currently throws overlapping stuff out of the cache */
345 sink.input(7, [{
346 one: { sub: [400, 401, 402], another: [40, 41, 42] },
347 two: { sub: [4000, 4001, 4002], marmalade: [0, 1, 2] }
348 }, {
349 one: { sub: [400, 401, 402], another: [40, 41, 42] },
350 two: { sub: [4000, 4001, 4002], marmalade: [0, 1, 2] }
351 }]);
352
353 const row2 = grid.add(sink, "one.sub.2");
354 grid.sync();
355
356 assert.deepEqual(row1, [undefined, 202, 402, 402, 302, 902], "row with filled data");
357 assert.deepEqual(row2, [undefined, 202, 402, 402, 302, 902], "row with overlapping data");
358
359 grid.close();
360 });
361
362 QUnit.test("cache limit", function (assert) {
363 const series = cockpit.series(1000, null);
364 series.limit = 5;
365 series.input(8, ["eight"]);
366 series.input(6, ["six", "seven"]);
367 series.input(9, ["nine"]);
368
369 const grid = cockpit.grid(1000, 5, 15);
370 const row = grid.add(series, null);
371 grid.sync();
372
373 assert.deepEqual(row, [undefined, "six", "seven", "eight", "nine"], "initial data correct");
374
375 /* Force an expiry by adding too much data */
376 series.input(10, ["ten", "eleven"]);
377
378 /* Should have removed some data from cache */
379 grid.move(4, 14);
380 assert.deepEqual(row, [undefined, undefined, "six", "seven", undefined, "nine", "ten", "eleven"], "expired");
381
382 /* Force further expiry */
383 series.input(3, ["three", "four", "five"]);
384
385 /* Should have removed move data from cache */
386 grid.move(3, 13);
387 assert.deepEqual(row, ["three", "four", "five", undefined, undefined, undefined,
388 undefined, "ten", "eleven"], "expired more");
389
390 grid.close();
391 });
392
393 QUnit.test("move", function (assert) {
394 const fetched = [];
395 function fetch(beg, end) {
396 fetched.push([beg, end]);
397 }
398
399 const sink = cockpit.series(1000, null, fetch);
400 const grid = cockpit.grid(1000, 20, 25);
401
402 const row1 = grid.add(sink, "one.sub.2");
403 const row2 = grid.add(sink, ["one", "sub", 2]);
404 const calc = grid.add(function(row, x, n) {
405 for (let i = 0; i < n; i++)
406 row[x + i] = (row1[x + i] + row2[x + i]) || undefined;
407 });
408
409 let notified = null;
410 grid.addEventListener("notify", (ev, n, x) => {
411 notified = [n, x];
412 });
413
414 sink.input(7, [{
415 one: { sub: [200, 201, 202], another: [20, 21, 22] },
416 two: { sub: [2000, 2001, 2002], marmalade: [0, 1, 2] }
417 },
418 {
419 one: { sub: [300, 301, 302], another: [30, 31, 32] },
420 two: { sub: [3000, 3001, 3002], marmalade: [0, 1, 2] }
421 },
422 {
423 one: { sub: [400, 401, 402], another: [40, 41, 42] },
424 two: { sub: [4000, 4001, 4002], marmalade: [0, 1, 2] }
425 }]);
426
427 assert.deepEqual(fetched, [], "fetched no data");
428 assert.strictEqual(notified, null, "not notified");
429
430 assert.deepEqual(row1, [], "row1 empty");
431 assert.deepEqual(row2, [], "row2 empty");
432 assert.deepEqual(calc, [], "calc empty");
433
434 grid.move(5, 15);
435
436 assert.deepEqual(fetched, [[5, 7], [10, 15]], "fetched right data");
437 assert.deepEqual(notified, [0, 10], "not notified");
438
439 assert.deepEqual(row1, [undefined, undefined, 202, 302, 402], "row1 with data");
440 assert.deepEqual(row2, [undefined, undefined, 202, 302, 402], "row2 with data");
441 assert.deepEqual(calc, [undefined, undefined, 404, 604, 804, undefined,
442 undefined, undefined, undefined, undefined], "row with calculated");
443
444 grid.close();
445 });
446
447 QUnit.test("move negative", function (assert) {
448 const now = Date.now();
449 const grid = cockpit.grid(1000, -20, -5);
450
451 assert.equal(grid.beg, Math.floor(now / 1000) - 20);
452 assert.equal(grid.end, Math.floor(now / 1000) - 5);
453
454 grid.move(-30, -0);
455
456 assert.equal(grid.beg, Math.floor(now / 1000) - 30);
457 assert.equal(grid.end, Math.floor(now / 1000));
458
459 grid.close();
460 });
461
462 QUnit.test("test", function (assert) {
463 const done = assert.async();
464 assert.expect(5);
465
466 const fetched = [];
467 function fetch(beg, end) {
468 fetched.push([beg, end]);
469 }
470
471 const series = cockpit.series(100, null, fetch);
472 const grid = cockpit.grid(100, 20, 25);
473
474 grid.add(series, []);
475
476 let count = 0;
477 grid.walk();
478
479 grid.addEventListener("notify", () => {
480 count += 1;
481
482 assert.equal(count, fetched.length, "fetched " + count);
483
484 if (count == 5) {
485 grid.close();
486 done();
487 }
488 });
489 });
490
491 /* mock Date.now() function that returns a constant value to avoid races
492 */
493 Date.now = function() {
494 return 0;
495 };
496
497 QUnit.start();