mirror of
https://github.com/bitwarden/browser
synced 2025-12-22 19:23:52 +00:00
[PM-8748] Ensure Reasonable ConfigService Emission (#10452)
* Add Slow Emission Guard * Remove Jest Timer Call * Do Slow Emission Guard For All Environments * Update Comment
This commit is contained in:
@@ -3,8 +3,8 @@
|
||||
* @jest-environment ../../libs/shared/test.environment.ts
|
||||
*/
|
||||
|
||||
import { mock } from "jest-mock-extended";
|
||||
import { Subject, firstValueFrom, of } from "rxjs";
|
||||
import { matches, mock } from "jest-mock-extended";
|
||||
import { BehaviorSubject, Subject, bufferCount, firstValueFrom, of } from "rxjs";
|
||||
|
||||
import {
|
||||
FakeGlobalState,
|
||||
@@ -35,6 +35,7 @@ import {
|
||||
RETRIEVAL_INTERVAL,
|
||||
GLOBAL_SERVER_CONFIGURATIONS,
|
||||
USER_SERVER_CONFIG,
|
||||
SLOW_EMISSION_GUARD,
|
||||
} from "./default-config.service";
|
||||
|
||||
describe("ConfigService", () => {
|
||||
@@ -65,12 +66,14 @@ describe("ConfigService", () => {
|
||||
describe.each([null, userId])("active user: %s", (activeUserId) => {
|
||||
let sut: DefaultConfigService;
|
||||
|
||||
const environmentSubject = new BehaviorSubject(environmentFactory(activeApiUrl));
|
||||
|
||||
beforeAll(async () => {
|
||||
await accountService.switchAccount(activeUserId);
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
environmentService.environment$ = of(environmentFactory(activeApiUrl));
|
||||
environmentService.environment$ = environmentSubject;
|
||||
sut = new DefaultConfigService(
|
||||
configApiService,
|
||||
environmentService,
|
||||
@@ -129,7 +132,8 @@ describe("ConfigService", () => {
|
||||
await firstValueFrom(sut.serverConfig$);
|
||||
|
||||
expect(logService.error).toHaveBeenCalledWith(
|
||||
`Unable to fetch ServerConfig from ${activeApiUrl}: Unable to fetch`,
|
||||
`Unable to fetch ServerConfig from ${activeApiUrl}`,
|
||||
matches<Error>((e) => e.message === "Unable to fetch"),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -138,6 +142,10 @@ describe("ConfigService", () => {
|
||||
const response = serverConfigResponseFactory();
|
||||
const newConfig = new ServerConfig(new ServerConfigData(response));
|
||||
|
||||
beforeEach(() => {
|
||||
configApiService.get.mockResolvedValue(response);
|
||||
});
|
||||
|
||||
it("should be a new config", async () => {
|
||||
expect(newConfig).not.toEqual(activeUserId ? userStored : globalStored[activeApiUrl]);
|
||||
});
|
||||
@@ -149,8 +157,6 @@ describe("ConfigService", () => {
|
||||
});
|
||||
|
||||
it("returns the updated config", async () => {
|
||||
configApiService.get.mockResolvedValue(response);
|
||||
|
||||
const actual = await firstValueFrom(sut.serverConfig$);
|
||||
|
||||
// This is the time the response is converted to a config
|
||||
@@ -270,6 +276,51 @@ describe("ConfigService", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("slow configuration", () => {
|
||||
const environmentSubject = new BehaviorSubject<Environment>(null);
|
||||
|
||||
let sut: DefaultConfigService = null;
|
||||
|
||||
beforeEach(async () => {
|
||||
const config = serverConfigFactory("existing-data", tooOld);
|
||||
environmentService.environment$ = environmentSubject;
|
||||
|
||||
globalState.stateSubject.next({ [apiUrl(0)]: config });
|
||||
userState.stateSubject.next([userId, config]);
|
||||
|
||||
configApiService.get.mockImplementation(() => {
|
||||
return new Promise<ServerConfigResponse>((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve(serverConfigResponseFactory("slow-response"));
|
||||
}, SLOW_EMISSION_GUARD + 20);
|
||||
});
|
||||
});
|
||||
|
||||
sut = new DefaultConfigService(
|
||||
configApiService,
|
||||
environmentService,
|
||||
logService,
|
||||
stateProvider,
|
||||
authService,
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
it("emits old configuration when the http call takes a long time", async () => {
|
||||
environmentSubject.next(environmentFactory(apiUrl(0)));
|
||||
|
||||
const configs = await firstValueFrom(sut.serverConfig$.pipe(bufferCount(2)));
|
||||
|
||||
await jest.runOnlyPendingTimersAsync();
|
||||
|
||||
expect(configs[0].gitHash).toBe("existing-data");
|
||||
expect(configs[1].gitHash).toBe("slow-response");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function apiUrl(count: number) {
|
||||
@@ -305,8 +356,9 @@ function serverConfigResponseFactory(hash?: string) {
|
||||
});
|
||||
}
|
||||
|
||||
function environmentFactory(apiUrl: string) {
|
||||
function environmentFactory(apiUrl: string, isCloud: boolean = true) {
|
||||
return {
|
||||
getApiUrl: () => apiUrl,
|
||||
isCloud: () => isCloud,
|
||||
} as Environment;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user