deno.land / x / replicache@v10.0.0-beta.0 / persist / heartbeat.test.ts

heartbeat.test.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import {LogContext} from '@rocicorp/logger';import {expect} from '@esm-bundle/chai';import * as sinon from 'sinon';import {SinonFakeTimers, useFakeTimers} from 'sinon';import * as dag from '../dag/mod';import { latestHeartbeatUpdate, startHeartbeats, writeHeartbeat,} from './heartbeat';import {ClientMap, ClientStateNotFoundError, getClients} from './clients';import {fakeHash} from '../hash';import {makeClient, setClients} from './clients-test-helpers';import {assertNotUndefined} from '../asserts';
let clock: SinonFakeTimers;const START_TIME = 100000;const ONE_MIN_IN_MS = 60 * 1000;setup(() => { clock = useFakeTimers(START_TIME);});
teardown(() => { clock.restore();});
function awaitLatestHeartbeatUpdate(): Promise<ClientMap> { const latest = latestHeartbeatUpdate; assertNotUndefined(latest); return latest;}
test('startHeartbeats starts interval that writes heartbeat each minute', async () => { const dagStore = new dag.TestStore(); const client1 = { heartbeatTimestampMs: 1000, headHash: fakeHash('headclient1'), mutationID: 10, lastServerAckdMutationID: 10, }; const client2 = { heartbeatTimestampMs: 3000, headHash: fakeHash('headclient2'), mutationID: 100, lastServerAckdMutationID: 90, }; const clientMap = new Map( Object.entries({ client1, client2, }), ); await setClients(clientMap, dagStore);
const controller = new AbortController(); startHeartbeats( 'client1', dagStore, () => undefined, new LogContext(), controller.signal, );
await dagStore.withRead(async (read: dag.Read) => { const readClientMap = await getClients(read); expect(readClientMap).to.deep.equal(clientMap); });
await clock.tickAsync(ONE_MIN_IN_MS); await awaitLatestHeartbeatUpdate();
await dagStore.withRead(async (read: dag.Read) => { const readClientMap = await getClients(read); expect(readClientMap).to.deep.equal( new Map( Object.entries({ client1: { ...client1, heartbeatTimestampMs: START_TIME + ONE_MIN_IN_MS, }, client2, }), ), ); });
await clock.tickAsync(ONE_MIN_IN_MS); await awaitLatestHeartbeatUpdate();
await dagStore.withRead(async (read: dag.Read) => { const readClientMap = await getClients(read); expect(readClientMap).to.deep.equal( new Map( Object.entries({ client1: { ...client1, heartbeatTimestampMs: START_TIME + ONE_MIN_IN_MS + ONE_MIN_IN_MS, }, client2, }), ), ); });});
test('calling function returned by startHeartbeats, stops heartbeats', async () => { const dagStore = new dag.TestStore(); const client1 = makeClient({ heartbeatTimestampMs: 1000, headHash: fakeHash('headclient1'), }); const client2 = makeClient({ heartbeatTimestampMs: 3000, headHash: fakeHash('headclient2'), }); const clientMap = new Map( Object.entries({ client1, client2, }), ); await setClients(clientMap, dagStore);
const controller = new AbortController(); startHeartbeats( 'client1', dagStore, () => undefined, new LogContext(), controller.signal, );
await dagStore.withRead(async (read: dag.Read) => { const readClientMap = await getClients(read); expect(readClientMap).to.deep.equal(clientMap); });
await clock.tickAsync(ONE_MIN_IN_MS); await awaitLatestHeartbeatUpdate();
await dagStore.withRead(async (read: dag.Read) => { const readClientMap = await getClients(read); expect(Object.fromEntries(readClientMap)).to.deep.equal({ client1: { ...client1, heartbeatTimestampMs: START_TIME + ONE_MIN_IN_MS, }, client2, }); });
controller.abort(); clock.tick(ONE_MIN_IN_MS); await awaitLatestHeartbeatUpdate();
await dagStore.withRead(async (read: dag.Read) => { const readClientMap = await getClients(read); expect(readClientMap).to.deep.equal( new Map( Object.entries({ client1: { ...client1, // Heartbeat *NOT* updated to START_TIME + ONE_MIN_IN_MS + ONE_MIN_IN_MS heartbeatTimestampMs: START_TIME + ONE_MIN_IN_MS, }, client2, }), ), ); });});
test('writeHeartbeat writes heartbeat', async () => { const dagStore = new dag.TestStore(); const client1 = { heartbeatTimestampMs: 1000, headHash: fakeHash('headclient1'), mutationID: 10, lastServerAckdMutationID: 10, }; const client2 = { heartbeatTimestampMs: 3000, headHash: fakeHash('headclient2'), mutationID: 100, lastServerAckdMutationID: 90, }; const clientMap = new Map( Object.entries({ client1, client2, }), );
await setClients(clientMap, dagStore);
const TICK_IN_MS = 20000; clock.tick(TICK_IN_MS);
await writeHeartbeat('client1', dagStore); await dagStore.withRead(async (read: dag.Read) => { const readClientMap = await getClients(read); expect(readClientMap).to.deep.equal( new Map( Object.entries({ client1: { ...client1, heartbeatTimestampMs: START_TIME + TICK_IN_MS, }, client2, }), ), ); });});
test('writeHeartbeat throws Error if no Client is found for clientID', async () => { const dagStore = new dag.TestStore(); let e; try { await writeHeartbeat('client1', dagStore); } catch (ex) { e = ex; } expect(e) .to.be.instanceOf(ClientStateNotFoundError) .property('id', 'client1');});
test('heartbeat with missing client calls callback', async () => { const dagStore = new dag.TestStore(); const onClientStateNotFound = sinon.fake(); const controller = new AbortController(); startHeartbeats( 'client1', dagStore, onClientStateNotFound, new LogContext(), controller.signal, ); await clock.tickAsync(ONE_MIN_IN_MS); expect(onClientStateNotFound.callCount).to.equal(1); controller.abort();});
replicache

Version Info

Tagged at
2 years ago