Skip to content

Commit

Permalink
Log when out of order
Browse files Browse the repository at this point in the history
  • Loading branch information
SketchingDev committed Jul 28, 2023
1 parent aa38854 commit 3c61a67
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EventEmitter } from 'events';
import { Response } from '../Response';
import { orderByTimestamp } from './orderByTimestamp';
import debug from 'debug';

/**
* Provides the ability to delay messages for the purpose of re-ordering them.
Expand All @@ -16,6 +17,8 @@ export interface MessageDelayer extends EventEmitter {
* timestamps.
*/
export class ReorderedMessageDelayer extends EventEmitter implements MessageDelayer {
private static readonly debugger = debug('ReorderedMessageDelayer');

private static readonly DELAY_IN_MS = 2000;
private messages: Response<unknown>[] = [];

Expand Down Expand Up @@ -58,7 +61,13 @@ export class ReorderedMessageDelayer extends EventEmitter implements MessageDela

private releaseOldestMessage(): void {
if (this.messages.length > 0) {
this.messages = orderByTimestamp(this.messages);
const result = orderByTimestamp(this.messages);
if (result.wasRearranged) {
ReorderedMessageDelayer.debugger('Messages out of order: %O', this.messages);
}

this.messages = result.responses;

this.emit('message', this.messages.shift());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { orderByTimestamp } from './orderByTimestamp';
import { orderByTimestamp, orderByTimestampResult } from './orderByTimestamp';
import { Response } from '../Response';
import { StructuredMessage } from '../StructuredMessage';
import { SessionResponse, SessionResponseSuccessBody } from '../SessionResponse';
Expand Down Expand Up @@ -45,29 +45,35 @@ describe('orderWithTimestamps', () => {
createStructuredMessage('6', '2023-07-22T21:19:25.256Z'),
];

expect(orderByTimestamp(unordered)).toEqual([
createStructuredMessage('7', '2023-07-22T20:13:55.256Z'),
createSessionResponse({ connected: true, newSession: false, readOnly: false }),
createStructuredMessage('6', '2023-07-22T21:19:25.256Z'),
createStructuredMessage('1', '2023-07-22T21:19:41.256Z'),
createSessionResponse({ connected: false, newSession: true, readOnly: false }),
createSessionResponse({ connected: false, newSession: false, readOnly: true }),
createStructuredMessage('3', '2023-07-22T21:19:55.256Z'),
]);
expect(orderByTimestamp(unordered)).toStrictEqual<orderByTimestampResult>({
wasRearranged: true,
responses: [
createStructuredMessage('7', '2023-07-22T20:13:55.256Z'),
createSessionResponse({ connected: true, newSession: false, readOnly: false }),
createStructuredMessage('6', '2023-07-22T21:19:25.256Z'),
createStructuredMessage('1', '2023-07-22T21:19:41.256Z'),
createSessionResponse({ connected: false, newSession: true, readOnly: false }),
createSessionResponse({ connected: false, newSession: false, readOnly: true }),
createStructuredMessage('3', '2023-07-22T21:19:55.256Z'),
],
});
});

test('Does not change the order of non-timestamped elements', () => {
const input: Response<unknown>[] = [
createSessionResponse({ connected: true, newSession: false, readOnly: false }),
createSessionResponse({ connected: false, newSession: true, readOnly: false }),
createSessionResponse({ connected: false, newSession: false, readOnly: true }),
createSessionResponse({ connected: true, newSession: true, readOnly: false }),
createSessionResponse({ connected: true, newSession: true, readOnly: true }),
];

expect(orderByTimestamp(input)).toEqual([
createSessionResponse({ connected: true, newSession: false, readOnly: false }),
createSessionResponse({ connected: true, newSession: true, readOnly: false }),
createSessionResponse({ connected: false, newSession: false, readOnly: true }),
]);
expect(orderByTimestamp(input)).toStrictEqual<orderByTimestampResult>({
wasRearranged: false,
responses: [
createSessionResponse({ connected: true, newSession: false, readOnly: false }),
createSessionResponse({ connected: true, newSession: true, readOnly: false }),
createSessionResponse({ connected: true, newSession: true, readOnly: true }),
],
});
});

test('Correctly sorts an array with only timestamped elements', () => {
Expand All @@ -77,10 +83,13 @@ describe('orderWithTimestamps', () => {
createStructuredMessage('3', '2023-07-22T21:19:25.256Z'),
];

expect(orderByTimestamp(unordered)).toStrictEqual([
createStructuredMessage('3', '2023-07-22T21:19:25.256Z'),
createStructuredMessage('1', '2023-07-22T21:19:41.256Z'),
createStructuredMessage('2', '2023-07-22T21:19:55.256Z'),
]);
expect(orderByTimestamp(unordered)).toStrictEqual<orderByTimestampResult>({
wasRearranged: true,
responses: [
createStructuredMessage('3', '2023-07-22T21:19:25.256Z'),
createStructuredMessage('1', '2023-07-22T21:19:41.256Z'),
createStructuredMessage('2', '2023-07-22T21:19:55.256Z'),
],
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { Response } from '../Response';
import { StructuredMessage } from '../StructuredMessage';
import { isStructuredMessage } from '../WebMessengerGuestSession';

export function orderByTimestamp(array: Response<unknown>[]): Response<unknown>[] {
export interface orderByTimestampResult {
wasRearranged: boolean;
responses: Response<unknown>[];
}

export function orderByTimestamp(array: Response<unknown>[]): orderByTimestampResult {
const withTimestamps: StructuredMessage[] = [];
const withoutTimestamps: Response<unknown>[] = [];

Expand Down Expand Up @@ -32,5 +37,13 @@ export function orderByTimestamp(array: Response<unknown>[]): Response<unknown>[
}
}

return result;
let wasRearranged = false;
for (let i = 0; i < array.length; i++) {
if (!Object.is(result[i], array[i])) {
wasRearranged = true;
break;
}
}

return { wasRearranged, responses: result };
}

0 comments on commit 3c61a67

Please sign in to comment.