1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Convert getStartableStoppable to be always sync as it is never used in an async fashion

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-11-07 11:12:39 -05:00
parent b68d3a6e78
commit 0ef8c4d0c2
2 changed files with 41 additions and 229 deletions

View File

@ -2,21 +2,17 @@
* Copyright (c) OpenLens Authors. All rights reserved. * Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information. * Licensed under MIT License. See LICENSE in root directory for more information.
*/ */
import type { AsyncFnMock } from "@async-fn/jest"; import type { StartableStoppable } from "./get-startable-stoppable";
import asyncFn from "@async-fn/jest";
import { getStartableStoppable } from "./get-startable-stoppable"; import { getStartableStoppable } from "./get-startable-stoppable";
import { getPromiseStatus } from "../test-utils/get-promise-status";
import { flushPromises } from "../test-utils/flush-promises";
describe("getStartableStoppable", () => { describe("getStartableStoppable", () => {
let stopMock: AsyncFnMock<() => Promise<void>>; let stopMock: jest.MockedFunction<() => void>;
let startMock: AsyncFnMock<() => Promise<() => Promise<void>>>; let startMock: jest.MockedFunction<() => () => void>;
let actual: { stop: () => Promise<void>; start: () => Promise<void>; started: boolean }; let actual: StartableStoppable;
beforeEach(() => { beforeEach(() => {
stopMock = asyncFn(); stopMock = jest.fn();
startMock = asyncFn(); startMock = jest.fn().mockImplementation(() => stopMock);
actual = getStartableStoppable("some-id", startMock); actual = getStartableStoppable("some-id", startMock);
}); });
@ -29,7 +25,7 @@ describe("getStartableStoppable", () => {
}); });
it("when stopping before ever starting, throws", () => { it("when stopping before ever starting, throws", () => {
expect(actual.stop).rejects.toThrow("Tried to stop \"some-id\", but it has not started yet."); expect(() => actual.stop()).toThrow("Tried to stop \"some-id\", but it is already stopped.");
}); });
it("is not started", () => { it("is not started", () => {
@ -37,199 +33,30 @@ describe("getStartableStoppable", () => {
}); });
describe("when started", () => { describe("when started", () => {
let startPromise: Promise<void>;
beforeEach(() => { beforeEach(() => {
startPromise = actual.start(); actual.start();
}); });
it("starts starting", () => { it("calls start function", () => {
expect(startMock).toHaveBeenCalled(); expect(startMock).toHaveBeenCalled();
}); });
it("starting does not resolve yet", async () => { it("is started", () => {
const promiseStatus = await getPromiseStatus(startPromise); expect(actual.started).toBe(true);
expect(promiseStatus.fulfilled).toBe(false);
}); });
it("is not started yet", () => { describe("when stopped", () => {
expect(actual.started).toBe(false);
});
describe("when started again before the start has finished", () => {
let error: Error;
beforeEach(() => { beforeEach(() => {
startMock.mockClear(); actual.stop();
actual.start().catch((e) => { error = e; });
}); });
it("does not start starting again", () => { it("calls stop function", () => {
expect(startMock).not.toHaveBeenCalled(); expect(stopMock).toBeCalled();
}); });
it("throws", () => { it("is stopped", () => {
expect(error.message).toBe("Tried to start \"some-id\", but it is already being started.");
});
});
describe("when starting finishes", () => {
beforeEach(async () => {
await startMock.resolve(stopMock);
});
it("is started", () => {
expect(actual.started).toBe(true);
});
it("starting resolves", async () => {
const promiseStatus = await getPromiseStatus(startPromise);
expect(promiseStatus.fulfilled).toBe(true);
});
it("when started again, throws", () => {
expect(actual.start).rejects.toThrow("Tried to start \"some-id\", but it has already started.");
});
it("does not stop yet", () => {
expect(stopMock).not.toHaveBeenCalled();
});
describe("when stopped", () => {
let stopPromise: Promise<void>;
beforeEach(() => {
stopPromise = actual.stop();
});
it("starts stopping", () => {
expect(stopMock).toHaveBeenCalled();
});
it("stopping does not resolve yet", async () => {
const promiseStatus = await getPromiseStatus(stopPromise);
expect(promiseStatus.fulfilled).toBe(false);
});
it("is not stopped yet", () => {
expect(actual.started).toBe(true);
});
describe("when stopping finishes", () => {
beforeEach(async () => {
await stopMock.resolve();
});
it("is not started", () => {
expect(actual.started).toBe(false);
});
it("stopping resolves", async () => {
const promiseStatus = await getPromiseStatus(stopPromise);
expect(promiseStatus.fulfilled).toBe(true);
});
it("when stopped again, throws", () => {
expect(actual.stop).rejects.toThrow("Tried to stop \"some-id\", but it has already stopped.");
});
describe("when started again", () => {
beforeEach(
() => {
startMock.mockClear();
actual.start();
});
it("starts", () => {
expect(startMock).toHaveBeenCalled();
});
it("is not started yet", () => {
expect(actual.started).toBe(false);
});
describe("when starting finishes", () => {
beforeEach(async () => {
await startMock.resolve(stopMock);
});
it("is started", () => {
expect(actual.started).toBe(true);
});
it("when stopped again, starts stopping again", async () => {
stopMock.mockClear();
actual.stop();
await flushPromises();
expect(stopMock).toHaveBeenCalled();
});
});
});
});
});
});
describe("when stopped before starting finishes", () => {
let stopPromise: Promise<void>;
beforeEach(() => {
stopPromise = actual.stop();
});
it("does not resolve yet", async () => {
const promiseStatus = await getPromiseStatus(stopPromise);
expect(promiseStatus.fulfilled).toBe(false);
});
it("is not started yet", () => {
expect(actual.started).toBe(false); expect(actual.started).toBe(false);
}); });
describe("when starting finishes", () => {
beforeEach(async () => {
await startMock.resolve(stopMock);
});
it("starts stopping", () => {
expect(stopMock).toHaveBeenCalled();
});
it("is not stopped yet", () => {
expect(actual.started).toBe(true);
});
it("does not resolve yet", async () => {
const promiseStatus = await getPromiseStatus(stopPromise);
expect(promiseStatus.fulfilled).toBe(false);
});
describe("when stopping finishes", () => {
beforeEach(async () => {
await stopMock.resolve();
});
it("is stopped", () => {
expect(actual.started).toBe(false);
});
it("resolves", async () => {
const promiseStatus = await getPromiseStatus(stopPromise);
expect(promiseStatus.fulfilled).toBe(true);
});
});
});
}); });
}); });
}); });

