Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ jobs:
with:
node-version: ${{ matrix.node-version }}

# We use this for a single test that runs Bun for a subprocess
- name: Install Bun
uses: oven-sh/setup-bun@v1
with:
bun-version: latest

- run: npm ci
- run: npm run build --if-present

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
.eslintcache
dist
.envrc
src/__tests__/temp-test.ts
54 changes: 54 additions & 0 deletions src/__tests__/prefab.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as path from "path";
import Long from "long";
import fs from "fs";
import { spawn } from "child_process";

import basicConfig from "./fixtures/basicConfig";
import { DEFAULT_SOURCES } from "../sources";
Expand Down Expand Up @@ -1158,4 +1160,56 @@ describe("prefab", () => {
expect(prefab.get(secret.key)).toEqual(clearText);
});
});

describe("closing behavior", () => {
// NOTE: for ease of running the subprocess, we use Bun here.
// https://bun.sh/docs/installation
it("closes the prefab when the resolver is closed", async () => {
const testFile = path.join(__dirname, "temp-test.ts");
const testCode = `
import { Prefab } from "../prefab";

(async () => {
const prefab = new Prefab({
apiKey: "${validApiKey}",
});
await prefab.init();
const value = prefab.get("abc");
console.log(\`ABC is \${value}\`);
prefab.close();
})();
`;
fs.writeFileSync(testFile, testCode);

const startTime = Date.now();
const subprocess = spawn("bun", ["run", testFile]);

let output = "";
let errorOutput = "";

subprocess.stdout.on("data", (data: Buffer) => {
output += data.toString();
});

subprocess.stderr.on("data", (data: Buffer) => {
errorOutput += data.toString();
});

await new Promise<void>((resolve) => {
subprocess.on("close", () => {
resolve();
});
});

const duration = Date.now() - startTime;
fs.unlinkSync(testFile);

if (errorOutput.length > 0) {
console.error("Subprocess stderr:", errorOutput);
}

expect(output.trim()).toEqual("ABC is true");
expect(duration).toBeLessThan(3000);
});
});
});
12 changes: 12 additions & 0 deletions src/prefab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,18 @@ class Prefab implements PrefabInterface {
if (this.pollTimeout !== undefined && this.pollTimeout !== null) {
this.pollTimeout.unref();
}

// Clear all telemetry timeouts
Object.values(this.telemetry).forEach((telemetryComponent) => {
// First disable the component
telemetryComponent.enabled = false;
// Then clear its timeout
if (telemetryComponent.timeout !== undefined) {
clearTimeout(telemetryComponent.timeout);
telemetryComponent.timeout = undefined;
}
});

this.running = false;
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/telemetry/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@ import type { Telemetry } from "./types";
export const now = (): Long => Long.fromNumber(Date.now());

const syncTelemetry = (telemetry: Telemetry, backoff: Backoff): void => {
// Exit early if telemetry is disabled
if (!telemetry.enabled) {
return;
}

const delay = backoff.call();

telemetry.timeout = setTimeout(() => {
// Check if telemetry is still enabled before attempting to sync
if (!telemetry.enabled) {
return;
}

telemetry.sync().finally(() => {
syncTelemetry(telemetry, backoff);
});
Expand Down