Skip to content
Open
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
Expand Up @@ -180,46 +180,105 @@ describe('Session service', () => {

expect(abort).toBeCalledTimes(3);
});
});

describe('Keeping searches alive', () => {
let dateNowSpy: jest.SpyInstance;
let now = Date.now();
const advanceTimersBy = (by: number) => {
now = now + by;
jest.advanceTimersByTime(by);
};
beforeEach(() => {
dateNowSpy = jest.spyOn(Date, 'now').mockImplementation(() => now);
now = Date.now();
jest.useFakeTimers();
});
afterEach(() => {
dateNowSpy.mockRestore();
jest.useRealTimers();
describe('Keeping searches alive', () => {
let dateNowSpy: jest.SpyInstance;
let now = Date.now();
const advanceTimersBy = (by: number) => {
now = now + by;
jest.advanceTimersByTime(by);
};
beforeEach(() => {
dateNowSpy = jest.spyOn(Date, 'now').mockImplementation(() => now);
now = Date.now();

sessionService.enableStorage({
getName: async () => 'Name',
getLocatorData: async () => ({
id: 'id',
initialState: {},
restoreState: {},
}),
});

it('Polls all completed searches to keep them alive', async () => {
const abort = jest.fn();
const poll = jest.fn(() => Promise.resolve());
jest.useFakeTimers();
});
afterEach(() => {
dateNowSpy.mockRestore();
jest.useRealTimers();
});

sessionService.enableStorage({
getName: async () => 'Name',
getLocatorData: async () => ({
id: 'id',
initialState: {},
restoreState: {},
}),
describe('when there is only 1 search', () => {
describe('when it finishes', () => {
it('should NOT poll the search', () => {
const abort = jest.fn();
const poll = jest.fn(() => Promise.resolve());

sessionService.start();

const searchTracker = sessionService.trackSearch({ abort, poll });
searchTracker.complete();

expect(poll).toHaveBeenCalledTimes(0);
advanceTimersBy(35_000);
expect(poll).toHaveBeenCalledTimes(0);
});
sessionService.start();
});
});

const searchTracker = sessionService.trackSearch({ abort, poll });
searchTracker.complete();
describe('when there are multiple searches', () => {
describe('when not all of them are is finished', () => {
it('should poll the finished searches', () => {
const search1 = {
poll: jest.fn(() => Promise.resolve()),
abort: jest.fn(),
};
const search2 = {
poll: jest.fn(() => Promise.resolve()),
abort: jest.fn(),
};

expect(poll).toHaveBeenCalledTimes(0);
sessionService.start();

advanceTimersBy(30000);
const searchTracker1 = sessionService.trackSearch(search1);
sessionService.trackSearch(search2);

expect(poll).toHaveBeenCalledTimes(1);
searchTracker1.complete();

expect(search1.poll).toHaveBeenCalledTimes(0);
expect(search2.poll).toHaveBeenCalledTimes(0);
advanceTimersBy(35_000);
expect(search1.poll).toHaveBeenCalledTimes(1);
expect(search2.poll).toHaveBeenCalledTimes(0);
});
});

describe('when all of them are is finished', () => {
it('should not poll anything', () => {
const search1 = {
poll: jest.fn(() => Promise.resolve()),
abort: jest.fn(),
};
const search2 = {
poll: jest.fn(() => Promise.resolve()),
abort: jest.fn(),
};

sessionService.start();

const searchTracker1 = sessionService.trackSearch(search1);
const searchTracker2 = sessionService.trackSearch(search2);

searchTracker1.complete();
searchTracker2.complete();

expect(search1.poll).toHaveBeenCalledTimes(0);
expect(search2.poll).toHaveBeenCalledTimes(0);
advanceTimersBy(35_000);
expect(search1.poll).toHaveBeenCalledTimes(0);
expect(search2.poll).toHaveBeenCalledTimes(0);
});
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@

import type { PublicContract, SerializableRecord } from '@kbn/utility-types';
import {
EMPTY,
distinctUntilChanged,
filter,
from,
interval,
map,
mapTo,
mergeMap,
repeat,
startWith,
switchMap,
take,
takeUntil,
tap,
} from 'rxjs';
import type { Observable } from 'rxjs';
import { BehaviorSubject, combineLatest, EMPTY, from, merge, of, Subscription, timer } from 'rxjs';
import { BehaviorSubject, combineLatest, merge, of, Subscription, timer } from 'rxjs';
import type {
PluginInitializerContext,
StartServicesAccessor,
Expand Down Expand Up @@ -50,7 +53,7 @@ import type { ISearchSessionEBTManager } from './ebt_manager';
* Polling interval for keeping completed searches alive
* until the user saves the session
*/
const KEEP_ALIVE_COMPLETED_SEARCHES_INTERVAL = 30000;
const KEEP_ALIVE_COMPLETED_SEARCHES_INTERVAL = 30_000;

/**
* To prevent the session ids map from growing indefinitely we can use an LRU cache - we will limit it to 30 sessions for
Expand Down Expand Up @@ -306,8 +309,20 @@ export class SessionService {
if (!this.hasAccess()) return EMPTY; // don't need to keep searches alive if the user can't save session
if (!this.isSessionStorageReady()) return EMPTY; // don't need to keep searches alive if app doesn't allow saving session

const finishedStates = [
SearchSessionState.Completed,
SearchSessionState.BackgroundCompleted,
SearchSessionState.Restored,
SearchSessionState.Canceled,
];

const stopOnFinishedState$ = this.state$.pipe(
filter((state) => finishedStates.includes(state)),
take(1)
);

const schedulePollSearches = () => {
return timer(KEEP_ALIVE_COMPLETED_SEARCHES_INTERVAL).pipe(
return interval(KEEP_ALIVE_COMPLETED_SEARCHES_INTERVAL).pipe(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've changed it from timer + repeat to just use interval

mergeMap(() => {
const searchesToKeepAlive = this.state.get().trackedSearches.filter(
(s) =>
Expand All @@ -333,8 +348,8 @@ export class SessionService {
)
);
}),
repeat(),
takeUntil(this.disableSaveAfterSearchesExpire$.pipe(filter((disable) => disable)))
takeUntil(this.disableSaveAfterSearchesExpire$.pipe(filter((disable) => disable))),
takeUntil(stopOnFinishedState$)
Comment on lines +351 to +352
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the main change, now on top of the existing behavior we stop when the search session is in a completed status

);
};

Expand Down