Creating semaphore adapters
Implementing your custom ISemaphoreAdapter
In order to create an adapter you need to implement the ISemaphoreAdapter contract.
Testing your custom ISemaphoreAdapter
We provide a complete test suite to test your semaphore adapter implementation. Simply use the semaphoreAdapterTestSuite function:
- Preconfigured Vitest test cases
- Common edge case coverage
Usage example:
// filename: MySemaphoreAdapter.test.ts
import { beforeEach, describe, expect, test } from "vitest";
import { semaphoreAdapterTestSuite } from "@daiso-tech/core/semaphore/test-utilities";
import { MemorySemaphoreAdapter } from "./MemorySemaphoreAdapter.js";
describe("class: MySemaphoreAdapter", () => {
semaphoreAdapterTestSuite({
createAdapter: () => new MemorySemaphoreAdapter(),
test,
beforeEach,
expect,
describe,
});
});
Implementing your custom IDatabaseSemaphoreAdapter
We provide an additional contract IDatabaseSemaphoreAdapter for building custom semaphore adapters tailored to databases.
Testing your custom IDatabaseSemaphoreAdapter
We provide a complete test suite to test your database semaphore adapter implementation. Simply use the databaseSemaphoreAdapterTestSuite function:
- Preconfigured Vitest test cases
- Common edge case coverage
Usage example:
import { beforeEach, describe, expect, test } from "vitest";
import { databaseSemaphoreAdapterTestSuite } from "@daiso-tech/core/semaphore/test-utilities";
import { MyDatabaseSemaphoreAdapter } from "./MyDatabaseSemaphoreAdapter.js";
describe("class: MyDatabaseSemaphoreAdapter", () => {
databaseSemaphoreAdapterTestSuite({
createAdapter: async () => {
return new MyDatabaseSemaphoreAdapter(),
},
test,
beforeEach,
expect,
describe,
});
});
Implementing your custom ISemaphoreFactory class
In some cases, you may need to implement a custom SemaphoreFactory class to optimize performance for your specific technology stack. You can then directly implement the ISemaphoreFactory contract.
Testing your custom ISemaphoreFactory class
We provide a complete test suite to verify your custom event bus class implementation. Simply use the semaphoreFactoryTestSuite function:
- Preconfigured Vitest test cases
- Standardized event bus behavior validation
- Common edge case coverage
Usage example:
// filename: MySemaphoreFactory.test.ts
import { beforeEach, describe, expect, test } from "vitest";
import { semaphoreFactoryTestSuite } from "@daiso-tech/core/semaphore/test-utilities";
import { MySemaphoreFactory } from "./MySemaphoreFactory.js";
describe("class: MySemaphoreFactory", () => {
semaphoreFactoryTestSuite({
createSemaphoreFactory: () => new MySemaphoreFactory(),
test,
beforeEach,
expect,
describe,
});
});
Further information
For further information refer to @daiso-tech/core/semaphore API docs.