feat: Replace expiration feature with startAt and endAt

This commit is contained in:
Joachim Van Herwegen
2023-01-24 13:44:49 +01:00
parent 10980e90a3
commit caee563dd6
11 changed files with 54 additions and 17 deletions

View File

@@ -70,7 +70,7 @@ describe('A KeyValueSubscriptionStorage', (): void => {
});
it('deletes expired info.', async(): Promise<void> => {
info.expiration = 0;
info.endAt = 0;
await storage.add(info);
await expect(storage.get(info.id)).resolves.toBeUndefined();
expect(internalMap.size).toBe(0);

View File

@@ -71,6 +71,17 @@ describe('A ListeningActivityHandler', (): void => {
expect(logger.error).toHaveBeenCalledTimes(0);
});
it('does not emit an event on subscriptions if their start time has not been reached.', async(): Promise<void> => {
info.startAt = Date.now() + 100000;
emitter.emit('changed', topic, activity);
await flushPromises();
expect(notificationHandler.handleSafe).toHaveBeenCalledTimes(0);
expect(logger.error).toHaveBeenCalledTimes(0);
});
it('does not stop if one subscription causes an error.', async(): Promise<void> => {
storage.getAll.mockResolvedValue([ info.id, info.id ]);
notificationHandler.handleSafe.mockRejectedValueOnce(new Error('bad input'));

View File

@@ -110,25 +110,25 @@ describe('A NotificationSubscriber', (): void => {
await subscriber.handle({ operation, request, response });
expect(subscriptionType.subscribe).toHaveBeenLastCalledWith(expect.objectContaining({
expiration: Date.now() + (60 * 60 * 1000),
endAt: Date.now() + (60 * 60 * 1000),
}), { public: {}});
operation.body.data = guardedStreamFrom(JSON.stringify({
...subscriptionBody,
expiration: new Date(Date.now() + 99999999999999).toISOString(),
endAt: new Date(Date.now() + 99999999999999).toISOString(),
}));
await subscriber.handle({ operation, request, response });
expect(subscriptionType.subscribe).toHaveBeenLastCalledWith(expect.objectContaining({
expiration: Date.now() + (60 * 60 * 1000),
endAt: Date.now() + (60 * 60 * 1000),
}), { public: {}});
operation.body.data = guardedStreamFrom(JSON.stringify({
...subscriptionBody,
expiration: new Date(Date.now() + 5).toISOString(),
endAt: new Date(Date.now() + 5).toISOString(),
}));
await subscriber.handle({ operation, request, response });
expect(subscriptionType.subscribe).toHaveBeenLastCalledWith(expect.objectContaining({
expiration: Date.now() + 5,
endAt: Date.now() + 5,
}), { public: {}});
jest.useRealTimers();

View File

@@ -40,16 +40,29 @@ describe('A Subscription', (): void => {
await expect(SUBSCRIBE_SCHEMA.isValid(subscription)).resolves.toBe(true);
});
it('converts the expiration date to a number.', async(): Promise<void> => {
it('converts the start date to a number.', async(): Promise<void> => {
const date = '1988-03-09T14:48:00.000Z';
const ms = Date.parse(date);
const subscription: unknown = {
...validSubscription,
expiration: date,
startAt: date,
};
await expect(SUBSCRIBE_SCHEMA.validate(subscription)).resolves.toEqual(expect.objectContaining({
expiration: ms,
startAt: ms,
}));
});
it('converts the end date to a number.', async(): Promise<void> => {
const date = '1988-03-09T14:48:00.000Z';
const ms = Date.parse(date);
const subscription: unknown = {
...validSubscription,
endAt: date,
};
await expect(SUBSCRIBE_SCHEMA.validate(subscription)).resolves.toEqual(expect.objectContaining({
endAt: ms,
}));
});

View File

@@ -44,7 +44,8 @@ describe('A WebHookSubscription2021', (): void => {
topic: 'https://storage.example/resource',
target,
state: undefined,
expiration: undefined,
startAt: undefined,
endAt: undefined,
accept: undefined,
rate: undefined,
};

View File

@@ -22,7 +22,8 @@ describe('A WebSocketSubscription2021', (): void => {
type: 'WebSocketSubscription2021',
topic: 'https://storage.example/resource',
state: undefined,
expiration: undefined,
startAt: undefined,
endAt: undefined,
accept: undefined,
rate: undefined,
};