Skip to content

Commit

Permalink
additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dillonstreator committed Dec 22, 2023
1 parent 98d92bf commit 1f5b49d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
39 changes: 33 additions & 6 deletions src/processor.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { describe, it, expect, vi, afterEach } from "vitest";
import { Processor, TxOBEvent, processEvents } from "./processor";
import {
Processor,
TxOBEvent,
defaultBackoff,
processEvents,
} from "./processor";
import { sleep } from "./sleep";

const mockTxClient = {
Expand Down Expand Up @@ -95,7 +100,18 @@ describe("processEvents", () => {
handler_results: {},
errors: 0,
};
const events = [evt1, evt2, evt3];
// skip processed
const evt4: TxOBEvent<keyof typeof handlerMap> = {
type: "evtType1",
id: "4",
timestamp: now,
data: {},
correlation_id: "xyz123",
handler_results: {},
errors: 0,
processed_at: now,
};
const events = [evt1, evt2, evt3, evt4];
mockClient.getUnprocessedEvents.mockImplementation(() => events);
mockTxClient.getEventByIdForUpdateSkipLocked.mockImplementation((id) => {
if (id === evt3.id) return null;
Expand All @@ -114,7 +130,7 @@ describe("processEvents", () => {
expect(mockClient.getUnprocessedEvents).toHaveBeenCalledOnce();
expect(mockClient.getUnprocessedEvents).toHaveBeenCalledWith(opts);

expect(mockClient.transaction).toHaveBeenCalledTimes(2);
expect(mockClient.transaction).toHaveBeenCalledTimes(3);

expect(handlerMap.evtType1.handler1).toHaveBeenCalledOnce();
expect(handlerMap.evtType1.handler1).toHaveBeenCalledWith(evt1, {
Expand All @@ -127,7 +143,7 @@ describe("processEvents", () => {
expect(handlerMap.evtType1.handler3).not.toHaveBeenCalled();

expect(mockTxClient.getEventByIdForUpdateSkipLocked).toHaveBeenCalledTimes(
2,
3,
);

expect(opts.backoff).toHaveBeenCalledOnce();
Expand Down Expand Up @@ -247,6 +263,17 @@ describe("processEvents", () => {
});
});

describe("defaultBackoff", () => {
it("should calculate a backoff", () => {
const backoff = defaultBackoff(3);
const actual = backoff.getTime();
const expected = Date.now() + 1000 * 2 ** 3;
const diff = Math.abs(actual - expected);

expect(diff).lessThanOrEqual(1);
});
});

describe("Processor", () => {
it("should shutdown gracefully", async () => {
let calls = 0;
Expand Down Expand Up @@ -276,9 +303,9 @@ describe("Processor", () => {

const start = Date.now();
try {
await processor.stop({ timeoutMs: 10});
await processor.stop({ timeoutMs: 10 });
} catch (error) {
expect(error.message).toBe('shutdown timeout 10ms elapsed')
expect(error.message).toBe("shutdown timeout 10ms elapsed");
}
const diff = Date.now() - start;
expect(diff).toBeLessThan(50);
Expand Down
2 changes: 1 addition & 1 deletion src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export interface TxOBTransactionProcessorClient<TxOBEventType extends string> {
updateEvent(event: TxOBEvent<TxOBEventType>): Promise<void>;
}

const defaultBackoff = (errorCount: number): Date => {
export const defaultBackoff = (errorCount: number): Date => {
const baseDelayMs = 1000;
const maxDelayMs = 1000 * 60;
const backoffMs = Math.min(baseDelayMs * 2 ** errorCount, maxDelayMs);
Expand Down
4 changes: 2 additions & 2 deletions src/sleep.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const sleep = (ms: number) =>
new Promise<void>((r) => setTimeout(r, ms));
export const sleep = (ms: number) =>
new Promise<void>((r) => setTimeout(r, ms));

0 comments on commit 1f5b49d

Please sign in to comment.