Skip to content

Commit

Permalink
address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
paula-stacho committed Nov 4, 2024
1 parent 0d21433 commit 618a683
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
css,
ButtonVariant,
Link,
SpinLoader,
} from '@mongodb-js/compass-components';
import React from 'react';
import ShardKeyMarkup from '../shard-key-markup';
Expand Down Expand Up @@ -68,6 +69,7 @@ export function IncompleteShardingSetup({
onClick={onResume}
variant={ButtonVariant.Default}
isLoading={isSubmittingForSharding}
loadingIndicator={<SpinLoader />}
className={manageBtnStyles}
>
Enable Global Writes
Expand Down
70 changes: 68 additions & 2 deletions packages/compass-global-writes/src/store/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function createStore({
} = {}): GlobalWritesStore {
const atlasService = {
authenticatedFetch: (uri: string) => {
if (uri.includes(`/geoSharding`) && failsOnShardingRequest()) {
if (uri.endsWith(`/geoSharding`) && failsOnShardingRequest()) {
return Promise.reject(new Error('Failed to shard'));
}

Expand Down Expand Up @@ -313,7 +313,7 @@ describe('GlobalWritesStore Store', function () {
});
});

it('incomplete setup -> shard key correct', async function () {
it('incomplete setup -> sharding -> shard key correct', async function () {
// initial state -> incomplete shardingSetup
clock = sinon.useFakeTimers({
shouldAdvanceTime: true,
Expand All @@ -335,12 +335,78 @@ describe('GlobalWritesStore Store', function () {
'SUBMITTING_FOR_SHARDING_INCOMPLETE'
);
await promise;

// sharding
expect(store.getState().status).to.equal('SHARDING');

// done
clock.tick(POLLING_INTERVAL);
await waitFor(() => {
expect(store.getState().status).to.equal('SHARD_KEY_CORRECT');
});
});

it('incomplete setup -> sharding -> incomplete setup (request was cancelled)', async function () {
// initial state -> incomplete shardingSetup
clock = sinon.useFakeTimers({
shouldAdvanceTime: true,
});
const store = createStore({
isNamespaceManaged: () => false,
hasShardKey: () => true,
});
await waitFor(() => {
expect(store.getState().status).to.equal('INCOMPLETE_SHARDING_SETUP');
expect(store.getState().managedNamespace).to.be.undefined;
});

// user asks to resume geosharding
const promise = store.dispatch(resumeManagedNamespace());
expect(store.getState().status).to.equal(
'SUBMITTING_FOR_SHARDING_INCOMPLETE'
);
await promise;

// sharding
expect(store.getState().status).to.equal('SHARDING');

// user cancels the request - we go back to incomplete
const promise2 = store.dispatch(cancelSharding());
await promise2;
clock.tick(POLLING_INTERVAL);
await waitFor(() => {
expect(store.getState().status).to.equal('INCOMPLETE_SHARDING_SETUP');
});
});

it('incomplete setup -> incomplete setup (failed manage attempt)', async function () {
// initial state -> incomplete shardingSetup
clock = sinon.useFakeTimers({
shouldAdvanceTime: true,
});
const store = createStore({
isNamespaceManaged: () => false,
hasShardKey: () => true,
failsOnShardingRequest: () => true,
});
await waitFor(() => {
expect(store.getState().status).to.equal('INCOMPLETE_SHARDING_SETUP');
expect(store.getState().managedNamespace).to.be.undefined;
});

// user asks to resume geosharding
const promise = store.dispatch(resumeManagedNamespace());
expect(store.getState().status).to.equal(
'SUBMITTING_FOR_SHARDING_INCOMPLETE'
);
await promise;

// it failed
await waitFor(() => {
expect(store.getState().status).to.equal('INCOMPLETE_SHARDING_SETUP');
});
});

it('valid shard key -> incomplete', async function () {
// initial state === shard key correct
const store = createStore({
Expand Down
32 changes: 31 additions & 1 deletion packages/compass-global-writes/src/store/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,9 @@ const reducer: Reducer<RootState, Action> = (state = initialState, action) => {
) {
return {
...state,
status: ShardingStatuses.UNSHARDED,
status: state.shardKey
? ShardingStatuses.INCOMPLETE_SHARDING_SETUP
: ShardingStatuses.UNSHARDED,
shardingError: undefined,
};
}
Expand All @@ -511,6 +513,34 @@ const reducer: Reducer<RootState, Action> = (state = initialState, action) => {
};
}

if (
isAction<SubmittingForShardingErroredAction>(
action,
GlobalWritesActionTypes.SubmittingForShardingErrored
) &&
state.status === ShardingStatuses.SUBMITTING_FOR_SHARDING_ERROR
) {
return {
...state,
managedNamespace: undefined,
status: ShardingStatuses.SUBMITTING_FOR_SHARDING_ERROR,
};
}

if (
isAction<SubmittingForShardingErroredAction>(
action,
GlobalWritesActionTypes.SubmittingForShardingErrored
) &&
state.status === ShardingStatuses.SUBMITTING_FOR_SHARDING_INCOMPLETE
) {
return {
...state,
managedNamespace: undefined,
status: ShardingStatuses.INCOMPLETE_SHARDING_SETUP,
};
}

if (
isAction<UnmanagingNamespaceStartedAction>(
action,
Expand Down

0 comments on commit 618a683

Please sign in to comment.