-
Notifications
You must be signed in to change notification settings - Fork 405
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
add test for the message handling behavior #1626
Merged
davidkel
merged 1 commit into
hyperledger-caliper:main
from
tunedev:test-worker-message-handler
Oct 10, 2024
Merged
add test for the message handling behavior #1626
davidkel
merged 1 commit into
hyperledger-caliper:main
from
tunedev:test-worker-message-handler
Oct 10, 2024
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
davidkel
requested changes
Sep 18, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, changes required here
Thanks for the feedback, would tend to all of them in order
…On Wed, 18 Sept 2024 at 09:55, Dave Kelsey ***@***.***> wrote:
***@***.**** requested changes on this pull request.
Sorry, changes required here
------------------------------
In packages/caliper-core/test/worker/worker-message-handler.js
<#1626 (comment)>:
> +
+ afterEach(() => {
+ mockery.deregisterAll();
+ mockery.disable();
+ sandbox.reset();
+ });
+
+ it('should register the worker when receiving a "register" message', async () => {
+ new WorkerMessageHandler(messengerMock, connectorFactoryMock);
+ const message = {
+ getType: () => MessageTypes.Register,
+ getSender: () => 'manager-uuid',
+ stringify: () => 'register message'
+ };
+
+ const registerHandler = messengerMock.on.getCalls().find(call => call.args[0] === MessageTypes.Register).args[1];
I don't think we should be finding the registered internal function and
invoking it to prove it works. This is like trying to drive a private
method. I'm sure there would be better ways to do this.
------------------------------
In packages/caliper-core/test/worker/worker-message-handler.js
<#1626 (comment)>:
> + const assignHandler = messengerMock.on.getCalls().find(call => call.args[0] === MessageTypes.AssignId).args[1];
+ await assignHandler(message);
+
+ sinon.assert.calledOnce(messengerMock.send);
+ sinon.assert.calledWith(messengerMock.send, sinon.match.instanceOf(AssignedMessage));
+ });
+
+ it('should initialize the worker when receiving an "initialize" message', async () => {
+ new WorkerMessageHandler(messengerMock, connectorFactoryMock);
+ connectorFactoryMock.resolves('connector-instance');
+ const message = {
+ getType: () => MessageTypes.Initialize,
+ stringify: () => 'initialize message'
+ };
+
+ const registeredHandler = messengerMock.on.getCalls().find(call => call.args[0] === MessageTypes.Initialize).args[1];
same comment about calling internal methods here.
------------------------------
In packages/caliper-core/test/worker/worker-message-handler.js
<#1626 (comment)>:
> + sinon.assert.calledOnce(messengerMock.send);
+ sinon.assert.calledWith(messengerMock.send, sinon.match.instanceOf(ReadyMessage));
+ });
+
+ it('should prepare the test when receiving a "prepare" message', async () => {
+ const handler = new WorkerMessageHandler(messengerMock, connectorFactoryMock);
+ handler.worker = workerMock;
+ const message = {
+ getType: () => MessageTypes.Prepare,
+ getRoundIndex: () => 0,
+ stringify: () => 'prepare message'
+ };
+
+ workerMock.prepareTest.resolves();
+
+ const registeredHandler = messengerMock.on.getCalls().find(call => call.args[0] === MessageTypes.Prepare).args[1];
same comment about calling internal methods here.
------------------------------
In packages/caliper-core/test/worker/worker-message-handler.js
<#1626 (comment)>:
> +
+ sinon.assert.calledOnce(workerMock.executeRound);
+ sinon.assert.calledOnce(messengerMock.send);
+ sinon.assert.calledWith(messengerMock.send, sinon.match.instanceOf(TestResultMessage));
+ });
+
+ it('should resolve the exit promise when receiving an "exit" message', async () => {
+ const handler = new WorkerMessageHandler(messengerMock, connectorFactoryMock);
+ handler.exitPromiseFunctions.resolve = sandbox.stub();
+ const message = {
+ getType: () => MessageTypes.Exit,
+ stringify: () => 'exit message'
+ };
+
+
+ const exitHandler = messengerMock.on.getCalls().find(call => call.args[0] === MessageTypes.Exit).args[1];
same comment about calling internal methods here.
------------------------------
In packages/caliper-core/test/worker/worker-message-handler.js
<#1626 (comment)>:
> +
+const sinon = require('sinon');
+const chai = require('chai');
+chai.should();
+const mockery = require('mockery');
+
+const MessageTypes = require('../../lib/common/utils/constants').Messages.Types;
+const ConnectedMessage = require('../../lib/common/messages/connectedMessage');
+const AssignedMessage = require('../../lib/common/messages/assignedMessage');
+const ReadyMessage = require('../../lib/common/messages/readyMessage');
+const PreparedMessage = require('../../lib/common/messages/preparedMessage');
+const TestResultMessage = require('../../lib/common/messages/testResultMessage');
+const WorkerMessageHandler = require('../../lib/worker/worker-message-handler');
+const CaliperWorker = require('../../lib/worker/caliper-worker');
+
+describe('Message Handling Behavior', () => {
This describe could be something like
"When receiving a message"
------------------------------
In packages/caliper-core/test/worker/worker-message-handler.js
<#1626 (comment)>:
> + warnOnUnregistered: false,
+ useCleanCache: true
+ });
+
+ mockery.registerMock('./caliper-worker', {
+ CaliperWorker: () => workerMock
+ });
+ });
+
+ afterEach(() => {
+ mockery.deregisterAll();
+ mockery.disable();
+ sandbox.reset();
+ });
+
+ it('should register the worker when receiving a "register" message', async () => {
"should register the worker when it's a "register" message"
------------------------------
In packages/caliper-core/test/worker/worker-message-handler.js
<#1626 (comment)>:
> + it('should register the worker when receiving a "register" message', async () => {
+ new WorkerMessageHandler(messengerMock, connectorFactoryMock);
+ const message = {
+ getType: () => MessageTypes.Register,
+ getSender: () => 'manager-uuid',
+ stringify: () => 'register message'
+ };
+
+ const registerHandler = messengerMock.on.getCalls().find(call => call.args[0] === MessageTypes.Register).args[1];
+ await registerHandler(message);
+
+ sinon.assert.calledOnce(messengerMock.send);
+ sinon.assert.calledWith(messengerMock.send, sinon.match.instanceOf(ConnectedMessage));
+ });
+
+ it('should assign the worker ID when receiving an "assignId" message', async () => {
see pattern for register message
------------------------------
In packages/caliper-core/test/worker/worker-message-handler.js
<#1626 (comment)>:
> + new WorkerMessageHandler(messengerMock, connectorFactoryMock);
+ const message = {
+ getType: () => MessageTypes.AssignId,
+ getWorkerIndex: () => 1,
+ stringify: () => 'assignId message'
+ };
+
+
+ const assignHandler = messengerMock.on.getCalls().find(call => call.args[0] === MessageTypes.AssignId).args[1];
+ await assignHandler(message);
+
+ sinon.assert.calledOnce(messengerMock.send);
+ sinon.assert.calledWith(messengerMock.send, sinon.match.instanceOf(AssignedMessage));
+ });
+
+ it('should initialize the worker when receiving an "initialize" message', async () => {
see pattern for register message
------------------------------
In packages/caliper-core/test/worker/worker-message-handler.js
<#1626 (comment)>:
> + getType: () => MessageTypes.Prepare,
+ getRoundIndex: () => 0,
+ stringify: () => 'prepare message'
+ };
+
+ workerMock.prepareTest.resolves();
+
+ const registeredHandler = messengerMock.on.getCalls().find(call => call.args[0] === MessageTypes.Prepare).args[1];
+ await registeredHandler(message);
+
+ sinon.assert.calledOnce(workerMock.prepareTest);
+ sinon.assert.calledOnce(messengerMock.send);
+ sinon.assert.calledWith(messengerMock.send, sinon.match.instanceOf(PreparedMessage));
+ });
+
+ it('should execute the test round when receiving a "test" message', async () => {
see pattern for register message
------------------------------
In packages/caliper-core/test/worker/worker-message-handler.js
<#1626 (comment)>:
> + getRoundIndex: () => 0,
+ stringify: () => 'test message'
+ };
+
+ workerMock.executeRound.resolves();
+
+ const registeredHandler = messengerMock.on.getCalls().find(call => call.args[0] === MessageTypes.Test).args[1];
+ await registeredHandler(message);
+
+
+ sinon.assert.calledOnce(workerMock.executeRound);
+ sinon.assert.calledOnce(messengerMock.send);
+ sinon.assert.calledWith(messengerMock.send, sinon.match.instanceOf(TestResultMessage));
+ });
+
+ it('should resolve the exit promise when receiving an "exit" message', async () => {
see pattern for register message
------------------------------
In packages/caliper-core/test/worker/worker-message-handler.js
<#1626 (comment)>:
> + it('should resolve the exit promise when receiving an "exit" message', async () => {
+ const handler = new WorkerMessageHandler(messengerMock, connectorFactoryMock);
+ handler.exitPromiseFunctions.resolve = sandbox.stub();
+ const message = {
+ getType: () => MessageTypes.Exit,
+ stringify: () => 'exit message'
+ };
+
+
+ const exitHandler = messengerMock.on.getCalls().find(call => call.args[0] === MessageTypes.Exit).args[1];
+ await exitHandler(message);
+
+ handler.exitPromiseFunctions.resolve.should.have.been.calledOnce;
+ });
+
+ it('should throw Error when constructor validations are violated', () => {
This should be in it's own describe "When creating a Worker Message
Handler"
We should also split this into 3 tests because we want to provide details
of each failure, the above doesn't tell me what the constructor validations
are
------------------------------
In packages/caliper-core/test/worker/worker-message-handler.js
<#1626 (comment)>:
> + const message = {
+ getType: () => MessageTypes.Exit,
+ stringify: () => 'exit message'
+ };
+
+
+ const exitHandler = messengerMock.on.getCalls().find(call => call.args[0] === MessageTypes.Exit).args[1];
+ await exitHandler(message);
+
+ handler.exitPromiseFunctions.resolve.should.have.been.calledOnce;
+ });
+
+ it('should throw Error when constructor validations are violated', () => {
+ const createHandler = (messenger, connectorFactory) => {
+ return () => new WorkerMessageHandler(messenger, connectorFactory);
+ };
does it need to be this convoluted ?
wouldn't
expect(() => {
new WorkerMessageHandler(undefined, connectorFactory);
}).to.throw(/'Messenger instance is undefined');
work as this is easier to see what exactly was being done
—
Reply to this email directly, view it on GitHub
<#1626 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AH6TKS7XKJE2OGQUU2YHA3DZXE5YJAVCNFSM6AAAAABOAHNW3SVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDGMJSGAZDIOBXGE>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
tunedev
force-pushed
the
test-worker-message-handler
branch
from
September 23, 2024 07:00
8d3eece
to
8f90b8f
Compare
Signed-off-by: Babatunde Sanusi <swisskid95@gmail.com>
tunedev
force-pushed
the
test-worker-message-handler
branch
from
September 23, 2024 08:25
8f90b8f
to
bbc06e4
Compare
davidkel
approved these changes
Oct 10, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Checklist
Issue/User story
Partially addresses #1606
Steps to Reproduce
Existing issues
Design of the fix
Validation of the fix
After this PR: Running the tests in caliper-core the listed %stmts in the code coverage report should tally with the below listed
Before the PR: Running the tests in caliper-core the listed %stmts in the code coverage report the below listed was what was there before
Automated Tests
What documentation has been provided for this pull request