2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2022-04-09 23:44:38 +02:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2021-03-16 23:38:59 +01:00
|
|
|
function make_zblueslip() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const lib = {};
|
2018-04-09 10:01:24 +02:00
|
|
|
|
2020-02-07 22:26:05 +01:00
|
|
|
const opts = {
|
2018-04-09 10:01:24 +02:00
|
|
|
// Silently swallow all debug, log and info calls.
|
|
|
|
debug: false,
|
|
|
|
log: false,
|
|
|
|
info: false,
|
|
|
|
// Check against expected error values for the following.
|
|
|
|
warn: true,
|
|
|
|
error: true,
|
2020-02-07 22:26:05 +01:00
|
|
|
};
|
2020-04-03 16:41:13 +02:00
|
|
|
const names = Array.from(Object.keys(opts));
|
2018-04-09 10:01:24 +02:00
|
|
|
|
2020-04-08 15:35:06 +02:00
|
|
|
// For fatal messages, we should use assert.throws
|
2022-04-09 23:44:38 +02:00
|
|
|
/* istanbul ignore next */
|
2020-04-08 15:35:06 +02:00
|
|
|
lib.fatal = (msg) => {
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error(msg);
|
2020-04-08 15:35:06 +02:00
|
|
|
};
|
|
|
|
|
2018-04-09 10:01:24 +02:00
|
|
|
// Store valid test data for options.
|
|
|
|
lib.test_data = {};
|
|
|
|
lib.test_logs = {};
|
2020-04-03 16:41:13 +02:00
|
|
|
|
|
|
|
for (const name of names) {
|
2018-04-09 10:01:24 +02:00
|
|
|
lib.test_data[name] = [];
|
|
|
|
lib.test_logs[name] = [];
|
2020-04-03 16:41:13 +02:00
|
|
|
}
|
|
|
|
|
2022-04-09 23:44:38 +02:00
|
|
|
lib.expect = (name, message, expected_count = 1) => {
|
|
|
|
assert.notEqual(opts[name], undefined, `unexpected arg for expect: ${name}`);
|
|
|
|
assert.ok(
|
|
|
|
expected_count > 0 && Number.isInteger(expected_count),
|
|
|
|
"expected count should be a positive integer",
|
|
|
|
);
|
|
|
|
const obj = {message, count: 0, expected_count};
|
2020-04-20 12:38:24 +02:00
|
|
|
lib.test_data[name].push(obj);
|
2018-04-09 10:01:24 +02:00
|
|
|
};
|
2020-04-03 16:41:13 +02:00
|
|
|
|
2020-04-20 12:46:22 +02:00
|
|
|
const check_seen_messages = () => {
|
2020-04-03 16:41:13 +02:00
|
|
|
for (const name of names) {
|
2020-04-20 12:38:24 +02:00
|
|
|
for (const obj of lib.test_logs[name]) {
|
|
|
|
const message = obj.message;
|
2020-07-02 01:39:34 +02:00
|
|
|
const i = lib.test_data[name].findIndex((x) => x.message === message);
|
2020-04-20 12:38:24 +02:00
|
|
|
if (i === -1) {
|
|
|
|
// Only throw this for message types we want to explicitly track.
|
|
|
|
// For example, we do not want to throw here for debug messages.
|
|
|
|
if (opts[name]) {
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error(`Unexpected '${name}' message: ${message}`);
|
2020-04-20 12:38:24 +02:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2022-04-09 23:44:38 +02:00
|
|
|
lib.test_data[name][i].count += 1;
|
2020-04-20 12:38:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const obj of lib.test_data[name]) {
|
|
|
|
const message = obj.message;
|
2022-04-09 23:44:38 +02:00
|
|
|
assert.equal(
|
|
|
|
obj.count,
|
|
|
|
obj.expected_count,
|
|
|
|
`Expected ${obj.expected_count} of '${name}': ${message}`,
|
|
|
|
);
|
2020-04-03 16:41:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-20 12:38:24 +02:00
|
|
|
lib.reset = (skip_checks = false) => {
|
|
|
|
if (!skip_checks) {
|
2020-04-20 12:46:22 +02:00
|
|
|
check_seen_messages();
|
2020-04-20 12:38:24 +02:00
|
|
|
}
|
2020-04-03 16:41:13 +02:00
|
|
|
|
|
|
|
for (const name of names) {
|
|
|
|
lib.test_data[name] = [];
|
|
|
|
lib.test_logs[name] = [];
|
2018-04-09 10:01:24 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-02 01:41:40 +02:00
|
|
|
lib.get_test_logs = (name) => lib.test_logs[name];
|
2018-04-09 10:01:24 +02:00
|
|
|
|
|
|
|
// Create logging functions
|
2020-04-03 16:41:13 +02:00
|
|
|
for (const name of names) {
|
2018-04-09 10:01:24 +02:00
|
|
|
if (!opts[name]) {
|
2018-04-24 00:29:46 +02:00
|
|
|
// should just log the message.
|
2018-07-10 09:28:25 +02:00
|
|
|
lib[name] = function (message, more_info, stack) {
|
|
|
|
lib.test_logs[name].push({message, more_info, stack});
|
2018-04-24 00:29:46 +02:00
|
|
|
};
|
2020-04-03 16:41:13 +02:00
|
|
|
continue;
|
2018-04-09 10:01:24 +02:00
|
|
|
}
|
2018-07-10 09:28:25 +02:00
|
|
|
lib[name] = function (message, more_info, stack) {
|
2022-04-09 23:44:38 +02:00
|
|
|
/* istanbul ignore if */
|
2020-07-15 01:29:15 +02:00
|
|
|
if (typeof message !== "string") {
|
2020-04-03 16:26:59 +02:00
|
|
|
// We may catch exceptions in blueslip, and if
|
|
|
|
// so our stub should include that.
|
2020-07-15 01:29:15 +02:00
|
|
|
if (message.toString().includes("exception")) {
|
2020-04-03 16:26:59 +02:00
|
|
|
message = message.toString();
|
|
|
|
} else {
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error("message should be string: " + message);
|
2020-04-03 16:26:59 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-10 09:28:25 +02:00
|
|
|
lib.test_logs[name].push({message, more_info, stack});
|
2020-07-02 01:39:34 +02:00
|
|
|
const matched_error_message = lib.test_data[name].find((x) => x.message === message);
|
2020-04-20 12:38:24 +02:00
|
|
|
const exact_match_fail = !matched_error_message;
|
2020-04-03 16:26:59 +02:00
|
|
|
if (exact_match_fail) {
|
2020-10-07 09:58:04 +02:00
|
|
|
const error = new Error(`Invalid ${name} message: "${message}".`);
|
2018-04-09 10:01:24 +02:00
|
|
|
error.blueslip = true;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
2020-04-03 16:41:13 +02:00
|
|
|
}
|
2018-04-09 10:01:24 +02:00
|
|
|
|
2018-07-09 20:06:32 +02:00
|
|
|
lib.exception_msg = function (ex) {
|
|
|
|
return ex.message;
|
|
|
|
};
|
|
|
|
|
2021-02-04 00:30:50 +01:00
|
|
|
lib.measure_time = (label, f) => f();
|
2020-01-15 15:05:44 +01:00
|
|
|
|
2022-04-09 23:44:38 +02:00
|
|
|
/* istanbul ignore next */
|
2020-07-15 01:29:15 +02:00
|
|
|
lib.preview_node = (node) => "node:" + node;
|
2020-04-15 04:29:15 +02:00
|
|
|
|
2018-04-09 10:01:24 +02:00
|
|
|
return lib;
|
2021-03-16 23:38:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = make_zblueslip();
|