Skip to content

Commit

Permalink
Revert "add worker log prefix; proper hard shutdown"
Browse files Browse the repository at this point in the history
This reverts commit 76a5417.
  • Loading branch information
naltatis committed Aug 26, 2024
1 parent 9fc9ac2 commit d45e35a
Showing 1 changed file with 14 additions and 38 deletions.
52 changes: 14 additions & 38 deletions tests/evcc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import axios from "axios";
import { exec, execSync } from "child_process";
import os from "os";
import path from "path";
import { Transform } from "stream";

const BINARY = "./evcc";

Expand All @@ -17,26 +16,6 @@ function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

function logPrefix() {
return `[worker:${process.env.TEST_WORKER_INDEX}]`;
}

function createSteamLog() {
return new Transform({
transform(chunk, encoding, callback) {
const lines = chunk.toString().split("\n");
lines.forEach((line) => {
if (line.trim()) log(line);
});
callback();
},
});
}

function log(...args) {
console.log(logPrefix(), ...args);
}

export function baseUrl() {
return `http://localhost:${workerPort()}`;
}
Expand Down Expand Up @@ -76,55 +55,52 @@ export async function cleanRestart(config, sqlDumps) {
async function _restoreDatabase(sqlDumps) {
const dumps = Array.isArray(sqlDumps) ? sqlDumps : [sqlDumps];
for (const dump of dumps) {
log("loading database", dbPath(), dump);
console.log("loading database", dbPath(), dump);
execSync(`sqlite3 ${dbPath()} < tests/${dump}`);
}
}

async function _start(config) {
const configFile = config.includes("/") ? config : `tests/${config}`;
const port = workerPort();
log(`wait until port ${port} is available`);
console.log(`wait until port ${port} is available`);
await waitOn({ resources: [`tcp:localhost:${port}`], reverse: true });
log("starting evcc", { config, port });
console.log("starting evcc", { config, port });
const instance = exec(
`EVCC_NETWORK_PORT=${port} EVCC_DATABASE_DSN=${dbPath()} ${BINARY} --config ${configFile}`
);
const steamLog = createSteamLog();
instance.stdout.pipe(steamLog);
instance.stderr.pipe(steamLog);
instance.stdout.pipe(process.stdout);
instance.stderr.pipe(process.stderr);
instance.on("exit", (code) => {
log("evcc terminated", { code, port, config });
steamLog.end();
console.log("evcc terminated", { code, port, config });
});
await waitOn({ resources: [baseUrl()] });
return instance;
}

async function _stop(instance) {
const port = workerPort();
if (instance) {
log("shutting down evcc hard", { port });
console.log("shutting down evcc hard");
// hard kill, only use of normal shutdown doesn't work
instance.kill("SIGKILL");
await waitOn({ resources: [`tcp:localhost:${port}`], reverse: true });
log("evcc is down", { port });
await sleep(300);
return;
}
log("shutting down evcc", { port });
const port = workerPort();
console.log("shutting down evcc", { port });
const res = await axios.post(`${baseUrl()}/api/auth/login`, { password: "secret" });
log(res.status, res.statusText);
console.log(res.status, res.statusText);
const cookie = res.headers["set-cookie"];
await axios.post(`${baseUrl()}/api/system/shutdown`, {}, { headers: { cookie } });
log(`wait until port ${port} is closed`);
console.log(`wait until port ${port} is closed`);
await waitOn({ resources: [`tcp:localhost:${port}`], reverse: true });
log("evcc is down", { port });
console.log("evcc is down", { port });
}

async function _clean() {
const db = dbPath();
if (fs.existsSync(db)) {
log("delete database", db);
console.log("delete database", db);
fs.unlinkSync(db);
}
}

0 comments on commit d45e35a

Please sign in to comment.