Skip to content

Commit 46e6b3d

Browse files
committed
moving pollingTimeout out of the state
1 parent 747d4b0 commit 46e6b3d

File tree

3 files changed

+99
-117
lines changed

3 files changed

+99
-117
lines changed

packages/compass-global-writes/src/store/index.spec.ts

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,34 @@ function createAuthFetchResponse<
6767
};
6868
}
6969

70+
function wait(ms: number) {
71+
return new Promise((resolve) => {
72+
setTimeout(resolve, ms);
73+
});
74+
}
75+
76+
const expectPolling = async ({
77+
spy,
78+
clock,
79+
interval,
80+
reverse,
81+
}: {
82+
spy: Sinon.SinonSpy;
83+
clock: Sinon.SinonFakeTimers;
84+
interval: number;
85+
reverse?: boolean;
86+
}) => {
87+
spy.resetHistory();
88+
clock.tick(interval);
89+
// leaving time for the poll to actually execute after the clock tick
90+
await wait(1);
91+
if (!reverse) {
92+
expect(spy).to.have.been.called;
93+
} else {
94+
expect(spy).not.to.have.been.called;
95+
}
96+
};
97+
7098
function createStore({
7199
isNamespaceManaged = () => false,
72100
hasShardingError = () => false,
@@ -239,9 +267,10 @@ describe('GlobalWritesStore Store', function () {
239267
it('not managed -> sharding -> shard key correct', async function () {
240268
let mockShardKey = false;
241269
let mockManagedNamespace = false;
270+
const hasShardKey = Sinon.fake(() => mockShardKey);
242271
// initial state === unsharded
243272
const store = createStore({
244-
hasShardKey: Sinon.fake(() => mockShardKey),
273+
hasShardKey,
245274
isNamespaceManaged: Sinon.fake(() => mockManagedNamespace),
246275
});
247276
await waitFor(() => {
@@ -261,13 +290,26 @@ describe('GlobalWritesStore Store', function () {
261290
await promise;
262291
expect(store.getState().status).to.equal('SHARDING');
263292

264-
// polling continues for a while
265-
clock.tick(POLLING_INTERVAL);
266-
clock.tick(POLLING_INTERVAL);
293+
// empty polling for a while
294+
await expectPolling({
295+
clock,
296+
interval: POLLING_INTERVAL,
297+
spy: hasShardKey,
298+
});
299+
await expectPolling({
300+
clock,
301+
interval: POLLING_INTERVAL,
302+
spy: hasShardKey,
303+
});
267304

268305
// sharding ends with a shardKey
269306
mockShardKey = true;
270-
clock.tick(POLLING_INTERVAL);
307+
await expectPolling({
308+
clock,
309+
interval: POLLING_INTERVAL,
310+
spy: hasShardKey,
311+
});
312+
271313
await waitFor(() => {
272314
expect(store.getState().status).to.equal('SHARD_KEY_CORRECT');
273315
expect(store.getState().userActionInProgress).to.be.undefined;
@@ -400,24 +442,41 @@ describe('GlobalWritesStore Store', function () {
400442

401443
it('sharding -> cancelling request -> not managed', async function () {
402444
let mockManagedNamespace = true;
445+
const hasShardKey = Sinon.fake(() => false);
403446
confirmationStub.resolves(true);
404447
// initial state === sharding
448+
clock = sinon.useFakeTimers({
449+
shouldAdvanceTime: true,
450+
});
405451
const store = createStore({
406452
isNamespaceManaged: Sinon.fake(() => mockManagedNamespace),
453+
hasShardKey,
407454
});
408455
await waitFor(() => {
409456
expect(store.getState().status).to.equal('SHARDING');
410-
expect(store.getState().pollingTimeout).not.to.be.undefined;
411457
expect(store.getState().managedNamespace).to.equal(managedNamespace);
412458
});
413459

460+
await expectPolling({
461+
clock,
462+
interval: POLLING_INTERVAL,
463+
spy: hasShardKey,
464+
});
465+
414466
// user cancels the sharding request
415467
const promise = store.dispatch(cancelSharding());
416468
mockManagedNamespace = false;
417469
await promise;
418470
expect(store.getState().status).to.equal('UNSHARDED');
419-
expect(store.getState().pollingTimeout).to.be.undefined;
420471
expect(confirmationStub).to.have.been.called;
472+
473+
// is no longer polling
474+
await expectPolling({
475+
reverse: true,
476+
clock,
477+
interval: POLLING_INTERVAL,
478+
spy: hasShardKey,
479+
});
421480
});
422481

423482
it('valid shard key', async function () {

packages/compass-global-writes/src/store/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ type GlobalWritesExtraArgs = {
2020
track: TrackFunction;
2121
connectionInfoRef: ConnectionInfoRef;
2222
atlasGlobalWritesService: AtlasGlobalWritesService;
23+
pollingTimeoutRef: {
24+
current: ReturnType<typeof setTimeout> | null;
25+
};
2326
};
2427

2528
export type GlobalWritesThunkAction<R, A extends Action> = ThunkAction<
@@ -60,6 +63,9 @@ export function activateGlobalWritesPlugin(
6063
atlasService,
6164
connectionInfoRef
6265
);
66+
const pollingTimeoutRef = {
67+
current: null,
68+
};
6369
const store: GlobalWritesStore = createStore(
6470
reducer,
6571
{
@@ -73,6 +79,7 @@ export function activateGlobalWritesPlugin(
7379
track,
7480
connectionInfoRef,
7581
atlasGlobalWritesService,
82+
pollingTimeoutRef,
7683
})
7784
)
7885
);

0 commit comments

Comments
 (0)