Skip to content

Commit

Permalink
[no-issue]: Reduce flakiness in sync queue related tests (#4170)
Browse files Browse the repository at this point in the history
  • Loading branch information
rohan-bes authored Sep 15, 2022
1 parent 9e58433 commit a195574
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
24 changes: 19 additions & 5 deletions packages/central-server/src/tests/apiV2/changes/getChanges.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('GET /changes/count', async () => {

// Set up real sync queue for testing the /changes endpoint
await createPermissionsBasedMeditrakSyncQueue(models.database);
meditrakSyncQueue.setDebounceTime(100); // Faster debounce time for tests
meditrakSyncQueue.setDebounceTime(50); // Faster debounce time for tests
meditrakSyncQueue.listenForChanges();
});

Expand Down Expand Up @@ -50,7 +50,8 @@ describe('GET /changes/count', async () => {
}
// Wait one second for the triggers to have properly added the changes to the queue
await oneSecondSleep();
await meditrakSyncQueue.scheduleChangeQueueHandler();
// Wait for the triggers to have properly added the changes to the queue
await models.database.waitForAllChangeHandlers();
const response = await app.get(`changes/count?since=${since}`);
expect(response.body.changeCount).to.equal(numberOfQuestionsToAdd);
});
Expand All @@ -74,6 +75,10 @@ describe('GET /changes/count', async () => {

// Add some more questions
await oneSecondSleep();

// Wait for the triggers to have properly added the changes to the queue
await models.database.waitForAllChangeHandlers();

const timestampBeforeSecondUpdate = Date.now();
await oneSecondSleep();
const numberOfQuestionsToAddInSecondUpdate = randomIntBetween(1, 20);
Expand All @@ -84,6 +89,10 @@ describe('GET /changes/count', async () => {

// Delete some of the questions added in the first update
await oneSecondSleep();

// Wait for the triggers to have properly added the changes to the queue
await models.database.waitForAllChangeHandlers();

const timestampBeforeFirstDelete = Date.now();
await oneSecondSleep();
const numberOfQuestionsToDeleteFromFirstUpdate = randomIntBetween(
Expand All @@ -96,6 +105,10 @@ describe('GET /changes/count', async () => {

// Delete some of the questions added in the second update
await oneSecondSleep();

// Wait for the triggers to have properly added the changes to the queue
await models.database.waitForAllChangeHandlers();

const timestampBeforeSecondDelete = Date.now();
await oneSecondSleep();
const numberOfQuestionsToDeleteFromSecondUpdate = randomIntBetween(
Expand All @@ -106,8 +119,9 @@ describe('GET /changes/count', async () => {
await models.question.deleteById(newQuestionsInSecondUpdate[i].id);
}

// Wait one second for the triggers to have properly added the changes to the queue
// Wait for the triggers to have properly added the changes to the queue
await oneSecondSleep();
await models.database.waitForAllChangeHandlers();

// If syncing from before the first update, should only need to sync the number of records that
// actually need to be added. No need to know about deletes of records we never integrated
Expand All @@ -116,7 +130,6 @@ describe('GET /changes/count', async () => {
const totalDeletes =
numberOfQuestionsToDeleteFromFirstUpdate + numberOfQuestionsToDeleteFromSecondUpdate;
const netNewRecords = grossNewRecords - totalDeletes;
await meditrakSyncQueue.scheduleChangeQueueHandler();
let response = await app.get(`changes/count?since=${timestampBeforeFirstUpdate}`);
expect(response.body.changeCount).to.equal(netNewRecords);

Expand Down Expand Up @@ -151,7 +164,8 @@ describe('GET /changes/count', async () => {
await upsertEntity(); // version 1.7.102

await oneSecondSleep();
await meditrakSyncQueue.scheduleChangeQueueHandler();
// Wait for the triggers to have properly added the changes to the queue
await models.database.waitForAllChangeHandlers();
let response = await app.get(`changes/count?since=${since}&appVersion=0.0.1`);
expect(response.body.changeCount).to.equal(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('changes/count', () => {
const syncableChangeEnqueuer = new SyncableChangeEnqueuer(
getTestModels() as MeditrakAppServerModelRegistry,
);
syncableChangeEnqueuer.setDebounceTime(100);
syncableChangeEnqueuer.setDebounceTime(50);

beforeAll(async () => {
syncableChangeEnqueuer.listenForChanges();
Expand Down Expand Up @@ -112,6 +112,10 @@ describe('changes/count', () => {

// Add some more questions
await oneSecondSleep();

// Wait for the triggers to have properly added the changes to the queue
await models.database.waitForAllChangeHandlers();

const timestampBeforeSecondUpdate = Date.now();
await oneSecondSleep();
const numberOfQuestionsToAddInSecondUpdate = randomIntBetween(1, 20);
Expand All @@ -122,6 +126,10 @@ describe('changes/count', () => {

// Delete some of the questions added in the first update
await oneSecondSleep();

// Wait for the triggers to have properly added the changes to the queue
await models.database.waitForAllChangeHandlers();

const timestampBeforeFirstDelete = Date.now();
await oneSecondSleep();
const numberOfQuestionsToDeleteFromFirstUpdate = randomIntBetween(
Expand All @@ -134,6 +142,10 @@ describe('changes/count', () => {

// Delete some of the questions added in the second update
await oneSecondSleep();

// Wait for the triggers to have properly added the changes to the queue
await models.database.waitForAllChangeHandlers();

const timestampBeforeSecondDelete = Date.now();
await oneSecondSleep();
const numberOfQuestionsToDeleteFromSecondUpdate = randomIntBetween(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ describe('changes (GET)', () => {

// Add some more questions
await oneSecondSleep();

// Wait for the triggers to have properly added the changes to the queue
await models.database.waitForAllChangeHandlers();

const timestampBeforeSecondUpdate = Date.now();
await oneSecondSleep();
const numberOfQuestionsToAddInSecondUpdate = 4;
Expand All @@ -188,6 +192,10 @@ describe('changes (GET)', () => {

// Delete some of the questions added in the first update
await oneSecondSleep();

// Wait for the triggers to have properly added the changes to the queue
await models.database.waitForAllChangeHandlers();

const timestampBeforeFirstDelete = Date.now();
await oneSecondSleep();
const numberOfQuestionsToDeleteFromFirstUpdate = randomIntBetween(
Expand All @@ -208,6 +216,10 @@ describe('changes (GET)', () => {

// Delete some of the questions added in the second update
await oneSecondSleep();

// Wait for the triggers to have properly added the changes to the queue
await models.database.waitForAllChangeHandlers();

const timestampBeforeSecondDelete = Date.now();
await oneSecondSleep();
const numberOfQuestionsToDeleteFromSecondUpdate = randomIntBetween(
Expand Down

0 comments on commit a195574

Please sign in to comment.