View File

@ -3,58 +3,43 @@
* Licensed under MIT License. See LICENSE in root directory for more information. * Licensed under MIT License. See LICENSE in root directory for more information.
*/ */
type Stopper = () => Promise<void> | void; export type Stopper = () => void;
type Starter = () => Promise<Stopper> | Stopper; export type Starter = () => Stopper;
export const getStartableStoppable = ( export interface StartableStoppable {
id: string, readonly started: boolean;
startAndGetStopCallback: Starter, start: () => void;
) => { stop: () => void;
}
type StartableStoppableState = "stopped" | "started" | "starting";
export function getStartableStoppable(id: string, startAndGetStopper: Starter): StartableStoppable {
let stop: Stopper; let stop: Stopper;
let stopped = false; let state: StartableStoppableState = "stopped";
let started = false;
let starting = false;
let startingPromise: Promise<Stopper> | Stopper;
return { return {
get started() { get started() {
return started; return state === "started";
}, },
start: async () => { start: () => {
if (starting) { if (state !== "stopped") {
throw new Error(`Tried to start "${id}", but it is already being started.`); throw new Error(`Tried to start "${id}", but it is already ${state}.`);
} }
starting = true; state = "starting";
stop = startAndGetStopper();
if (started) { state = "started";
throw new Error(`Tried to start "${id}", but it has already started.`);
}
startingPromise = startAndGetStopCallback();
stop = await startingPromise;
stopped = false;
started = true;
starting = false;
}, },
stop: async () => { stop: () => {
await startingPromise; if (state !== "started") {
throw new Error(`Tried to stop "${id}", but it is already ${state}.`);
if (stopped) {
throw new Error(`Tried to stop "${id}", but it has already stopped.`);
} }
if (!started) { stop();
throw new Error(`Tried to stop "${id}", but it has not started yet.`); state = "stopped";
}
await stop();
started = false;
stopped = true;
}, },
}; };
}; }