mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-24 08:23:12 +09:00
Complete test coverage for lib/link/link.js
Add tests for the remaining uncovered lines in lib/link/link.js.
This commit is contained in:
1 parent
5dd1490c75
commit
f0afbf0a33
2 files changed
+48
-13
No files matched your search
+2
-2
@@ -34,7 +34,7 @@ class Link {
|
|||||||
if (err instanceof errors.InvalidMessage) {
|
if (err instanceof errors.InvalidMessage) {
|
||||||
this.connector.close(`Invalid message: ${err.message}`);
|
this.connector.close(`Invalid message: ${err.message}`);
|
||||||
} else {
|
} else {
|
||||||
throw err;
|
this.connector.emit('error', err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -57,7 +57,7 @@ class Link {
|
|||||||
*/
|
*/
|
||||||
processMessage(message) {
|
processMessage(message) {
|
||||||
if (!schema.message(message)) {
|
if (!schema.message(message)) {
|
||||||
throw new errors.InvalidMessage("Malformed message");
|
throw new errors.InvalidMessage("Malformed");
|
||||||
}
|
}
|
||||||
|
|
||||||
let validator = this._validators.get(message.type);
|
let validator = this._validators.get(message.type);
|
||||||
|
|||||||
+46
-11
@@ -9,17 +9,49 @@ const mock = require("../../mock");
|
|||||||
|
|
||||||
describe("lib/link/link", function() {
|
describe("lib/link/link", function() {
|
||||||
describe("class Link", function() {
|
describe("class Link", function() {
|
||||||
let testLink = new link.Link('source', 'target', new mock.MockConnector());
|
let testConnector;
|
||||||
testLink.setValidator('simple', schema.compile({
|
let testLink;
|
||||||
properties: {
|
|
||||||
"data": {
|
before(function() {
|
||||||
type: 'object',
|
testConnector = new mock.MockConnector();
|
||||||
properties: {
|
testLink = new link.Link('source', 'target', testConnector);
|
||||||
"string": { type: 'string' },
|
testLink.setValidator('simple', schema.compile({
|
||||||
|
properties: {
|
||||||
|
"data": {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
"string": { type: 'string' },
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
}));
|
||||||
}));
|
});
|
||||||
|
|
||||||
|
it("should close connector on invalid messages", function() {
|
||||||
|
testConnector.sentMessages = [];
|
||||||
|
testConnector.emit('message', { invalid: true });
|
||||||
|
assert.deepEqual(
|
||||||
|
testConnector.sentMessages,
|
||||||
|
[{
|
||||||
|
seq: testConnector._seq - 1,
|
||||||
|
type: 'close',
|
||||||
|
data: { reason: "Invalid message: Malformed" },
|
||||||
|
}]
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should disconnect on close", function() {
|
||||||
|
testConnector.disconnectCalled = false;
|
||||||
|
testConnector.emit('message', { seq: 1, type: 'close', data: { reason: "test" }});
|
||||||
|
assert(testConnector.disconnectCalled, "disconnect was not called");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should emitt error processing message to connector", async function() {
|
||||||
|
testLink.setHandler('throws', () => { throw new Error("Handler error"); }, () => true);
|
||||||
|
let result = events.once(testConnector, 'message');
|
||||||
|
testConnector.emit('message', { seq: 1, type: 'throws', data: {}});
|
||||||
|
await assert.rejects(result, new Error("Handler error"));
|
||||||
|
});
|
||||||
|
|
||||||
describe(".setValidator()", function() {
|
describe(".setValidator()", function() {
|
||||||
it("should set the validator", function() {
|
it("should set the validator", function() {
|
||||||
@@ -58,7 +90,7 @@ describe("lib/link/link", function() {
|
|||||||
it("should throw on invalid message", function() {
|
it("should throw on invalid message", function() {
|
||||||
assert.throws(
|
assert.throws(
|
||||||
() => testLink.processMessage({ data: "invalid message" }),
|
() => testLink.processMessage({ data: "invalid message" }),
|
||||||
new errors.InvalidMessage("Malformed message")
|
new errors.InvalidMessage("Malformed")
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
it("should throw on message without validator", function() {
|
it("should throw on message without validator", function() {
|
||||||
@@ -130,7 +162,10 @@ describe("lib/link/link", function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe(".waitFor()", function() {
|
describe(".waitFor()", function() {
|
||||||
testLink.setValidator('waiter', (message) => true);
|
before(function() {
|
||||||
|
testLink.setValidator('waiter', (message) => true);
|
||||||
|
});
|
||||||
|
|
||||||
it("should throw on missing validator", async function() {
|
it("should throw on missing validator", async function() {
|
||||||
await assert.rejects(
|
await assert.rejects(
|
||||||
testLink.waitFor('no_validator', {}),
|
testLink.waitFor('no_validator', {}),
|
||||||
|
|||||||
Reference in new issue
Block a user