2020-02-07 22:26:05 +01:00
|
|
|
exports.make_zblueslip = function () {
|
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
|
|
|
|
lib.fatal = (msg) => {
|
|
|
|
throw Error(msg);
|
|
|
|
};
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-04-20 12:38:24 +02:00
|
|
|
lib.expect = (name, message, count = 1) => {
|
2020-04-08 15:35:06 +02:00
|
|
|
if (opts[name] === undefined) {
|
|
|
|
throw Error('unexpected arg for expect: ' + name);
|
|
|
|
}
|
2020-04-20 12:38:24 +02:00
|
|
|
if (count <= 0 && Number.isInteger(count)) {
|
|
|
|
throw Error('expected count should be a positive integer');
|
|
|
|
}
|
|
|
|
const obj = {message, count, expected_count: count};
|
|
|
|
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;
|
|
|
|
const i = lib.test_data[name].findIndex(x => x.message === message);
|
|
|
|
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]) {
|
|
|
|
throw Error (`Unexpected '${name}' message: ${message}`);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
lib.test_data[name][i].count -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const obj of lib.test_data[name]) {
|
|
|
|
const message = obj.message;
|
|
|
|
if (obj.count > 0) {
|
|
|
|
throw Error(`We saw ${obj.count} (expected ${obj.expected_count}) of '${name}': ${message}`);
|
|
|
|
} else if (obj.count < 0) {
|
|
|
|
throw Error(`We saw ${obj.expected_count - obj.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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
lib.get_test_logs = (name) => {
|
|
|
|
return lib.test_logs[name];
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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) {
|
2020-04-03 16:26:59 +02:00
|
|
|
if (typeof message !== 'string') {
|
|
|
|
// We may catch exceptions in blueslip, and if
|
|
|
|
// so our stub should include that.
|
|
|
|
if (message.toString().includes('exception')) {
|
|
|
|
message = message.toString();
|
|
|
|
} else {
|
|
|
|
throw Error('message should be string: ' + message);
|
|
|
|
}
|
|
|
|
}
|
2018-07-10 09:28:25 +02:00
|
|
|
lib.test_logs[name].push({message, more_info, stack});
|
2020-04-20 12:38:24 +02:00
|
|
|
const matched_error_message = lib.test_data[name].find(x => x.message === message);
|
|
|
|
const exact_match_fail = !matched_error_message;
|
2020-04-03 16:26:59 +02:00
|
|
|
if (exact_match_fail) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const error = 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;
|
|
|
|
};
|
|
|
|
|
2020-01-15 15:05:44 +01:00
|
|
|
lib.start_timing = () => {
|
|
|
|
return () => {};
|
|
|
|
};
|
|
|
|
|
2018-04-09 10:01:24 +02:00
|
|
|
return lib;
|
|
|
|
};
|