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

Add getSyncStartableStoppable and tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-11-02 16:10:16 -04:00
parent b51490ecfd
commit f7a9643c11
2 changed files with 125 additions and 41 deletions

View File

@ -4,14 +4,15 @@
*/
import type { AsyncFnMock } from "@async-fn/jest";
import asyncFn from "@async-fn/jest";
import { getStartableStoppable } from "./get-startable-stoppable";
import type { StartableStoppable, SyncStartableStoppable } from "./get-startable-stoppable";
import { getSyncStartableStoppable, getStartableStoppable } from "./get-startable-stoppable";
import { getPromiseStatus } from "../test-utils/get-promise-status";
import { flushPromises } from "../test-utils/flush-promises";
describe("getStartableStoppable", () => {
let stopMock: AsyncFnMock<() => Promise<void>>;
let startMock: AsyncFnMock<() => Promise<() => Promise<void>>>;
let actual: { stop: () => Promise<void>; start: () => Promise<void>; started: boolean };
let actual: StartableStoppable;
beforeEach(() => {
stopMock = asyncFn();
@ -29,7 +30,7 @@ describe("getStartableStoppable", () => {
});
it("when stopping before ever starting, throws", () => {
expect(actual.stop).rejects.toThrow("Tried to stop \"some-id\", but it has not started yet.");
expect(async () => actual.stop()).rejects.toThrow("Tried to stop \"some-id\", but it is already stopped.");
});
it("is not started", () => {
@ -71,7 +72,7 @@ describe("getStartableStoppable", () => {
});
it("throws", () => {
expect(error.message).toBe("Tried to start \"some-id\", but it is already being started.");
expect(error.message).toBe("Tried to start \"some-id\", but it is already starting.");
});
});
@ -91,7 +92,7 @@ describe("getStartableStoppable", () => {
});
it("when started again, throws", () => {
expect(actual.start).rejects.toThrow("Tried to start \"some-id\", but it has already started.");
expect(actual.start).rejects.toThrow("Tried to start \"some-id\", but it is already started.");
});
it("does not stop yet", () => {
@ -135,7 +136,7 @@ describe("getStartableStoppable", () => {
});
it("when stopped again, throws", () => {
expect(actual.stop).rejects.toThrow("Tried to stop \"some-id\", but it has already stopped.");
expect(actual.stop).rejects.toThrow("Tried to stop \"some-id\", but it is already stopped.");
});
describe("when started again", () => {
@ -233,3 +234,59 @@ describe("getStartableStoppable", () => {
});
});
});
describe("getSyncStartableStoppable", () => {
let stopMock: jest.MockedFunction<() => void>;
let startMock: jest.MockedFunction<() => () => void>;
let actual: SyncStartableStoppable;
beforeEach(() => {
stopMock = jest.fn();
startMock = jest.fn().mockImplementation(() => stopMock);
actual = getSyncStartableStoppable("some-id", startMock);
});
it("does not start yet", () => {
expect(startMock).not.toHaveBeenCalled();
});
it("does not stop yet", () => {
expect(stopMock).not.toHaveBeenCalled();
});
it("when stopping before ever starting, throws", () => {
expect(() => actual.stop()).toThrow("Tried to stop \"some-id\", but it is already stopped.");
});
it("is not started", () => {
expect(actual.started).toBe(false);
});
describe("when started", () => {
beforeEach(() => {
actual.start();
});
it("calls start function", () => {
expect(startMock).toHaveBeenCalled();
});
it("is started", () => {
expect(actual.started).toBe(true);
});
describe("when stopped", () => {
beforeEach(() => {
actual.stop();
});
it("calls stop function", () => {
expect(stopMock).toBeCalled();
});
it("is stopped", () => {
expect(actual.started).toBe(false);
});
});
});
});

View File

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