Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix queued channel failure logic #436

Merged
merged 7 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/channel/queuedChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ export class QueuedChannel<T> implements OutputChannel<T> {

this.enqueue(message);

this.pending = this.pending.then(
() => this.channel
.publish(message)
.then(this.dequeue.bind(this)),
);
this.pending = this.pending
.catch(() => this.logger.debug('Failed to publish message, skipping...'))
.then(
() => this.channel
.publish(message)
.then(this.dequeue.bind(this)),
);

return this.pending;
}
Expand Down
34 changes: 34 additions & 0 deletions test/channel/queuedChannel.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {InMemoryQueue, CapacityRestrictedQueue} from '../../src/queue';
import {QueuedChannel, OutputChannel, MessageDeliveryError} from '../../src/channel';
import {Logger} from '../../src/logging';

describe('A queued channel', () => {
afterEach(() => {
Expand Down Expand Up @@ -166,6 +167,39 @@ describe('A queued channel', () => {
expect(outputChannel.publish).toHaveBeenCalledTimes(2);
});

it('should publish the next message after a failed message', async () => {
const logger: Logger = {
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
};

const outputChannel: OutputChannel<string> = {
close: jest.fn().mockResolvedValue(undefined),
publish: jest.fn()
.mockImplementationOnce(
() => new Promise((_, reject) => {
setTimeout(() => reject(new Error('Failed')), 1);
}),
)
.mockImplementationOnce(() => Promise.resolve(undefined)),
};

const channel = new QueuedChannel(outputChannel, new InMemoryQueue(), logger);

const failedPromise = channel.publish('foo');
const successPromise = channel.publish('bar');

await expect(failedPromise).rejects.toEqual(expect.any(Error));
await expect(successPromise).resolves.toBeUndefined();

expect(outputChannel.publish).toHaveBeenNthCalledWith(1, 'foo');
expect(outputChannel.publish).toHaveBeenNthCalledWith(2, 'bar');

expect(logger.debug).toHaveBeenCalledWith('Failed to publish message, skipping...');
});

it('should close the output channel and wait for pending messages', async () => {
const outputChannel: OutputChannel<string> = {
close: jest.fn().mockResolvedValue(undefined),
Expand Down
Loading