Skip to content

Commit

Permalink
moving pollingTimeout out of the state
Browse files Browse the repository at this point in the history
  • Loading branch information
Paula Stachova committed Nov 12, 2024
1 parent 747d4b0 commit 46e6b3d
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 117 deletions.
73 changes: 66 additions & 7 deletions packages/compass-global-writes/src/store/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,34 @@ function createAuthFetchResponse<
};
}

function wait(ms: number) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}

const expectPolling = async ({
spy,
clock,
interval,
reverse,
}: {
spy: Sinon.SinonSpy;
clock: Sinon.SinonFakeTimers;
interval: number;
reverse?: boolean;
}) => {
spy.resetHistory();
clock.tick(interval);
// leaving time for the poll to actually execute after the clock tick
await wait(1);
if (!reverse) {
expect(spy).to.have.been.called;
} else {
expect(spy).not.to.have.been.called;
}
};

function createStore({
isNamespaceManaged = () => false,
hasShardingError = () => false,
Expand Down Expand Up @@ -239,9 +267,10 @@ describe('GlobalWritesStore Store', function () {
it('not managed -> sharding -> shard key correct', async function () {
let mockShardKey = false;
let mockManagedNamespace = false;
const hasShardKey = Sinon.fake(() => mockShardKey);
// initial state === unsharded
const store = createStore({
hasShardKey: Sinon.fake(() => mockShardKey),
hasShardKey,
isNamespaceManaged: Sinon.fake(() => mockManagedNamespace),
});
await waitFor(() => {
Expand All @@ -261,13 +290,26 @@ describe('GlobalWritesStore Store', function () {
await promise;
expect(store.getState().status).to.equal('SHARDING');

// polling continues for a while
clock.tick(POLLING_INTERVAL);
clock.tick(POLLING_INTERVAL);
// empty polling for a while
await expectPolling({
clock,
interval: POLLING_INTERVAL,
spy: hasShardKey,
});
await expectPolling({
clock,
interval: POLLING_INTERVAL,
spy: hasShardKey,
});

// sharding ends with a shardKey
mockShardKey = true;
clock.tick(POLLING_INTERVAL);
await expectPolling({
clock,
interval: POLLING_INTERVAL,
spy: hasShardKey,
});

await waitFor(() => {
expect(store.getState().status).to.equal('SHARD_KEY_CORRECT');
expect(store.getState().userActionInProgress).to.be.undefined;
Expand Down Expand Up @@ -400,24 +442,41 @@ describe('GlobalWritesStore Store', function () {

it('sharding -> cancelling request -> not managed', async function () {
let mockManagedNamespace = true;
const hasShardKey = Sinon.fake(() => false);
confirmationStub.resolves(true);
// initial state === sharding
clock = sinon.useFakeTimers({
shouldAdvanceTime: true,
});
const store = createStore({
isNamespaceManaged: Sinon.fake(() => mockManagedNamespace),
hasShardKey,
});
await waitFor(() => {
expect(store.getState().status).to.equal('SHARDING');
expect(store.getState().pollingTimeout).not.to.be.undefined;
expect(store.getState().managedNamespace).to.equal(managedNamespace);
});

await expectPolling({
clock,
interval: POLLING_INTERVAL,
spy: hasShardKey,
});

// user cancels the sharding request
const promise = store.dispatch(cancelSharding());
mockManagedNamespace = false;
await promise;
expect(store.getState().status).to.equal('UNSHARDED');
expect(store.getState().pollingTimeout).to.be.undefined;
expect(confirmationStub).to.have.been.called;

// is no longer polling
await expectPolling({
reverse: true,
clock,
interval: POLLING_INTERVAL,
spy: hasShardKey,
});
});

it('valid shard key', async function () {
Expand Down
7 changes: 7 additions & 0 deletions packages/compass-global-writes/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type GlobalWritesExtraArgs = {
track: TrackFunction;
connectionInfoRef: ConnectionInfoRef;
atlasGlobalWritesService: AtlasGlobalWritesService;
pollingTimeoutRef: {
current: ReturnType<typeof setTimeout> | null;
};
};

export type GlobalWritesThunkAction<R, A extends Action> = ThunkAction<
Expand Down Expand Up @@ -60,6 +63,9 @@ export function activateGlobalWritesPlugin(
atlasService,
connectionInfoRef
);
const pollingTimeoutRef = {
current: null,
};
const store: GlobalWritesStore = createStore(
reducer,
{
Expand All @@ -73,6 +79,7 @@ export function activateGlobalWritesPlugin(
track,
connectionInfoRef,
atlasGlobalWritesService,
pollingTimeoutRef,
})
)
);
Expand Down
Loading

0 comments on commit 46e6b3d

Please sign in to comment.