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:
parent
b51490ecfd
commit
f7a9643c11
@ -4,14 +4,15 @@
|
|||||||
*/
|
*/
|
||||||
import type { AsyncFnMock } from "@async-fn/jest";
|
import type { AsyncFnMock } from "@async-fn/jest";
|
||||||
import asyncFn 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 { getPromiseStatus } from "../test-utils/get-promise-status";
|
||||||
import { flushPromises } from "../test-utils/flush-promises";
|
import { flushPromises } from "../test-utils/flush-promises";
|
||||||
|
|
||||||
describe("getStartableStoppable", () => {
|
describe("getStartableStoppable", () => {
|
||||||
let stopMock: AsyncFnMock<() => Promise<void>>;
|
let stopMock: AsyncFnMock<() => Promise<void>>;
|
||||||
let startMock: AsyncFnMock<() => Promise<() => Promise<void>>>;
|
let startMock: AsyncFnMock<() => Promise<() => Promise<void>>>;
|
||||||
let actual: { stop: () => Promise<void>; start: () => Promise<void>; started: boolean };
|
let actual: StartableStoppable;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
stopMock = asyncFn();
|
stopMock = asyncFn();
|
||||||
@ -29,7 +30,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(async () => actual.stop()).rejects.toThrow("Tried to stop \"some-id\", but it is already stopped.");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("is not started", () => {
|
it("is not started", () => {
|
||||||
@ -71,7 +72,7 @@ describe("getStartableStoppable", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("throws", () => {
|
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", () => {
|
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", () => {
|
it("does not stop yet", () => {
|
||||||
@ -135,7 +136,7 @@ describe("getStartableStoppable", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("when stopped again, throws", () => {
|
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", () => {
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@ -3,58 +3,85 @@
|
|||||||
* 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 = () => Promise<void> | void;
|
||||||
type Starter = () => Promise<Stopper> | Stopper;
|
export type Starter = () => Promise<Stopper> | Stopper;
|
||||||
|
|
||||||
export const getStartableStoppable = (
|
export type SyncStopper = () => void;
|
||||||
id: string,
|
export type SyncStarter = () => SyncStopper;
|
||||||
startAndGetStopCallback: Starter,
|
|
||||||
) => {
|
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 stop: Stopper;
|
||||||
let stopped = false;
|
let state: StartableStoppableState = "stopped";
|
||||||
let started = false;
|
|
||||||
let starting = false;
|
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;
|
let startingPromise: Promise<Stopper> | Stopper;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
get started() {
|
get started() {
|
||||||
return started;
|
return state === "started";
|
||||||
},
|
},
|
||||||
|
|
||||||
start: async () => {
|
start: async () => {
|
||||||
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;
|
|
||||||
|
|
||||||
if (started) {
|
|
||||||
throw new Error(`Tried to start "${id}", but it has already started.`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
state = "starting";
|
||||||
startingPromise = startAndGetStopCallback();
|
startingPromise = startAndGetStopCallback();
|
||||||
stop = await startingPromise;
|
stop = await startingPromise;
|
||||||
|
state = "started";
|
||||||
stopped = false;
|
|
||||||
started = true;
|
|
||||||
starting = false;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
stop: async () => {
|
stop: async () => {
|
||||||
|
if (state === "stopped") {
|
||||||
|
throw new Error(`Tried to stop "${id}", but it is already ${state}.`);
|
||||||
|
}
|
||||||
|
|
||||||
await startingPromise;
|
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();
|
await stop();
|
||||||
|
state = "stopped";
|
||||||
started = false;
|
|
||||||
stopped = true;
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user