Skip to content

Commit

Permalink
Use setTimeout to schedule monitoring steps
Browse files Browse the repository at this point in the history
  • Loading branch information
Liza K committed Dec 8, 2020
1 parent ffabaa9 commit 63c7d6a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,27 @@ describe('BackgroundSessionService', () => {
service.stop();
});

it('schedules the next iteration', async () => {
const findSpy = jest.fn().mockResolvedValue({ saved_objects: [] });
createMockInternalSavedObjectClient(findSpy);

const mockIdMapping = createMockIdMapping([[MOCK_KEY_HASH, MOCK_ASYNC_ID]], moment());

Object.defineProperty(service, 'sessionSearchMap', {
get: () => mockIdMapping,
});

jest.advanceTimersByTime(INMEM_TRACKING_INTERVAL);
expect(findSpy).toHaveBeenCalledTimes(1);
await flushPromises();

jest.advanceTimersByTime(INMEM_TRACKING_INTERVAL);
expect(findSpy).toHaveBeenCalledTimes(2);
});

it('should delete expired IDs', async () => {
const bulkGetSpy = jest.fn().mockResolvedValueOnce({ saved_objects: [] });
createMockInternalSavedObjectClient(bulkGetSpy);
const findSpy = jest.fn().mockResolvedValueOnce({ saved_objects: [] });
createMockInternalSavedObjectClient(findSpy);

const mockIdMapping = createMockIdMapping(
[[MOCK_KEY_HASH, MOCK_ASYNC_ID]],
Expand All @@ -432,13 +450,13 @@ describe('BackgroundSessionService', () => {
// Get setInterval to fire
jest.advanceTimersByTime(INMEM_TRACKING_INTERVAL);

expect(bulkGetSpy).not.toHaveBeenCalled();
expect(findSpy).not.toHaveBeenCalled();
expect(deleteSpy).toHaveBeenCalledTimes(1);
});

it('should delete IDs that passed max retries', async () => {
const bulkGetSpy = jest.fn().mockResolvedValueOnce({ saved_objects: [] });
createMockInternalSavedObjectClient(bulkGetSpy);
const findSpy = jest.fn().mockResolvedValueOnce({ saved_objects: [] });
createMockInternalSavedObjectClient(findSpy);

const mockIdMapping = createMockIdMapping(
[[MOCK_KEY_HASH, MOCK_ASYNC_ID]],
Expand All @@ -454,16 +472,16 @@ describe('BackgroundSessionService', () => {
// Get setInterval to fire
jest.advanceTimersByTime(INMEM_TRACKING_INTERVAL);

expect(bulkGetSpy).not.toHaveBeenCalled();
expect(findSpy).not.toHaveBeenCalled();
expect(deleteSpy).toHaveBeenCalledTimes(1);
});

it('should not fetch when no IDs are mapped', async () => {
const bulkGetSpy = jest.fn().mockResolvedValueOnce({ saved_objects: [] });
createMockInternalSavedObjectClient(bulkGetSpy);
const findSpy = jest.fn().mockResolvedValueOnce({ saved_objects: [] });
createMockInternalSavedObjectClient(findSpy);

jest.advanceTimersByTime(INMEM_TRACKING_INTERVAL);
expect(bulkGetSpy).not.toHaveBeenCalled();
expect(findSpy).not.toHaveBeenCalled();
});

it('should try to fetch saved objects if some ids are mapped', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class BackgroundSessionService implements ISessionService {
*/
private sessionSearchMap = new Map<string, SessionInfo>();
private internalSavedObjectsClient!: SavedObjectsClientContract;
private monitorInterval!: NodeJS.Timeout;
private monitorTimer!: NodeJS.Timeout;

constructor(private readonly logger: Logger) {}

Expand All @@ -68,7 +68,7 @@ export class BackgroundSessionService implements ISessionService {

public stop() {
this.sessionSearchMap.clear();
clearInterval(this.monitorInterval);
clearTimeout(this.monitorTimer);
}

private setupMonitoring = async (core: CoreStart, config$: Observable<ConfigSchema>) => {
Expand All @@ -77,7 +77,7 @@ export class BackgroundSessionService implements ISessionService {
this.logger.debug(`setupMonitoring | Enabling monitoring`);
const internalRepo = core.savedObjects.createInternalRepository([BACKGROUND_SESSION_TYPE]);
this.internalSavedObjectsClient = new SavedObjectsClient(internalRepo);
this.monitorInterval = setInterval(this.monitorMappedIds.bind(this), INMEM_TRACKING_INTERVAL);
this.monitorTimer = setTimeout(this.monitorMappedIds.bind(this), INMEM_TRACKING_INTERVAL);
}
};

Expand Down Expand Up @@ -121,7 +121,7 @@ export class BackgroundSessionService implements ISessionService {
});
};

public async monitorMappedIds() {
private async monitorMappedIds() {
try {
this.clearSessions();

Expand Down Expand Up @@ -150,8 +150,11 @@ export class BackgroundSessionService implements ISessionService {
}
});
} catch (e) {
this.logger.error(`monitorMappedIds | Error fetching sessions. ${e}`);
this.logger.error(`monitorMappedIds | Error while updating sessions. ${e}`);
}

// Schedule the next iteration
this.monitorTimer = setTimeout(this.monitorMappedIds.bind(this), INMEM_TRACKING_INTERVAL);
}

private async updateAllSavedObjects(
Expand Down

0 comments on commit 63c7d6a

Please sign in to comment.