Skip to content

Commit

Permalink
fix(test): Fix flaky amqplib test
Browse files Browse the repository at this point in the history
  • Loading branch information
onurtemizkan committed Oct 3, 2024
1 parent 0b739c5 commit 587ce2c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@ import { connectToRabbitMQ, consumeMessageFromQueue, createQueue, sendMessageToQ

const queueName = 'queue1';

// Stop the process from exiting before the transaction is sent
// eslint-disable-next-line @typescript-eslint/no-empty-function
setInterval(() => {}, 1000);

// eslint-disable-next-line @typescript-eslint/no-floating-promises
(async () => {
const { connection, channel } = await connectToRabbitMQ();
await createQueue(queueName, channel);

const consumeMessagePromise = consumeMessageFromQueue(queueName, channel);

await Sentry.startSpan({ name: 'root span' }, async () => {
sendMessageToQueue(queueName, channel, JSON.stringify({ foo: 'bar01' }));
});

await consumeMessageFromQueue(queueName, channel);
await consumeMessagePromise;

await channel.close();
await connection.close();
})();
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,22 @@ export function sendMessageToQueue(queueName: string, channel: Channel, message:
}

async function consumer(queueName: string, channel: Channel): Promise<void> {
await channel.consume(
queueName,
message => {
if (message) {
channel.ack(message);
}
},
ACKNOWLEDGEMENT,
);
return new Promise((resolve, reject) => {
channel
.consume(
queueName,
message => {
if (message) {
channel.ack(message);
resolve();
} else {
reject(new Error('No message received'));
}
},
ACKNOWLEDGEMENT,
)
.catch(reject);
});
}

export async function consumeMessageFromQueue(queueName: string, channel: Channel): Promise<void> {
Expand Down

0 comments on commit 587ce2c

Please sign in to comment.