44 lines
1.2 KiB
JavaScript
44 lines
1.2 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('assert');
|
|
const {
|
|
MIN_REFUND_SPACING_SECONDS,
|
|
MAX_REFUND_SPACING_SECONDS,
|
|
randomRefundSpacingSeconds,
|
|
waitForRandomRefundSpacing,
|
|
} = require('../src/services/workspace_refund_spacing');
|
|
|
|
(async () => {
|
|
assert.strictEqual(randomRefundSpacingSeconds(() => 0), MIN_REFUND_SPACING_SECONDS);
|
|
assert.strictEqual(randomRefundSpacingSeconds(() => 0.999999), MAX_REFUND_SPACING_SECONDS);
|
|
|
|
const waits = [];
|
|
let logMessage = '';
|
|
const waited = await waitForRandomRefundSpacing({
|
|
random: () => 0,
|
|
delay: async ms => waits.push(ms),
|
|
onLog: message => { logMessage = message; },
|
|
});
|
|
assert.deepStrictEqual(waited, { completed: true, seconds: 15 });
|
|
assert.deepStrictEqual(waits, [15000]);
|
|
assert.match(logMessage, /随机间隔 15 秒/);
|
|
|
|
let stop = false;
|
|
const stopAwareWaits = [];
|
|
const stopped = await waitForRandomRefundSpacing({
|
|
random: () => 0,
|
|
shouldStop: () => stop,
|
|
delay: async ms => {
|
|
stopAwareWaits.push(ms);
|
|
stop = true;
|
|
},
|
|
});
|
|
assert.strictEqual(stopped.completed, false);
|
|
assert.deepStrictEqual(stopAwareWaits, [1000]);
|
|
|
|
console.log('workspace_refund_spacing.test.js: PASS');
|
|
})().catch(error => {
|
|
console.error(error);
|
|
process.exitCode = 1;
|
|
});
|