mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-11 23:04:52 +09:00
Reverse the direction of the API for sending messages from
messageDefinition.send(link, { content });
to
link.send(new MessageClass(content));
and convert all message definitions to use the JSON serialisable class
pattern and explicit registration of message handlers, along with a
end-to-end based routing of messages instead of link to link.
The experience I gained from the existing way messages were defined and
sent over links was that it was difficult to understand for people
unfamiliar with the system. The automatic registration of message
handlers ment that code was being called as if by magic from nowhere
with no easy way to discover it. The manual forwarding and rules for
what can be sent where and how obscured its working.
The use of pure JSON as both input and output from message handlers made
it tedious to work with. Data had to be manually converted between the
program representation and the over the wire format, which made handler
code unplesant to write. Particularly because the over the wire format
used snake_case while the program code uses camelCase for variables.
The idea behind the decision to do it this way was to make it more clear
which data came from the wire and which from the program, and while this
worked it didn't make message handlers any nicer to write.
The direction of the API and magic registration of message handlers also
meant that a future migration to TypeScript would be troublesome. While
it may be possible to implement, the degree of TypeScript magic required
would go starkly against the principle of keep it simple stupid.
By refactoring to using the JSON serialisable class pattern most of
these issues go away. There is no longer a need to distinguish the on
wire format of messages with the program representation because the only
place that deals with the wire format of messages are the toJSON and
fromJSON methods on the classes themselves. Message handlers will work
with proper instances of classes instead of raw data, something that
makes them easier to write, and trivial to type with TypeScript.
End-to-end routing also simplifies sending messages that need to do
mulitple hops to reach the destination. And removes the need for having
all messages be specified in plugin's info module, as they can be routed
without having to be parsed completely.
The explicit registration of message handlers is something that I
thought would be tedious boilerplate code that could be automated away.
But experience now shows it to be the opposite. Allthough dull
repetition, the purpose it serves is to link together the code in a way
that humans can easily find. With explicit registration searching the
name of the message handler in the editor will show the registration that
ties it together with the type of message it handles and the kind of
registration. With implicit registration searching the name of the
message handler turns up only the handler with no clue as to what calls
it.
131 lines
4.8 KiB
JavaScript
131 lines
4.8 KiB
JavaScript
"use strict";
|
|
const assert = require("assert").strict;
|
|
const fs = require("fs-extra");
|
|
const path = require("path");
|
|
const util = require("util");
|
|
|
|
|
|
const { testLines } = require("./factorio/lines");
|
|
const libLoggingUtils = require("@clusterio/lib/logging_utils");
|
|
const libStream = require("@clusterio/lib/stream");
|
|
|
|
|
|
describe("lib/logging_utils.js", function() {
|
|
describe("formatServerOutput", function() {
|
|
it("should pass the test lines", function() {
|
|
for (let [reference, output] of testLines) {
|
|
let line = libLoggingUtils._formatServerOutput(output);
|
|
// Strip colours
|
|
line = line.replace(/\x1B\[\d+m/g, "");
|
|
assert.deepEqual(line, reference);
|
|
}
|
|
});
|
|
});
|
|
|
|
let logDir = path.join("test", "file", "log");
|
|
describe("class LogIndex", function() {
|
|
it("should create a new empty index if index.json does not exist", async function() {
|
|
let logIndex = await libLoggingUtils.LogIndex.load(logDir);
|
|
assert.deepEqual(logIndex.index, new Map());
|
|
});
|
|
it("should build index of only past log files", async function() {
|
|
let logIndex = await libLoggingUtils.LogIndex.load(logDir);
|
|
await logIndex.buildIndex();
|
|
assert.deepEqual(
|
|
new Set(logIndex.index.keys()),
|
|
new Set(["test-2021-12-10.log", "test-2021-12-11.log", "test-2021-12-12.log"])
|
|
);
|
|
});
|
|
it("should roundtrip index on save load cycle", async function() {
|
|
let logIndex = await libLoggingUtils.LogIndex.load(logDir);
|
|
await logIndex.buildIndex();
|
|
let tempDir = path.join("temp", "test", "log");
|
|
logIndex.logDirectory = tempDir;
|
|
await logIndex.save();
|
|
let loadedIndex = await libLoggingUtils.LogIndex.load(tempDir);
|
|
assert.deepEqual(
|
|
loadedIndex,
|
|
logIndex
|
|
);
|
|
});
|
|
it("should handle loading broken index", async function() {
|
|
let tempDir = path.join("temp", "test", "log");
|
|
await fs.outputFile(path.join(tempDir, "index.json"), "Broken JSON");
|
|
let logIndex = await libLoggingUtils.LogIndex.load(tempDir);
|
|
assert.deepEqual(logIndex.index, new Map());
|
|
});
|
|
});
|
|
|
|
let allLines = [];
|
|
let logIndex = null;
|
|
before(async function() {
|
|
for (let file of (await fs.readdir(logDir)).sort()) {
|
|
if (file === "excluded.txt") {
|
|
continue;
|
|
}
|
|
let fileStream = fs.createReadStream(path.join(logDir, file));
|
|
let lineStream = new libStream.LineSplitter({ readableObjectMode: true });
|
|
fileStream.pipe(lineStream);
|
|
for await (let line of lineStream) {
|
|
try {
|
|
allLines.push(JSON.parse(line));
|
|
} catch (err) {
|
|
allLines.push({ "level": "info", "message": line.toString() });
|
|
}
|
|
}
|
|
}
|
|
|
|
logIndex = await libLoggingUtils.LogIndex.load(logDir);
|
|
await logIndex.buildIndex();
|
|
});
|
|
|
|
describe("queryLog", function() {
|
|
for (let index of [() => undefined, () => logIndex]) {
|
|
describe(index() === undefined ? "without index" : "with index", function() {
|
|
it("returns nothing with an empty filter", async function() {
|
|
let log = await libLoggingUtils.queryLog(logDir, {}, index());
|
|
assert.deepEqual(log, []);
|
|
});
|
|
it("should return the whole log when querying all", async function() {
|
|
let log = await libLoggingUtils.queryLog(logDir, { all: true }, index());
|
|
assert.deepEqual(log, allLines);
|
|
});
|
|
it("should return the whole log in reverse when querying all desc", async function() {
|
|
let log = await libLoggingUtils.queryLog(logDir, { all: true, order: "desc" }, index());
|
|
assert.deepEqual(log, [...allLines].reverse());
|
|
});
|
|
it("should limit by limit", async function() {
|
|
let log = await libLoggingUtils.queryLog(logDir, { limit: 2, all: true }, index());
|
|
assert.deepEqual(log, allLines.slice(0, 2));
|
|
});
|
|
it("should filter by maxLevel", async function() {
|
|
let log = await libLoggingUtils.queryLog(logDir, { maxLevel: "info", all: true }, index());
|
|
assert.deepEqual(log, allLines.filter(
|
|
info => ["fatal", "error", "warn", "audit", "info"].includes(info.level)
|
|
));
|
|
log = await libLoggingUtils.queryLog(logDir, { maxLevel: "fatal", all: true }, index());
|
|
assert.deepEqual(log, allLines.filter(
|
|
info => ["fatal"].includes(info.level)
|
|
));
|
|
});
|
|
it("should filter by controller", async function() {
|
|
let log = await libLoggingUtils.queryLog(logDir, { controller: true }, index());
|
|
assert.deepEqual(log, allLines.filter(
|
|
info => info.host_id === undefined && info.instance_id === undefined
|
|
));
|
|
});
|
|
it("should filter by hostIds", async function() {
|
|
let log = await libLoggingUtils.queryLog(logDir, { hostIds: [1, 2] }, index());
|
|
assert.deepEqual(log, allLines.filter(
|
|
info => [1, 2].includes(info.host_id) && info.instance_id === undefined
|
|
));
|
|
});
|
|
it("should filter by instanceIds", async function() {
|
|
let log = await libLoggingUtils.queryLog(logDir, { instanceIds: [10, 11] }, index());
|
|
assert.deepEqual(log, allLines.filter(info => [10, 11].includes(info.instance_id)));
|
|
});
|
|
});
|
|
}
|
|
});
|
|
});
|