Skip to content

Commit

Permalink
Change some tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ramhr committed Feb 4, 2024
1 parent 6420e27 commit a4f7aa3
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 54 deletions.
35 changes: 35 additions & 0 deletions test/disconnect-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,41 @@ describe('disconnections', function () {
},
);
});

describe('hooks', () => {
let beforeConnectHook;
let afterConnectHook;

beforeEach(() => {
beforeConnectHook = sandbox.spy();
afterConnectHook = sandbox.spy();
});

afterEach(() => {
arnavmq.hooks.connection.removeBeforeConnect(beforeConnectHook);
arnavmq.hooks.connection.removeAfterConnect(afterConnectHook);
});

it('calls event hooks on connecting', async () => {
const consumedAllPromise = pDefer();
await arnavmq.consumer.consume(queue, () => {
consumedAllPromise.resolve();
});

await docker.disconnectNetwork();

arnavmq.hooks.connection.beforeConnect(beforeConnectHook);
arnavmq.hooks.connection.afterConnect(afterConnectHook);

await docker.connectNetwork();
await arnavmq.producer.produce(queue);

await consumedAllPromise.promise;

sinon.assert.called(beforeConnectHook);
sinon.assert.called(afterConnectHook);
});
});
});

describe('RPC', () => {
Expand Down
137 changes: 83 additions & 54 deletions test/producer-consumer-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,61 +176,9 @@ describe('producer/consumer', function () {
setupHooks();

await arnavmq.consumer.consume(queueName, (message) => Promise.resolve(`${message}-test`));
const result = await arnavmq.producer.produce(queueName, '85.69.30.121', { rpc: true });
const result = await arnavmq.producer.produce(queueName, sentMessage, { rpc: true });

assert.equal(result, '85.69.30.121-test');

// Test hooks were called
sinon.assert.calledWith(beforePublishHook, {
queue: queueName,
message: sentMessage,
parsedMessage: sinon.match.instanceOf(Buffer),
properties: sinon.match({ rpc: true }),
currentRetry: 0,
});
sinon.assert.calledWith(afterPublishHook, {
queue: queueName,
message: sentMessage,
parsedMessage: sinon.match.instanceOf(Buffer),
properties: sinon.match({ rpc: true }),
currentRetry: 0,
result,
error: undefined,
});
sinon.assert.calledWith(beforeProcessMessageHook, {
queue: queueName,
message: sinon.match({ content: sinon.match.instanceOf(Buffer) }),
content: sentMessage,
});
sinon.assert.calledWith(afterProcessMessageHook, {
queue: queueName,
message: sinon.match({ content: sinon.match.instanceOf(Buffer) }),
content: sentMessage,
});
sinon.assert.calledWith(beforeRpcReplyHook, {
queue: queueName,
reply: result,
serializedReply: sinon.match.instanceOf(Buffer),
replyProperties: {
correlationId: sinon.match.string,
persistent: true,
durable: true,
},
receiveProperties: sinon.match.object,
error: undefined,
});
sinon.assert.calledWith(afterRpcReplyHook, {
queue: queueName,
reply: result,
serializedReply: sinon.match.instanceOf(Buffer),
replyProperties: {
correlationId: sinon.match.string,
persistent: true,
durable: true,
},
receiveProperties: sinon.match.object,
written: true,
});
assert.equal(result, `${sentMessage}-test`);
});

it('should receive message that is only array', async () => {
Expand Down Expand Up @@ -330,6 +278,87 @@ describe('producer/consumer', function () {
assert.equal(letters, 0);
},
);

describe('hooks', () => {
beforeEach(() => setupHooks());

it('calls the producer "before and after publish" hooks', async () => {
const queueName = 'test-only-string-queue';
const sentMessage = uuid.v4();

await arnavmq.consumer.consume(queueName, (message) => Promise.resolve(`${message}-test`));
const result = await arnavmq.producer.produce(queueName, sentMessage, { rpc: true });

sinon.assert.calledWith(beforePublishHook, {
queue: queueName,
message: sentMessage,
parsedMessage: sinon.match.instanceOf(Buffer),
properties: sinon.match({ rpc: true }),
currentRetry: 0,
});
sinon.assert.calledWith(afterPublishHook, {
queue: queueName,
message: sentMessage,
parsedMessage: sinon.match.instanceOf(Buffer),
properties: sinon.match({ rpc: true }),
currentRetry: 0,
result,
error: undefined,
});
});

it('calls the consumer "before and after process message" hooks', async () => {
const queueName = 'test-only-string-queue';
const sentMessage = uuid.v4();

await arnavmq.consumer.consume(queueName, (message) => Promise.resolve(`${message}-test`));
await arnavmq.producer.produce(queueName, sentMessage, { rpc: true });

sinon.assert.calledWith(beforeProcessMessageHook, {
queue: queueName,
message: sinon.match({ content: sinon.match.instanceOf(Buffer) }),
content: sentMessage,
});
sinon.assert.calledWith(afterProcessMessageHook, {
queue: queueName,
message: sinon.match({ content: sinon.match.instanceOf(Buffer) }),
content: sentMessage,
});
});

it('calls the consumer "before and after rpc reply" hooks', async () => {
const queueName = 'test-only-string-queue';
const sentMessage = uuid.v4();

await arnavmq.consumer.consume(queueName, (message) => Promise.resolve(`${message}-test`));
const result = await arnavmq.producer.produce(queueName, sentMessage, { rpc: true });

sinon.assert.calledWith(beforeRpcReplyHook, {
queue: queueName,
reply: result,
serializedReply: sinon.match.instanceOf(Buffer),
replyProperties: {
correlationId: sinon.match.string,
persistent: true,
durable: true,
},
receiveProperties: sinon.match.object,
error: undefined,
});
sinon.assert.calledWith(afterRpcReplyHook, {
queue: queueName,
reply: result,
serializedReply: sinon.match.instanceOf(Buffer),
replyProperties: {
correlationId: sinon.match.string,
persistent: true,
durable: true,
},
receiveProperties: sinon.match.object,
written: true,
});
});
});
});

describe('msg requeueing', () => {
Expand Down

0 comments on commit a4f7aa3

Please sign in to comment.