Skip to content

Commit

Permalink
[FEAT] [TEST] make chaos suspendable and restartable
Browse files Browse the repository at this point in the history
  • Loading branch information
aricart committed Oct 18, 2023
1 parent 2aa4293 commit 471f406
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions tests/helpers/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ if (argv.h || argv.help) {

const count = argv["count"] as number || 3;
const port = argv["port"] as number ?? 4222;
let chaosTimer: number | undefined;
let cluster: NatsServer[];

try {
const base = { debug: false };
const serverDebug = argv["server-debug"];
if (serverDebug) {
base.debug = true;
}
const cluster = argv.jetstream
cluster = argv.jetstream
? await NatsServer.jetstreamCluster(
count,
Object.assign(base, {
Expand Down Expand Up @@ -87,7 +89,6 @@ try {
if (argv.chaos === true && confirm("start chaos?")) {
chaos(cluster, 0);
}

waitForStop();
} catch (err) {
console.error(err);
Expand All @@ -99,7 +100,7 @@ function randomBetween(min: number, max: number): number {

function chaos(cluster: NatsServer[], delay: number) {
setTimeout(() => {
setInterval(() => {
chaosTimer = setInterval(() => {
restart(cluster);
}, 3000);
}, delay);
Expand Down Expand Up @@ -143,4 +144,24 @@ function waitForStop(): void {
Deno.addSignalListener("SIGTERM", () => {
Deno.exit();
});

console.log("control+t to stop/restart chaos");
Deno.addSignalListener("SIGINFO", () => {
if (chaosTimer === undefined) {
console.log("restarting chaos...");
console.log("control+t to stop chaos");
console.log("control+c to terminate");

chaos(cluster, 0);
return;
}
clearInterval(chaosTimer);
chaosTimer = undefined;
console.log("control+t to restart chaos");
console.log("control+c to terminate");
console.log("monitoring can be accessed:");
for (const s of cluster) {
console.log(`http://127.0.0.1:${s?.monitoring}`);
}
});
}

0 comments on commit 471f406

Please sign in to comment.