@kylewillett/troupe 0.2.0 → 0.3.1
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/CHANGELOG.md +16 -0
- package/README.md +8 -3
- package/dist/cell.d.ts +8 -2
- package/dist/cell.js +71 -10
- package/dist/cluster/sharding.d.ts +7 -0
- package/dist/cluster/sharding.js +14 -6
- package/dist/cluster/timed-store.d.ts +4 -0
- package/dist/cluster/timed-store.js +19 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/dist/internal.d.ts +5 -0
- package/dist/persistence/actor.d.ts +9 -0
- package/dist/persistence/actor.js +20 -9
- package/dist/persistence/errors.d.ts +19 -0
- package/dist/persistence/errors.js +22 -0
- package/dist/persistence/index.d.ts +1 -1
- package/dist/persistence/index.js +1 -1
- package/dist/persistence/projection.d.ts +6 -0
- package/dist/persistence/projection.js +21 -5
- package/dist/rehearsal/clock.d.ts +54 -0
- package/dist/rehearsal/clock.js +203 -0
- package/dist/rehearsal/database.d.ts +116 -0
- package/dist/rehearsal/database.js +349 -0
- package/dist/rehearsal/loop.d.ts +57 -0
- package/dist/rehearsal/loop.js +153 -0
- package/dist/rehearsal/network.d.ts +120 -0
- package/dist/rehearsal/network.js +524 -0
- package/dist/rehearsal/ranges.d.ts +8 -0
- package/dist/rehearsal/ranges.js +27 -0
- package/dist/rehearsal/yield.d.ts +9 -0
- package/dist/rehearsal/yield.js +24 -0
- package/dist/system.d.ts +18 -0
- package/dist/system.js +19 -1
- package/dist/testkit/kit.d.ts +2 -1
- package/dist/testkit/kit.js +6 -2
- package/dist/timeout.d.ts +8 -0
- package/dist/timeout.js +26 -0
- package/dist/types.d.ts +31 -3
- package/package.json +1 -1
|
@@ -1,10 +1,21 @@
|
|
|
1
1
|
import { checkInt, checkKeys, describe } from '../checks.js';
|
|
2
2
|
import { internalsOf, START } from '../internal.js';
|
|
3
3
|
import { isValidName } from '../names.js';
|
|
4
|
-
|
|
4
|
+
import { withTimeout } from '../timeout.js';
|
|
5
|
+
import { PersistenceTimeoutError } from './errors.js';
|
|
6
|
+
const OPTIONS = new Set([
|
|
7
|
+
'name',
|
|
8
|
+
'persistence',
|
|
9
|
+
'tag',
|
|
10
|
+
'handle',
|
|
11
|
+
'pollMs',
|
|
12
|
+
'batchSize',
|
|
13
|
+
'timeoutMs',
|
|
14
|
+
]);
|
|
5
15
|
const MAX_NAME_LENGTH = 255;
|
|
6
16
|
const DEFAULT_POLL_MS = 1_000;
|
|
7
17
|
const DEFAULT_BATCH_SIZE = 100;
|
|
18
|
+
const DEFAULT_TIMEOUT_MS = 10_000;
|
|
8
19
|
const POLL_TIMER = 'poll';
|
|
9
20
|
/** The projection's only message. Private, so nothing outside can tell it anything. */
|
|
10
21
|
const POLL = Object.freeze({ type: 'poll' });
|
|
@@ -33,10 +44,13 @@ export function projection(opts) {
|
|
|
33
44
|
const offsets = opts.persistence.offsets;
|
|
34
45
|
const pollMs = opts.pollMs ?? DEFAULT_POLL_MS;
|
|
35
46
|
const batchSize = opts.batchSize ?? DEFAULT_BATCH_SIZE;
|
|
47
|
+
const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
48
|
+
/** `call`, timed on the projection's scheduler (decision 0036). */
|
|
49
|
+
const timed = (ctx, operation, call) => withTimeout(call, timeoutMs, internalsOf(ctx).setTimer, () => new PersistenceTimeoutError(name, operation, timeoutMs));
|
|
36
50
|
const def = {
|
|
37
51
|
initial: { offset: 0 },
|
|
38
52
|
async receive(current, _poll, ctx) {
|
|
39
|
-
const batch = await journal.readByTag(tag, current.offset, batchSize);
|
|
53
|
+
const batch = await timed(ctx, 'readByTag', journal.readByTag(tag, current.offset, batchSize));
|
|
40
54
|
// After the read's await, so outside the cell's turn: what handle tells names the projection
|
|
41
55
|
// as its sender only through the hook (decision 0020). Called on opts, so a handle that uses
|
|
42
56
|
// `this` sees the object it was defined on.
|
|
@@ -46,7 +60,7 @@ export function projection(opts) {
|
|
|
46
60
|
const last = batch.at(-1);
|
|
47
61
|
let next = current;
|
|
48
62
|
if (last !== undefined) {
|
|
49
|
-
await offsets.save(name, last.offset);
|
|
63
|
+
await timed(ctx, 'saveOffset', offsets.save(name, last.offset));
|
|
50
64
|
next = { offset: last.offset };
|
|
51
65
|
}
|
|
52
66
|
// Exactly one poll is ever pending, in the mailbox or on the timer. A failure above consumes
|
|
@@ -58,7 +72,7 @@ export function projection(opts) {
|
|
|
58
72
|
return next;
|
|
59
73
|
},
|
|
60
74
|
async [START](_state, ctx) {
|
|
61
|
-
const offset = await offsets.load(name);
|
|
75
|
+
const offset = await timed(ctx, 'loadOffset', offsets.load(name));
|
|
62
76
|
ctx.self.tell(POLL);
|
|
63
77
|
return { offset };
|
|
64
78
|
},
|
|
@@ -68,7 +82,7 @@ export function projection(opts) {
|
|
|
68
82
|
}
|
|
69
83
|
function checkOptions(opts) {
|
|
70
84
|
checkKeys(opts, OPTIONS, 'projection');
|
|
71
|
-
const { name, persistence, tag, handle, pollMs, batchSize } = opts;
|
|
85
|
+
const { name, persistence, tag, handle, pollMs, batchSize, timeoutMs } = opts;
|
|
72
86
|
if (typeof name !== 'string' || name === '' || name.length > MAX_NAME_LENGTH) {
|
|
73
87
|
throw new TypeError(`projection: name must be a string of 1 to ${String(MAX_NAME_LENGTH)} characters, ` +
|
|
74
88
|
`got ${typeof name === 'string' ? `one of ${String(name.length)}` : describe(name)}`);
|
|
@@ -87,6 +101,8 @@ function checkOptions(opts) {
|
|
|
87
101
|
checkInt(pollMs, 'projection: pollMs');
|
|
88
102
|
if (batchSize !== undefined)
|
|
89
103
|
checkInt(batchSize, 'projection: batchSize');
|
|
104
|
+
if (timeoutMs !== undefined)
|
|
105
|
+
checkInt(timeoutMs, 'projection: timeoutMs');
|
|
90
106
|
}
|
|
91
107
|
function hasStores(value) {
|
|
92
108
|
if (typeof value !== 'object' || value === null)
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { Transport } from '../cluster/transport.js';
|
|
2
|
+
import type { Scheduler } from '../scheduler.js';
|
|
3
|
+
import type { ManualScheduler } from '../testkit/manual-scheduler.js';
|
|
4
|
+
/** Options for a {@link NodeClock}. */
|
|
5
|
+
export interface NodeClockOptions {
|
|
6
|
+
/**
|
|
7
|
+
* How far the clock's rate is from simulation time's, in parts per million: at 200, it reads
|
|
8
|
+
* 1,000,200 ms while a million pass. An integer from -100,000 to 100,000. Default 0.
|
|
9
|
+
*/
|
|
10
|
+
driftPpm?: number;
|
|
11
|
+
/** What its `now()` reads when it's made, in ms. Default the simulation's time then. */
|
|
12
|
+
startAt?: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* One node's scheduler in a simulation: a view onto the simulation's {@link ManualScheduler} at its
|
|
16
|
+
* own rate. Its `now()` is whole milliseconds and never goes backwards, and its timers fire when
|
|
17
|
+
* its own clock reaches them.
|
|
18
|
+
*
|
|
19
|
+
* {@link NodeClock.pause} stops the node's process, as a long GC or a suspended VM does: none of its
|
|
20
|
+
* tasks or timers run, though its clock keeps going. {@link NodeClock.resume} then runs everything
|
|
21
|
+
* that fell due meanwhile, in the order it fell due, before anything new.
|
|
22
|
+
*/
|
|
23
|
+
export declare class NodeClock implements Scheduler {
|
|
24
|
+
#private;
|
|
25
|
+
/**
|
|
26
|
+
* Makes a clock for one node on `sim`.
|
|
27
|
+
* @throws TypeError for options that can't be used.
|
|
28
|
+
*/
|
|
29
|
+
constructor(sim: ManualScheduler, opts?: NodeClockOptions);
|
|
30
|
+
/** The node's own time, in whole ms. */
|
|
31
|
+
now(): number;
|
|
32
|
+
/** Whether the node is paused. */
|
|
33
|
+
get paused(): boolean;
|
|
34
|
+
/** Queues `task` on the simulation, to run as this node's. */
|
|
35
|
+
runSoon(task: () => void): void;
|
|
36
|
+
/**
|
|
37
|
+
* Runs `task` once this node's clock has moved `delayMs` on: at the first whole millisecond of
|
|
38
|
+
* simulation time when `now()` reads at least that. A negative or `NaN` delay counts as 0.
|
|
39
|
+
*/
|
|
40
|
+
setTimer(task: () => void, delayMs: number): () => void;
|
|
41
|
+
/** Stops the node's process. Its clock keeps going. Pausing a paused node does nothing. */
|
|
42
|
+
pause(): void;
|
|
43
|
+
/**
|
|
44
|
+
* Lets the node run again: what fell due while it was paused runs on the simulation's next turn,
|
|
45
|
+
* in order, before anything that falls due later. Resuming a running node does nothing.
|
|
46
|
+
*/
|
|
47
|
+
resume(): void;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Wraps `transport` so that everything it hands its node, from new connections and frames to
|
|
51
|
+
* closes and refusals, arrives through the node's `clock`. While the node is paused, its frames
|
|
52
|
+
* wait at its sockets and it can't answer anyone, and when it resumes they arrive in order.
|
|
53
|
+
*/
|
|
54
|
+
export declare function throughClock(transport: Transport, clock: Scheduler): Transport;
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
// A node's clock in a simulation (decision 0033). Every node of a run shares one simulation clock,
|
|
2
|
+
// a ManualScheduler that nothing in the cluster reads. Each node gets a view onto it, with its own
|
|
3
|
+
// rate and its own reading, so a lease timed on a holder's clock can disagree with the store's.
|
|
4
|
+
//
|
|
5
|
+
// A node clock keeps no timer queue of its own: every task and timer goes onto the simulation's
|
|
6
|
+
// heap, converted to simulation time. One heap orders everything every node does, by due time and
|
|
7
|
+
// then creation, which is what makes a run repeat exactly.
|
|
8
|
+
import { checkInt, checkKeys } from '../checks.js';
|
|
9
|
+
const OPTIONS = new Set(['driftPpm', 'startAt']);
|
|
10
|
+
const MAX_DRIFT_PPM = 100_000;
|
|
11
|
+
/**
|
|
12
|
+
* One node's scheduler in a simulation: a view onto the simulation's {@link ManualScheduler} at its
|
|
13
|
+
* own rate. Its `now()` is whole milliseconds and never goes backwards, and its timers fire when
|
|
14
|
+
* its own clock reaches them.
|
|
15
|
+
*
|
|
16
|
+
* {@link NodeClock.pause} stops the node's process, as a long GC or a suspended VM does: none of its
|
|
17
|
+
* tasks or timers run, though its clock keeps going. {@link NodeClock.resume} then runs everything
|
|
18
|
+
* that fell due meanwhile, in the order it fell due, before anything new.
|
|
19
|
+
*/
|
|
20
|
+
export class NodeClock {
|
|
21
|
+
#sim;
|
|
22
|
+
#simStart;
|
|
23
|
+
#nodeStart;
|
|
24
|
+
#rate;
|
|
25
|
+
#paused = false;
|
|
26
|
+
// What fell due while paused, and, until it has all run, what fell due after.
|
|
27
|
+
#backlog = [];
|
|
28
|
+
#flushQueued = false;
|
|
29
|
+
/**
|
|
30
|
+
* Makes a clock for one node on `sim`.
|
|
31
|
+
* @throws TypeError for options that can't be used.
|
|
32
|
+
*/
|
|
33
|
+
constructor(sim, opts = {}) {
|
|
34
|
+
checkKeys(opts, OPTIONS, 'NodeClock');
|
|
35
|
+
const { driftPpm = 0, startAt = sim.now() } = opts;
|
|
36
|
+
checkInt(driftPpm, 'NodeClock: driftPpm', { min: -MAX_DRIFT_PPM });
|
|
37
|
+
if (driftPpm > MAX_DRIFT_PPM) {
|
|
38
|
+
throw new TypeError(`NodeClock: driftPpm must be at most ${String(MAX_DRIFT_PPM)}, got ${String(driftPpm)}`);
|
|
39
|
+
}
|
|
40
|
+
checkInt(startAt, 'NodeClock: startAt', { min: 0 });
|
|
41
|
+
this.#sim = sim;
|
|
42
|
+
this.#simStart = sim.now();
|
|
43
|
+
this.#nodeStart = startAt;
|
|
44
|
+
this.#rate = 1 + driftPpm / 1_000_000;
|
|
45
|
+
}
|
|
46
|
+
/** The node's own time, in whole ms. */
|
|
47
|
+
now() {
|
|
48
|
+
return this.#at(this.#sim.now());
|
|
49
|
+
}
|
|
50
|
+
/** Whether the node is paused. */
|
|
51
|
+
get paused() {
|
|
52
|
+
return this.#paused;
|
|
53
|
+
}
|
|
54
|
+
/** Queues `task` on the simulation, to run as this node's. */
|
|
55
|
+
runSoon(task) {
|
|
56
|
+
const item = { task, cancelled: false };
|
|
57
|
+
this.#sim.runSoon(() => {
|
|
58
|
+
this.#fire(item);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Runs `task` once this node's clock has moved `delayMs` on: at the first whole millisecond of
|
|
63
|
+
* simulation time when `now()` reads at least that. A negative or `NaN` delay counts as 0.
|
|
64
|
+
*/
|
|
65
|
+
setTimer(task, delayMs) {
|
|
66
|
+
const delay = Number.isNaN(delayMs) ? 0 : Math.max(0, delayMs);
|
|
67
|
+
const item = { task, cancelled: false };
|
|
68
|
+
const cancel = this.#sim.setTimer(() => {
|
|
69
|
+
this.#fire(item);
|
|
70
|
+
}, this.#simDelay(this.now() + delay));
|
|
71
|
+
return () => {
|
|
72
|
+
item.cancelled = true;
|
|
73
|
+
cancel();
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/** Stops the node's process. Its clock keeps going. Pausing a paused node does nothing. */
|
|
77
|
+
pause() {
|
|
78
|
+
this.#paused = true;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Lets the node run again: what fell due while it was paused runs on the simulation's next turn,
|
|
82
|
+
* in order, before anything that falls due later. Resuming a running node does nothing.
|
|
83
|
+
*/
|
|
84
|
+
resume() {
|
|
85
|
+
if (!this.#paused)
|
|
86
|
+
return;
|
|
87
|
+
this.#paused = false;
|
|
88
|
+
this.#queueFlush();
|
|
89
|
+
}
|
|
90
|
+
// Runs an item now, unless the node is paused or still has a backlog to run first.
|
|
91
|
+
#fire(item) {
|
|
92
|
+
if (item.cancelled)
|
|
93
|
+
return;
|
|
94
|
+
if (this.#paused || this.#backlog.length > 0) {
|
|
95
|
+
this.#backlog.push(item);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
item.task();
|
|
99
|
+
}
|
|
100
|
+
#queueFlush() {
|
|
101
|
+
if (this.#flushQueued || this.#backlog.length === 0)
|
|
102
|
+
return;
|
|
103
|
+
this.#flushQueued = true;
|
|
104
|
+
this.#sim.runSoon(this.#flush);
|
|
105
|
+
}
|
|
106
|
+
#flush = () => {
|
|
107
|
+
this.#flushQueued = false;
|
|
108
|
+
// One at a time, so a task that throws leaves the rest queued for the next turn.
|
|
109
|
+
while (!this.#paused) {
|
|
110
|
+
const item = this.#backlog.shift();
|
|
111
|
+
if (item === undefined)
|
|
112
|
+
return;
|
|
113
|
+
if (item.cancelled)
|
|
114
|
+
continue;
|
|
115
|
+
try {
|
|
116
|
+
item.task();
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
this.#queueFlush();
|
|
120
|
+
throw error;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
// The node's reading at simulation time `sim`.
|
|
125
|
+
#at(sim) {
|
|
126
|
+
return this.#nodeStart + Math.floor((sim - this.#simStart) * this.#rate);
|
|
127
|
+
}
|
|
128
|
+
// How long from now in simulation time, in whole ms, until this node's clock reads `target`.
|
|
129
|
+
#simDelay(target) {
|
|
130
|
+
const now = this.#sim.now();
|
|
131
|
+
let delay = Math.max(0, Math.ceil((target - this.#nodeStart) / this.#rate + this.#simStart - now));
|
|
132
|
+
// The division can land a millisecond either side of the first one that reaches `target`.
|
|
133
|
+
while (this.#at(now + delay) < target)
|
|
134
|
+
delay++;
|
|
135
|
+
while (delay > 0 && this.#at(now + delay - 1) >= target)
|
|
136
|
+
delay--;
|
|
137
|
+
return delay;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Wraps `transport` so that everything it hands its node, from new connections and frames to
|
|
142
|
+
* closes and refusals, arrives through the node's `clock`. While the node is paused, its frames
|
|
143
|
+
* wait at its sockets and it can't answer anyone, and when it resumes they arrive in order.
|
|
144
|
+
*/
|
|
145
|
+
export function throughClock(transport, clock) {
|
|
146
|
+
// Listens on the real connection at once, as a socket buffers what arrives before anyone reads,
|
|
147
|
+
// so frames that come while a new connection waits for a paused node aren't lost.
|
|
148
|
+
const wrap = (connection) => {
|
|
149
|
+
const frameListeners = [];
|
|
150
|
+
const closeListeners = [];
|
|
151
|
+
let closed;
|
|
152
|
+
connection.onFrame((frame) => {
|
|
153
|
+
clock.runSoon(() => {
|
|
154
|
+
for (const listener of frameListeners)
|
|
155
|
+
listener(frame);
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
connection.onClose((...args) => {
|
|
159
|
+
clock.runSoon(() => {
|
|
160
|
+
closed = args;
|
|
161
|
+
for (const listener of closeListeners.splice(0))
|
|
162
|
+
listener(...args);
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
return {
|
|
166
|
+
send: (frame) => {
|
|
167
|
+
connection.send(frame);
|
|
168
|
+
},
|
|
169
|
+
onFrame: (listener) => {
|
|
170
|
+
frameListeners.push(listener);
|
|
171
|
+
},
|
|
172
|
+
onClose: (listener) => {
|
|
173
|
+
const args = closed;
|
|
174
|
+
if (args === undefined)
|
|
175
|
+
closeListeners.push(listener);
|
|
176
|
+
else
|
|
177
|
+
clock.runSoon(() => {
|
|
178
|
+
listener(...args);
|
|
179
|
+
});
|
|
180
|
+
},
|
|
181
|
+
close: () => {
|
|
182
|
+
connection.close();
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
};
|
|
186
|
+
return {
|
|
187
|
+
address: transport.address,
|
|
188
|
+
listen: (onConnection, onRefused) => transport.listen((connection) => {
|
|
189
|
+
const wrapped = wrap(connection);
|
|
190
|
+
clock.runSoon(() => {
|
|
191
|
+
onConnection(wrapped);
|
|
192
|
+
});
|
|
193
|
+
}, onRefused === undefined
|
|
194
|
+
? undefined
|
|
195
|
+
: (reason) => {
|
|
196
|
+
clock.runSoon(() => {
|
|
197
|
+
onRefused(reason);
|
|
198
|
+
});
|
|
199
|
+
}),
|
|
200
|
+
connect: (address) => wrap(transport.connect(address)),
|
|
201
|
+
close: () => transport.close(),
|
|
202
|
+
};
|
|
203
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { type ShardStore } from '../cluster/shard-store.js';
|
|
2
|
+
import type { Persistence, StoredEvent } from '../persistence/types.js';
|
|
3
|
+
import type { Scheduler } from '../scheduler.js';
|
|
4
|
+
/** Options for {@link simulatedDatabase}. */
|
|
5
|
+
export interface SimulatedDatabaseOptions {
|
|
6
|
+
/** The simulation's scheduler, which every request and reply travels on. */
|
|
7
|
+
scheduler: Scheduler;
|
|
8
|
+
/** The database's own random stream, which draws every latency, error and lost reply. */
|
|
9
|
+
random: () => number;
|
|
10
|
+
/**
|
|
11
|
+
* The range each request's and each reply's latency is drawn from, in whole ms, both ends
|
|
12
|
+
* included. Default `[1, 3]`.
|
|
13
|
+
*/
|
|
14
|
+
latencyMs?: readonly [number, number];
|
|
15
|
+
/**
|
|
16
|
+
* The database's wall clock, in integer ms, which the shard store judges lease expiry by and
|
|
17
|
+
* the journal stamps events with. {@link SimulatedDatabase.jump} adds to it. Default the
|
|
18
|
+
* scheduler's `now`.
|
|
19
|
+
*/
|
|
20
|
+
clock?: () => number;
|
|
21
|
+
}
|
|
22
|
+
/** The database as one node incarnation reaches it. */
|
|
23
|
+
export interface DatabaseClient {
|
|
24
|
+
/** The node the calls come from. */
|
|
25
|
+
readonly node: string;
|
|
26
|
+
/** The journal, snapshot store and offset store. */
|
|
27
|
+
readonly persistence: Required<Persistence>;
|
|
28
|
+
/** The shard store, on the same endpoint. */
|
|
29
|
+
readonly shards: ShardStore;
|
|
30
|
+
/**
|
|
31
|
+
* Ends the client, as its process dying does. Requests already sent are still applied, but
|
|
32
|
+
* every call still waiting fails at once, and so does every later one, so an actor system left
|
|
33
|
+
* running in the same process can still wind down.
|
|
34
|
+
*/
|
|
35
|
+
close(): void;
|
|
36
|
+
}
|
|
37
|
+
/** What a simulated database has done with calls so far. */
|
|
38
|
+
export interface DatabaseCounts {
|
|
39
|
+
/** Calls made with valid arguments. */
|
|
40
|
+
readonly calls: number;
|
|
41
|
+
/** Calls applied to the data. */
|
|
42
|
+
readonly applied: number;
|
|
43
|
+
/** Calls that failed with an error from {@link SimulatedDatabase.fail}, unapplied. */
|
|
44
|
+
readonly failed: number;
|
|
45
|
+
/** Calls refused because their node was cut off. */
|
|
46
|
+
readonly refused: number;
|
|
47
|
+
/** Calls a hang held when they arrived. */
|
|
48
|
+
readonly hung: number;
|
|
49
|
+
/** Replies {@link SimulatedDatabase.loseReplies} lost. */
|
|
50
|
+
readonly lostReplies: number;
|
|
51
|
+
/** Replies that arrived as a lost connection, because their node was cut off meanwhile. */
|
|
52
|
+
readonly disconnected: number;
|
|
53
|
+
/** Replies that came after their client closed, which had failed the call already. */
|
|
54
|
+
readonly dropped: number;
|
|
55
|
+
}
|
|
56
|
+
/** The calls a fault applies to: every node's, or only `node`'s. */
|
|
57
|
+
export interface CallSelector {
|
|
58
|
+
readonly node?: string;
|
|
59
|
+
}
|
|
60
|
+
/** One endpoint for a whole simulated cluster's persistence and shard leases, with faults. */
|
|
61
|
+
export interface SimulatedDatabase {
|
|
62
|
+
/**
|
|
63
|
+
* A client for one node incarnation. Its replies are handed back on `clock`, the node's own,
|
|
64
|
+
* so they wait while it's paused. Default the database's scheduler.
|
|
65
|
+
* @throws TypeError unless `node` is a non-empty string.
|
|
66
|
+
*/
|
|
67
|
+
connect(node: string, clock?: Scheduler): DatabaseClient;
|
|
68
|
+
/** The database's wall clock, jumps included. */
|
|
69
|
+
now(): number;
|
|
70
|
+
/**
|
|
71
|
+
* Draws the latency of every request and reply of the selected calls from `latencyMs` instead,
|
|
72
|
+
* if that's longer, until the returned function ends it.
|
|
73
|
+
*/
|
|
74
|
+
slow(opts: CallSelector & {
|
|
75
|
+
readonly latencyMs: readonly [number, number];
|
|
76
|
+
}): () => void;
|
|
77
|
+
/**
|
|
78
|
+
* Holds the selected calls that arrive, unapplied and unanswered, until the returned function
|
|
79
|
+
* lets go. Then they're applied, in the order they arrived. Never calling it hangs them for good.
|
|
80
|
+
*/
|
|
81
|
+
hang(opts?: CallSelector): () => void;
|
|
82
|
+
/**
|
|
83
|
+
* Fails each selected call with chance `rate` (default 1), without applying it, until the
|
|
84
|
+
* returned function ends it.
|
|
85
|
+
*/
|
|
86
|
+
fail(opts?: CallSelector & {
|
|
87
|
+
readonly rate?: number;
|
|
88
|
+
}): () => void;
|
|
89
|
+
/**
|
|
90
|
+
* Applies each selected call and loses its reply, with chance `rate` (default 1), until the
|
|
91
|
+
* returned function ends it. Its caller never hears.
|
|
92
|
+
*/
|
|
93
|
+
loseReplies(opts?: CallSelector & {
|
|
94
|
+
readonly rate?: number;
|
|
95
|
+
}): () => void;
|
|
96
|
+
/**
|
|
97
|
+
* Cuts `node` off, every incarnation of it, until the returned function heals it: its calls
|
|
98
|
+
* are refused, and replies on their way to it arrive as a lost connection.
|
|
99
|
+
* @throws TypeError unless `node` is a non-empty string.
|
|
100
|
+
*/
|
|
101
|
+
cutOff(node: string): () => void;
|
|
102
|
+
/**
|
|
103
|
+
* Jumps the database's wall clock `ms` forward, for good.
|
|
104
|
+
* @throws TypeError unless `ms` is a positive integer.
|
|
105
|
+
*/
|
|
106
|
+
jump(ms: number): void;
|
|
107
|
+
/** Every stored event, in offset order, or only `persistenceId`'s, read with no time passing. */
|
|
108
|
+
events(persistenceId?: string): readonly StoredEvent[];
|
|
109
|
+
/** What the database has done with calls so far. */
|
|
110
|
+
counts(): DatabaseCounts;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Makes a simulated database on `scheduler`, empty, with no faults until they're injected.
|
|
114
|
+
* @throws TypeError for options that can't be used.
|
|
115
|
+
*/
|
|
116
|
+
export declare function simulatedDatabase(opts: SimulatedDatabaseOptions): SimulatedDatabase;
|