forked from Shared/esg
Add health check and retry logic and circuit breaker
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
import { IdempotencyService } from '../../src/idempotency/idempotency.service';
|
||||
import { IdempotencyRecordStatus } from '../../src/idempotency/schemas/idempotency-record.schema';
|
||||
|
||||
class FakeRecordModel {
|
||||
private readonly records = new Map<string, any>();
|
||||
|
||||
findOne(filter: { scopeHash: string }) {
|
||||
return { exec: async () => this.records.get(filter.scopeHash) ?? null };
|
||||
}
|
||||
|
||||
async create(record: any) {
|
||||
if (this.records.has(record.scopeHash)) {
|
||||
throw Object.assign(new Error('duplicate'), { code: 11000 });
|
||||
}
|
||||
const saved = { ...record, _id: record.scopeHash };
|
||||
this.records.set(record.scopeHash, saved);
|
||||
return saved;
|
||||
}
|
||||
|
||||
updateOne(filter: { scopeHash: string; status: IdempotencyRecordStatus }, update: any) {
|
||||
return {
|
||||
exec: async () => {
|
||||
const record = this.records.get(filter.scopeHash);
|
||||
if (record?.status === filter.status) Object.assign(record, update.$set);
|
||||
return { acknowledged: true };
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
deleteOne(filter: { scopeHash: string; status: IdempotencyRecordStatus }) {
|
||||
return {
|
||||
exec: async () => {
|
||||
const record = this.records.get(filter.scopeHash);
|
||||
if (record?.status === filter.status) this.records.delete(filter.scopeHash);
|
||||
return { acknowledged: true };
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
describe('IdempotencyService', () => {
|
||||
it('replays completed responses and rejects body reuse', async () => {
|
||||
const service = new IdempotencyService(new FakeRecordModel() as never);
|
||||
const input = {
|
||||
key: 'request-1',
|
||||
callerId: 'user-1',
|
||||
method: 'POST',
|
||||
path: '/inquiry/person',
|
||||
body: { birthDate: '1370-01-01', nationalCode: '0012345678' },
|
||||
};
|
||||
|
||||
const reservation = await service.begin(input);
|
||||
const response = { success: true, trackingCode: 'INQ-1' };
|
||||
await service.complete(reservation.scopeHash, response, 201);
|
||||
|
||||
const replay = await service.begin(input);
|
||||
expect(replay.replay).toMatchObject({ responseStatus: 201, responseBody: response });
|
||||
|
||||
await expect(service.begin({ ...input, body: { different: true } })).rejects.toMatchObject({
|
||||
normalizedError: { code: 'IDEMPOTENCY_KEY_REUSED' },
|
||||
});
|
||||
});
|
||||
|
||||
it('rejects a duplicate while the original request is in progress', async () => {
|
||||
const service = new IdempotencyService(new FakeRecordModel() as never);
|
||||
const input = {
|
||||
key: 'request-2',
|
||||
callerId: 'user-1',
|
||||
method: 'POST',
|
||||
path: '/inquiry/person',
|
||||
body: { nationalCode: '0012345678' },
|
||||
};
|
||||
|
||||
await service.begin(input);
|
||||
await expect(service.begin(input)).rejects.toMatchObject({
|
||||
normalizedError: { code: 'IDEMPOTENCY_REQUEST_IN_PROGRESS' },
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user