Refactor instance communication

Change Instance class to be a subclass of Link and refactor the
communication to and establisment of instances to use a virtual
connection between the slave and the instance.  This is simplifies the
implementation of plugin communication for instances.

Replace InstanceRequest with a forwardTo option to Request which does
the task of forwarding in a much cleaner way.
This commit is contained in:
Hornwitser committed 2019-12-13 21:57:05 +01:00
1 parent 9f24d200b7
commit 5d8fa4e4ce
7 files changed
+381 -265

No files matched your search

+25 -2
View File
@@ -2,11 +2,12 @@ const assert = require('assert').strict;
const path = require('path');
const fs = require('fs-extra');
const link = require("lib/link");
const client = require("../client");
describe("Client testing", function() {
describe("class Instance", function() {
let instance = new client._Instance("dir", "factorioDir", { name:"foo" })
let instance = new client._Instance(new link.VirtualConnector(), "dir", "factorioDir", { name:"foo" });
it("should give the name on .name", function() {
assert.equal(instance.name, "foo");
})
@@ -81,7 +82,9 @@ describe("Client testing", function() {
fs.outputFileSync(path.join(testDir, "shared", "mod_a.zip"), "a");
fs.outputFileSync(path.join(testDir, "shared", "mod_b.zip"), "b");
fs.outputFileSync(path.join(testDir, "shared", "mod.dat"), "c");
let instance = new client._Instance(path.join(testDir, "instance"), "factorioDir", { name: "test" });
let instance = new client._Instance(
new link.VirtualConnector(), path.join(testDir, "instance"), "factorioDir", { name: "test" }
);
fs.outputFileSync(instance.path("mods", "mod_i.zip"), "i");
let discardingLogger = {
@@ -127,4 +130,24 @@ describe("Client testing", function() {
assert.equal(await fs.readFile(instance.path("mods", "mod_i.zip"), "utf-8"), "i");
});
});
describe("discoverInstances()", function() {
it("should discover test instance", async function() {
let logger = { log: () => {}, error: () => {} };
let instancePath = path.join("test", "file", "instances");
let instanceInfos = await client._discoverInstances(instancePath, logger);
assert.deepEqual(instanceInfos, new Map([
[1, {
config: {
id: 1,
name: "test",
factorioPort: null,
clientPort: null,
clientPassword: null
},
path: path.join(instancePath, "test"),
}],
]));
});
});
});
+7
View File
@@ -0,0 +1,7 @@
{
"id": 1,
"name": "test",
"factorioPort": null,
"clientPort": null,
"clientPassword": null
}
-1
View File
@@ -63,7 +63,6 @@ describe("Integration of Clusterio", function() {
masterAuthToken: "invalid",
publicIP: "invalid",
});
testSlave.instances = await testSlave.findInstances();
testSlaveConnection = new master._SlaveConnection({ agent: "test", version, name: "slave", id: 4}, slaveServer);
master._slaveConnections.set(4, testSlaveConnection);
});
+18 -17
View File
@@ -18,6 +18,15 @@ describe("lib/link/messages", function() {
links: ['source-target'],
});
describe("constructor", function() {
it("should throw on invalid forwardTo", function() {
assert.throws(
() => new link.Request({ forwardTo: 'invalid'}),
new Error("Invalid forwardTo value invalid")
);
});
});
describe(".attach()", function() {
it("should attach validator to a source link", function() {
testRequest.attach(testSourceLink);
@@ -35,20 +44,6 @@ describe("lib/link/messages", function() {
testRequest.attach(testTargetLink, async (message) => handlerResult );
assert(testTargetLink._handlers.has('test_request'), "Handler was not set");
});
it("should throw on handler returning object with seq", async function() {
handlerResult = { seq: 3 };
testTargetLink._handlers.get('test_request')({ seq: 2 });
let result = await new Promise(resolve => {
testTargetLink.connector.send = (type, data) => resolve({ type, data });
});
assert.deepEqual(result, {
type: 'test_response',
data: {
error: "response contains reserved property 'seq'",
seq: 2,
},
});
});
it("should send result of calling the handler", async function() {
handlerResult = { test: "handler" };
testTargetLink._handlers.get('test_request')({ seq: 2 });
@@ -107,15 +102,21 @@ describe("lib/link/messages", function() {
});
});
describe("class InstanceRequest", function() {
});
describe("class Event", function() {
let testEvent = new link.Event({
type: 'test',
links: ['source-target'],
});
describe("constructor", function() {
it("should throw on invalid forwardTo", function() {
assert.throws(
() => new link.Event({ forwardTo: 'invalid'}),
new Error("Invalid forwardTo value invalid")
);
});
});
describe(".attach()", function() {
it("should throw if missing handler on target link", function() {
assert.throws(