Skip to main content

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:

The suite provides preconfigured Vitest test cases with common edge case coverage and standardized semaphore adapter contract conformance testing.

Usage example:

./samples/semaphore-adapter-test-suite.ts
import { beforeEach, describe, expect, test } from "vitest";
import { semaphoreAdapterTestSuite } from "eridu-tech/semaphore/test-utilities";
import { MemorySemaphoreAdapter } from "eridu-tech/semaphore/memory-semaphore-adapter";

describe("class: MySemaphoreAdapter", () => {
semaphoreAdapterTestSuite({
createAdapter: () => new MemorySemaphoreAdapter(),
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:

The suite provides preconfigured Vitest test cases with common edge case coverage and standardized semaphore factory contract conformance testing.

Usage example:

./samples/semaphore-factory-test-suite.ts
import { beforeEach, describe, expect, test } from "vitest";
import { semaphoreFactoryTestSuite } from "eridu-tech/semaphore/test-utilities";
import { SemaphoreFactory } from "eridu-tech/semaphore";
import { MemorySemaphoreAdapter } from "eridu-tech/semaphore/memory-semaphore-adapter";
import { Serde } from "eridu-tech/serde";
import { SuperJsonSerdeAdapter } from "eridu-tech/serde/super-json-serde-adapter";

describe("class: MySemaphoreFactory", () => {
semaphoreFactoryTestSuite({
createSemaphoreFactory: () => ({
semaphoreFactory: new SemaphoreFactory({
adapter: new MemorySemaphoreAdapter(),
}),
serde: new Serde(new SuperJsonSerdeAdapter()),
}),
test,
beforeEach,
expect,
describe,
});
});

Further information​

For further information refer to eridu-tech/semaphore API docs.