Skip to content

Commit

Permalink
improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dillonstreator committed Dec 17, 2023
1 parent 35f1da1 commit 882e8a1
Showing 1 changed file with 109 additions and 8 deletions.
117 changes: 109 additions & 8 deletions src/processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ describe('processEvents', () => {
handler2: vi.fn(() => Promise.reject(err)),
handler3: vi.fn(() => Promise.resolve()),
},
evtType2: {
handler1: vi.fn(() => Promise.resolve()),
},
};
const evt1: TxOBEvent<keyof typeof handlerMap> = {
type: 'evtType1',
Expand All @@ -68,13 +71,34 @@ describe('processEvents', () => {
processed_at: now,
},
},
errors: opts.maxErrors - 1,
};
// should skip evt2 due to max errors
const evt2: TxOBEvent<keyof typeof handlerMap> = {
type: 'evtType1',
id: '2',
timestamp: now,
data: {},
correlation_id: 'abc456',
handler_results: {},
errors: opts.maxErrors,
};
const evt3: TxOBEvent<keyof typeof handlerMap> = {
type: 'evtType1',
id: '3',
timestamp: now,
data: {},
correlation_id: 'abc789',
handler_results: {},
errors: 0,
};
const events = [evt1];
const events = [evt1, evt2, evt3];
mockClient.getUnprocessedEvents.mockImplementation(() => events);
mockClient.getEventByIdForUpdateSkipLocked.mockImplementation((id) =>
events.find((e) => e.id === id)
);
mockClient.getEventByIdForUpdateSkipLocked.mockImplementation((id) => {
if (id === evt3.id) return null;

return events.find((e) => e.id === id);
});
let updateEventCalls = 0;
mockClient.updateEvent.mockImplementation(() => {
updateEventCalls++;
Expand All @@ -99,17 +123,17 @@ describe('processEvents', () => {

expect(
mockClient.getEventByIdForUpdateSkipLocked
).toHaveBeenCalledOnce();
).toHaveBeenCalledTimes(2);

expect(opts.backoff).toHaveBeenCalledOnce();
expect(opts.backoff).toHaveBeenCalledWith(1); // evt.errors + 1
expect(opts.backoff).toHaveBeenCalledWith(5); // evt.errors + 1

expect(mockClient.updateEvent).toHaveBeenCalledTimes(2);
expect(mockClient.updateEvent).toHaveBeenCalledWith({
backoff_until: undefined,
backoff_until: null,
correlation_id: 'abc123',
data: {},
errors: 1,
errors: 5,
handler_results: {
handler1: {
errors: [
Expand Down Expand Up @@ -137,4 +161,81 @@ describe('processEvents', () => {
type: 'evtType1',
});
});

it('process event if no handler errors encountered', async () => {
const opts = {
maxErrors: 5,
backoff: vi.fn(),
};
const err = new Error('some error');
const handlerMap = {
evtType1: {
handler1: vi.fn(() => Promise.resolve()),
handler2: vi.fn(() => Promise.resolve()),
},
};
const evt1: TxOBEvent<keyof typeof handlerMap> = {
type: 'evtType1',
id: '1',
timestamp: now,
data: {},
correlation_id: 'abc123',
handler_results: {
handler1: {
errors: [{ error: err.message, timestamp: now }],
},
handler2: {
processed_at: now,
},
},
errors: 1,
};
const events = [evt1];
mockClient.getUnprocessedEvents.mockImplementation(() => events);
mockClient.getEventByIdForUpdateSkipLocked.mockImplementation((id) => {
return events.find((e) => e.id === id);
});
mockClient.updateEvent.mockImplementation(() => {
return Promise.resolve();
});

await processEvents(mockClient, handlerMap, opts);

expect(mockClient.getUnprocessedEvents).toHaveBeenCalledOnce();
expect(mockClient.getUnprocessedEvents).toHaveBeenCalledWith(opts);

expect(handlerMap.evtType1.handler1).toHaveBeenCalledOnce();
expect(handlerMap.evtType1.handler1).toHaveBeenCalledWith(evt1, {
signal: undefined,
});
expect(
mockClient.getEventByIdForUpdateSkipLocked
).toHaveBeenCalledTimes(1);

expect(mockClient.updateEvent).toHaveBeenCalledTimes(1);
expect(mockClient.updateEvent).toHaveBeenCalledWith({
backoff_until: null,
correlation_id: 'abc123',
data: {},
errors: 1,
handler_results: {
handler1: {
errors: [
{
error: err.message,
timestamp: now,
},
],
processed_at: now,
},
handler2: {
processed_at: now,
},
},
id: '1',
timestamp: now,
type: 'evtType1',
processed_at: now,
});
});
});

0 comments on commit 882e8a1

Please sign in to comment.