Challenge: Save conversationIds and start queues

This commit is contained in:
Scott Nonnenberg 2022-03-21 14:19:37 -07:00 committed by GitHub
parent c369e44d8e
commit bddd55d574
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 316 additions and 476 deletions

View file

@ -7,27 +7,21 @@ import { assert } from 'chai';
import { noop } from 'lodash';
import * as sinon from 'sinon';
import type { MinimalMessage } from '../challenge';
import { ChallengeHandler } from '../challenge';
type CreateMessageOptions = {
readonly sentAt?: number;
readonly retryAfter?: number;
readonly isNormalBubble?: boolean;
};
import { STORAGE_KEY, ChallengeHandler } from '../challenge';
import type { RegisteredChallengeType } from '../challenge';
import { DAY, SECOND } from '../util/durations';
type CreateHandlerOptions = {
readonly autoSolve?: boolean;
readonly challengeError?: Error;
readonly expireAfter?: number;
readonly onChallengeSolved?: () => void;
readonly onChallengeFailed?: (retryAfter?: number) => void;
readonly onChallengeFailed?: (retryAt?: number) => void;
};
const NOW = Date.now();
const ONE_DAY = 24 * 3600 * 1000;
const NEVER_RETRY = NOW + ONE_DAY;
const IMMEDIATE_RETRY = NOW - ONE_DAY;
const NEVER_RETRY = NOW + DAY;
const IMMEDIATE_RETRY = NOW - DAY;
// Various timeouts in milliseconds
const DEFAULT_RETRY_AFTER = 25;
@ -35,15 +29,13 @@ const SOLVE_AFTER = 5;
describe('ChallengeHandler', () => {
const storage = new Map<string, any>();
const messageStorage = new Map<string, MinimalMessage>();
let challengeStatus = 'idle';
let sent: Array<string> = [];
let queuesStarted: Array<string> = [];
beforeEach(function beforeEach() {
storage.clear();
messageStorage.clear();
challengeStatus = 'idle';
sent = [];
queuesStarted = [];
this.sandbox = sinon.createSandbox();
this.clock = this.sandbox.useFakeTimers({
@ -55,56 +47,16 @@ describe('ChallengeHandler', () => {
this.sandbox.restore();
});
const createMessage = (
id: string,
options: CreateMessageOptions = {}
): MinimalMessage => {
const {
sentAt = 0,
isNormalBubble = true,
retryAfter = NOW + DEFAULT_RETRY_AFTER,
} = options;
const testLocalSent = sent;
const events = new Map<string, () => void>();
const createChallenge = (
conversationId: string,
options: Partial<RegisteredChallengeType> = {}
): RegisteredChallengeType => {
return {
id,
idForLogging: () => id,
isNormalBubble() {
return isNormalBubble;
},
getLastChallengeError() {
return {
name: 'Ignored',
message: 'Ignored',
retryAfter,
data: { token: 'token', options: ['recaptcha'] },
};
},
get(name) {
assert.equal(name, 'sent_at');
return sentAt;
},
on(name, handler) {
if (events.get(name)) {
throw new Error('Duplicate event');
}
events.set(name, handler);
},
off(name, handler) {
assert.equal(events.get(name), handler);
events.delete(name);
},
async retrySend() {
const handler = events.get('sent');
if (!handler) {
throw new Error('Expected handler');
}
handler();
testLocalSent.push(this.id);
},
conversationId,
token: '1',
retryAt: NOW + DEFAULT_RETRY_AFTER,
createdAt: NOW - SECOND,
...options,
};
};
@ -127,6 +79,10 @@ describe('ChallengeHandler', () => {
},
},
startQueue(conversationId: string) {
queuesStarted.push(conversationId);
},
onChallengeSolved,
onChallengeFailed,
@ -143,10 +99,6 @@ describe('ChallengeHandler', () => {
}, SOLVE_AFTER);
},
async getMessageById(messageId) {
return messageStorage.get(messageId);
},
async sendChallengeResponse() {
if (challengeError) {
throw challengeError;
@ -162,200 +114,156 @@ describe('ChallengeHandler', () => {
return handler;
};
const isInStorage = (messageId: string) => {
return (storage.get('challenge:retry-message-ids') || []).some(
({ messageId: storageId }: { messageId: string }) => {
return storageId === messageId;
const isInStorage = (conversationId: string) => {
return (storage.get(STORAGE_KEY) || []).some(
({ conversationId: storageId }: { conversationId: string }) => {
return storageId === conversationId;
}
);
};
it('should automatically retry after timeout', async function test() {
it('should automatically start queue after timeout', async function test() {
const handler = await createHandler();
const one = createMessage('1');
messageStorage.set('1', one);
const one = createChallenge('1');
await handler.register(one);
assert.isTrue(isInStorage(one.id));
assert.isTrue(isInStorage(one.conversationId));
assert.equal(challengeStatus, 'required');
await this.clock.nextAsync();
assert.deepEqual(sent, ['1']);
assert.deepEqual(queuesStarted, [one.conversationId]);
assert.equal(challengeStatus, 'idle');
assert.isFalse(isInStorage(one.id));
assert.isFalse(isInStorage(one.conversationId));
});
it('should send challenge response', async function test() {
const handler = await createHandler({ autoSolve: true });
const one = createMessage('1', { retryAfter: NEVER_RETRY });
messageStorage.set('1', one);
const one = createChallenge('1', {
retryAt: NEVER_RETRY,
});
await handler.register(one);
assert.equal(challengeStatus, 'required');
await this.clock.nextAsync();
assert.deepEqual(sent, ['1']);
assert.isFalse(isInStorage(one.id));
assert.deepEqual(queuesStarted, [one.conversationId]);
assert.isFalse(isInStorage(one.conversationId));
assert.equal(challengeStatus, 'idle');
});
it('should send old messages', async function test() {
it('should send old challenges', async function test() {
const handler = await createHandler();
// Put messages in reverse order to validate that the send order is correct
const messages = [
createMessage('3', { sentAt: 3 }),
createMessage('2', { sentAt: 2 }),
createMessage('1', { sentAt: 1 }),
const challenges = [
createChallenge('1'),
createChallenge('2'),
createChallenge('3'),
];
for (const message of messages) {
messageStorage.set(message.id, message);
await handler.register(message);
for (const challenge of challenges) {
await handler.register(challenge);
}
assert.equal(challengeStatus, 'required');
assert.deepEqual(sent, []);
assert.deepEqual(queuesStarted, []);
for (const message of messages) {
for (const challenge of challenges) {
assert.isTrue(
isInStorage(message.id),
`${message.id} should be in storage`
isInStorage(challenge.conversationId),
`${challenge.conversationId} should be in storage`
);
}
await handler.onOffline();
// Wait for messages to mature
// Wait for challenges to mature
await this.clock.nextAsync();
// Create new handler to load old messages from storage
// Create new handler to load old challenges from storage; it will start up online
await createHandler();
for (const message of messages) {
await handler.unregister(message);
for (const challenge of challenges) {
await handler.unregister(challenge.conversationId);
}
for (const message of messages) {
for (const challenge of challenges) {
assert.isFalse(
isInStorage(message.id),
`${message.id} should not be in storage`
isInStorage(challenge.conversationId),
`${challenge.conversationId} should not be in storage`
);
}
// The order has to be correct
assert.deepEqual(sent, ['1', '2', '3']);
assert.deepEqual(queuesStarted, ['1', '2', '3']);
assert.equal(challengeStatus, 'idle');
});
it('should send message immediately if it is ready', async () => {
it('should send challenge immediately if it is ready', async () => {
const handler = await createHandler();
const one = createMessage('1', { retryAfter: IMMEDIATE_RETRY });
await handler.register(one);
assert.equal(challengeStatus, 'idle');
assert.deepEqual(sent, ['1']);
});
it('should not change challenge status on non-bubble messages', async function test() {
const handler = await createHandler();
const one = createMessage('1', {
isNormalBubble: false,
const one = createChallenge('1', {
retryAt: IMMEDIATE_RETRY,
});
await handler.register(one);
assert.equal(challengeStatus, 'idle');
assert.deepEqual(sent, []);
await this.clock.nextAsync();
assert.deepEqual(sent, ['1']);
assert.deepEqual(queuesStarted, [one.conversationId]);
});
it('should not retry expired messages', async function test() {
it('should not retry expired challenges', async function test() {
const handler = await createHandler();
const bubble = createMessage('1');
messageStorage.set('1', bubble);
await handler.register(bubble);
assert.isTrue(isInStorage(bubble.id));
const one = createChallenge('1');
await handler.register(one);
assert.isTrue(isInStorage(one.conversationId));
const newHandler = await createHandler({
autoSolve: true,
expireAfter: -1,
});
await handler.unregister(bubble);
await handler.unregister(one.conversationId);
challengeStatus = 'idle';
await newHandler.load();
assert.equal(challengeStatus, 'idle');
assert.deepEqual(sent, []);
assert.deepEqual(queuesStarted, []);
await this.clock.nextAsync();
assert.equal(challengeStatus, 'idle');
assert.deepEqual(sent, []);
assert.isFalse(isInStorage(bubble.id));
assert.deepEqual(queuesStarted, []);
assert.isFalse(isInStorage(one.conversationId));
});
it('should send messages that matured while we were offline', async function test() {
it('should send challenges that matured while we were offline', async function test() {
const handler = await createHandler();
const one = createMessage('1');
messageStorage.set('1', one);
const one = createChallenge('1');
await handler.register(one);
assert.isTrue(isInStorage(one.id));
assert.deepEqual(sent, []);
assert.isTrue(isInStorage(one.conversationId));
assert.deepEqual(queuesStarted, []);
assert.equal(challengeStatus, 'required');
await handler.onOffline();
// Let messages mature
// Let challenges mature
await this.clock.nextAsync();
assert.isTrue(isInStorage(one.id));
assert.deepEqual(sent, []);
assert.isTrue(isInStorage(one.conversationId));
assert.deepEqual(queuesStarted, []);
assert.equal(challengeStatus, 'required');
// Go back online
await handler.onOnline();
assert.isFalse(isInStorage(one.id));
assert.deepEqual(sent, [one.id]);
assert.isFalse(isInStorage(one.conversationId));
assert.deepEqual(queuesStarted, [one.conversationId]);
assert.equal(challengeStatus, 'idle');
});
it('should not retry more than 5 times', async function test() {
const handler = await createHandler();
const one = createMessage('1', { retryAfter: IMMEDIATE_RETRY });
const retrySend = sinon.stub(one, 'retrySend');
messageStorage.set('1', one);
await handler.register(one);
assert.isTrue(isInStorage(one.id));
assert.deepEqual(sent, []);
assert.equal(challengeStatus, 'required');
// Wait more than 5 times
for (let i = 0; i < 6; i += 1) {
await this.clock.nextAsync();
}
assert.isTrue(isInStorage(one.id));
assert.deepEqual(sent, []);
assert.equal(challengeStatus, 'required');
sinon.assert.callCount(retrySend, 5);
});
it('should trigger onChallengeSolved', async function test() {
const onChallengeSolved = sinon.stub();
@ -364,8 +272,9 @@ describe('ChallengeHandler', () => {
onChallengeSolved,
});
const one = createMessage('1', { retryAfter: NEVER_RETRY });
messageStorage.set('1', one);
const one = createChallenge('1', {
retryAt: NEVER_RETRY,
});
await handler.register(one);
// Let the challenge go through
@ -383,8 +292,9 @@ describe('ChallengeHandler', () => {
onChallengeFailed,
});
const one = createMessage('1', { retryAfter: NEVER_RETRY });
messageStorage.set('1', one);
const one = createChallenge('1', {
retryAt: NEVER_RETRY,
});
await handler.register(one);
// Let the challenge go through