Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add negative test to detect replication failures #1445

Merged
merged 4 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {runReplicaReadTests} from '@gomomento/common-integration-tests';
import {SetupIntegrationTest} from '../../integration-setup';

const {cacheClientWithBalancedReadConcern, integrationTestCacheName} =
SetupIntegrationTest();

runReplicaReadTests(
cacheClientWithBalancedReadConcern,
integrationTestCacheName
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {runReplicaReadTests} from '@gomomento/common-integration-tests';
import {SetupIntegrationTest} from '../../integration-setup';

const {cacheClientWithBalancedReadConcern, integrationTestCacheName} =
SetupIntegrationTest();

runReplicaReadTests(
cacheClientWithBalancedReadConcern,
integrationTestCacheName
);
61 changes: 61 additions & 0 deletions packages/common-integration-tests/src/cache/replica-reads.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {v4} from 'uuid';
import {CacheGet, CacheSet} from '@gomomento/sdk-core';
import {expectWithMessage} from '../common-int-test-utils';
import {ICacheClient} from '@gomomento/sdk-core/dist/src/internal/clients/cache';

export function runReplicaReadTests(
cacheClientWithBalancedReadConcern: ICacheClient,
integrationTestCacheName: string
) {
describe('Replica Read Tests', () => {
it('should read the latest value after replication delay using balanced read concern', async () => {
const client = cacheClientWithBalancedReadConcern;
const numTrials = 10;
const delayBetweenTrials = 100;
const replicationDelayMs = 1000;
const trials = [];

const trialFn = async (trialNumber: number) => {
// Start this trial at it's own time plus a random delay
const startDelay =
trialNumber * delayBetweenTrials + (Math.random() - 0.5) * 10;
await new Promise(resolve => setTimeout(resolve, startDelay));

const cacheKey = v4();
const cacheValue = v4();

// Perform a set operation
const setResponse = await client.set(
integrationTestCacheName,
cacheKey,
cacheValue
);
expectWithMessage(() => {
expect(setResponse).toBeInstanceOf(CacheSet.Success);
}, `expected SUCCESS but got ${setResponse.toString()}`);

// Wait for replication to complete
await new Promise(resolve => setTimeout(resolve, replicationDelayMs));

// Verify that the value can be read
const getResponse = await client.get(
integrationTestCacheName,
cacheKey
);
expectWithMessage(() => {
expect(getResponse).toBeInstanceOf(CacheGet.Hit);
}, `expected HIT but got ${getResponse.toString()}`);

expectWithMessage(() => {
expect(getResponse.value()).toEqual(cacheValue);
}, `expected ${cacheValue} but got ${getResponse.value() ?? 'undefined'}`);
};

for (let i = 0; i < numTrials; i++) {
trials.push(trialFn(i));
}

await Promise.all(trials);
krispraws marked this conversation as resolved.
Show resolved Hide resolved
});
});
}
1 change: 1 addition & 0 deletions packages/common-integration-tests/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export * from './cache/update-ttl';
export * from './leaderboard/leaderboard-client';
export * from './webhooks/webhooks';
export * from './cache/batch-get-set';
export * from './cache/replica-reads';
export * from './storage/storage';
Loading