Skip to content

Commit

Permalink
Remove unnecessary check and fix broken tests
Browse files Browse the repository at this point in the history
Fixes failing executor tests that result in removing the check. Tests were changed to: await the async TestEnvironment initialize function, remove additional initialization of core module because it was already created in TestEnvironment initialization, fix timers for tests that check the delta queue to check after 1 flush.
  • Loading branch information
shepherd-l committed Aug 2, 2024
1 parent 65ed239 commit f680239
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
43 changes: 17 additions & 26 deletions __test__/unit/core/executors.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { TestEnvironment } from '../../support/environment/TestEnvironment';
import ModelCache from '../../../src/core/caching/ModelCache';
import CoreModule from '../../../src/core/CoreModule';
import { IdentityExecutor } from '../../../src/core/executors/IdentityExecutor';
import { PropertiesExecutor } from '../../../src/core/executors/PropertiesExecutor';
import { SubscriptionExecutor } from '../../../src/core/executors/SubscriptionExecutor';
Expand All @@ -10,6 +9,7 @@ import {
ModelName,
SupportedModel,
} from '../../../src/core/models/SupportedModels';
import { DELTA_QUEUE_TIME_ADVANCE } from '../../support/constants';
import { CoreDelta } from '../../../src/core/models/CoreDeltas';
import { generateNewSubscription } from '../../support/helpers/core';
import 'jest-localstorage-mock';
Expand All @@ -23,18 +23,19 @@ describe('Executor tests', () => {
| jest.SpyInstance<void, [() => Promise<void>], any>
| jest.SpyInstance<void>;

beforeEach(() => {
beforeEach(async () => {
spyProcessOperationQueue = jest.spyOn(
ExecutorBase.prototype as any,
'_processOperationQueue',
);

jest.useFakeTimers();

test.stub(ModelCache.prototype, 'load', Promise.resolve({}));
test.stub(PropertiesExecutor.prototype, 'getOperationsFromCache', []);
test.stub(IdentityExecutor.prototype, 'getOperationsFromCache', []);
test.stub(SubscriptionExecutor.prototype, 'getOperationsFromCache', []);
TestEnvironment.initialize();
await TestEnvironment.initialize();
});

afterAll(async () => {
Expand All @@ -50,9 +51,6 @@ describe('Executor tests', () => {

/* F L U S H */
test('Subscriptions executor flushes deltas at end of `processDeltaQueue`', async () => {
const core = new CoreModule();
await core.init();

const processDeltaQueueSpy = jest.spyOn(
SubscriptionExecutor.prototype,
'processDeltaQueue',
Expand All @@ -62,21 +60,19 @@ describe('Executor tests', () => {
'_flushDeltas',
);

core.modelRepo?.broadcast({
const { modelRepo } = OneSignal.coreDirector.core;
modelRepo?.broadcast({
changeType: CoreChangeType.Add,
model: new OSModel(ModelName.Subscriptions, {}),
});

jest.runOnlyPendingTimers();
jest.advanceTimersByTime(DELTA_QUEUE_TIME_ADVANCE);

expect(processDeltaQueueSpy).toHaveBeenCalledTimes(1);
expect(flushDeltasSpy).toHaveBeenCalledTimes(1);
});

test('Identity executor flushes deltas at end of `processDeltaQueue`', async () => {
const core = new CoreModule();
await core.init();

const processDeltaQueueSpy = jest.spyOn(
IdentityExecutor.prototype,
'processDeltaQueue',
Expand All @@ -86,21 +82,19 @@ describe('Executor tests', () => {
'_flushDeltas',
);

core.modelRepo?.broadcast({
const { modelRepo } = OneSignal.coreDirector.core;
modelRepo?.broadcast({
changeType: CoreChangeType.Update,
model: new OSModel(ModelName.Identity, {}),
});

jest.runOnlyPendingTimers();
jest.advanceTimersByTime(DELTA_QUEUE_TIME_ADVANCE);

expect(processDeltaQueueSpy).toHaveBeenCalledTimes(1);
expect(flushDeltasSpy).toHaveBeenCalledTimes(1);
});

test('User Properties executor flushes deltas at end of `processDeltaQueue`', async () => {
const core = new CoreModule();
await core.init();

const processDeltaQueueSpy = jest.spyOn(
PropertiesExecutor.prototype,
'processDeltaQueue',
Expand All @@ -110,22 +104,20 @@ describe('Executor tests', () => {
'_flushDeltas',
);

core.modelRepo?.broadcast({
const { modelRepo } = OneSignal.coreDirector.core;
modelRepo?.broadcast({
changeType: CoreChangeType.Update,
model: new OSModel(ModelName.Properties, {}),
});

jest.runOnlyPendingTimers();
jest.advanceTimersByTime(DELTA_QUEUE_TIME_ADVANCE);

expect(processDeltaQueueSpy).toHaveBeenCalledTimes(1);
expect(flushDeltasSpy).toHaveBeenCalledTimes(1);
});

/* S U B S C R I P T I O N E X E C U T O R H E L P E R S */
test('separateDeltasByModelId returns correct delta matrix', async () => {
const core = new CoreModule();
await core.init();

const model = generateNewSubscription();
const separateDeltasByModelIdSpy = jest.spyOn(
SubscriptionExecutor.prototype as any,
Expand All @@ -137,7 +129,8 @@ describe('Executor tests', () => {
model: model as OSModel<SupportedModel>,
};

core.modelRepo?.broadcast(delta);
const { modelRepo } = OneSignal.coreDirector.core;
modelRepo?.broadcast(delta);

jest.runOnlyPendingTimers();

Expand All @@ -148,9 +141,6 @@ describe('Executor tests', () => {
});

test('separateDeltasByChangeType returns correct delta array map', async () => {
const core = new CoreModule();
await core.init();

const model = generateNewSubscription();
const separateDeltasByChangeTypeSpy = jest.spyOn(
SubscriptionExecutor.prototype as any,
Expand All @@ -161,7 +151,8 @@ describe('Executor tests', () => {
changeType: CoreChangeType.Add,
model: model as OSModel<SupportedModel>,
};
core.modelRepo?.broadcast(delta);
const { modelRepo } = OneSignal.coreDirector.core;
modelRepo?.broadcast(delta);

jest.runOnlyPendingTimers();

Expand Down
4 changes: 1 addition & 3 deletions src/core/operationRepo/OperationRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ export class OperationRepo {
);

setInterval(() => {
if (this._deltaQueue.length > 0) {
this._processDeltaQueue();
}
this._processDeltaQueue();
}, OperationRepo.DELTAS_BATCH_PROCESSING_TIME * 1_000);
}

Expand Down

0 comments on commit f680239

Please sign in to comment.