The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
GDGT-3201 fix flakiness in dev server test (#83308)
Jakub Chodorowiczfb70ee539f10
diff --git a/e2e/support/helpers/e2e-custom-viz-dev-server-tasks.ts b/e2e/support/helpers/e2e-custom-viz-dev-server-tasks.tsindex 6e43826216e..2c18f1ee1be 100644--- a/e2e/support/helpers/e2e-custom-viz-dev-server-tasks.ts+++ b/e2e/support/helpers/e2e-custom-viz-dev-server-tasks.ts@@ -4,8 +4,13 @@ import { tmpdir } from "node:os";import { join } from "node:path";const TIMEOUT = 30_000;+const SHUTDOWN_TIMEOUT = 10_000;const POLL_INTERVAL = 250;+function sleep(ms: number): Promise<void> {+ return new Promise((resolve) => setTimeout(resolve, ms));+}+async function waitForHttpOk(url: string,timeoutMs: number,@@ -22,7 +27,7 @@ async function waitForHttpOk(} catch {console.log("Dev server not up yet, keep polling");}- await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL));+ await sleep(POLL_INTERVAL);}throw new Error(@@ -30,6 +35,37 @@ async function waitForHttpOk();}+async function waitForConnectionRefused(+ url: string,+ timeoutMs: number,+): Promise<boolean> {+ const start = Date.now();++ while (Date.now() - start < timeoutMs) {+ const signal = AbortSignal.timeout(POLL_INTERVAL * 4);+ try {+ await fetch(url, { signal });+ } catch {+ if (!signal.aborted) {+ return true;+ }+ }+ await sleep(POLL_INTERVAL);+ }++ return false;+}++function killProcessGroup(pid: number, signal: NodeJS.Signals): void {+ try {+ process.kill(-pid, signal);+ } catch {+ try {+ process.kill(pid, signal);+ } catch {}+ }+}+type StartCustomVizDevServerArgs = {cwd: string;port?: number;@@ -48,7 +84,19 @@ export async function startCustomVizDevServer(const port = args.port ?? 5174;if (running) {- stopCustomVizDevServer(running.pid);+ await stopCustomVizDevServer(running.pid);+ }++ const url = `http://localhost:${port}`;+ const manifestUrl = `${url}/metabase-plugin.json`;+ const connectionRefused = await waitForConnectionRefused(+ manifestUrl,+ SHUTDOWN_TIMEOUT,+ );+ if (!connectionRefused) {+ throw new Error(+ `Cannot start custom viz dev server: port ${port} already in use`,+ );}const logFilePath = join(tmpdir(), `custom-viz-dev-server-${Date.now()}.log`);@@ -67,34 +115,41 @@ export async function startCustomVizDevServer(throw new Error("Failed to start custom-viz dev server (no pid)");}- const url = `http://localhost:${port}`;-// Ensure the dev server is ready and serving the manifest.- await waitForHttpOk(`${url}/metabase-plugin.json`, TIMEOUT, logFilePath);+ await waitForHttpOk(manifestUrl, TIMEOUT, logFilePath);running = { pid: child.pid, url };console.log(`Custom viz dev server started at ${url} (log: ${logFilePath})`);return running;}-export function stopCustomVizDevServer(pid: number): null {+export async function stopCustomVizDevServer(pid: number): Promise<null> {if (!pid) {return null;}- try {- process.kill(-pid, "SIGTERM");- } catch {- try {- process.kill(pid, "SIGTERM");- } catch {- // ignore- }+ const handle = running?.pid === pid ? running : null;+ if (handle) {+ running = null;}- if (running?.pid === pid) {- running = null;+ killProcessGroup(pid, "SIGTERM");++ if (!handle) {+ return null;+ }++ const manifestUrl = `${handle.url}/metabase-plugin.json`;+ if (await waitForConnectionRefused(manifestUrl, SHUTDOWN_TIMEOUT)) {+ return null;}- return null;+ killProcessGroup(pid, "SIGKILL");+ if (await waitForConnectionRefused(manifestUrl, SHUTDOWN_TIMEOUT)) {+ return null;+ }++ throw new Error(+ `Custom viz dev server still responding after SIGKILL: ${handle.url}`,+ );}diff --git a/e2e/test/scenarios/visualizations-charts/custom-viz.cy.spec.ts b/e2e/test/scenarios/visualizations-charts/custom-viz.cy.spec.tsindex 8d997ef0dbb..53dacdbec52 100644--- a/e2e/test/scenarios/visualizations-charts/custom-viz.cy.spec.ts+++ b/e2e/test/scenarios/visualizations-charts/custom-viz.cy.spec.ts@@ -1656,23 +1656,24 @@ describe("admin > custom visualizations", () => {const QUESTION_NAME = "Custom Viz Dev Mode Question Test";let devServerPid: number | null = null;- before(() => {- cy.exec(`mkdir -p ${tmpDir}`);+ beforeEach(() => {+ H.restore("postgres-writable");+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");+ H.updateSetting("csp-img-enabled", true);+ H.updateSetting("custom-viz-enabled", true);++ // The SDK build lives in `beforeEach` rather than `before` on purpose:+ // Cypress never retries a failed `before all` hook, so a slow install+ // there fails the whole suite with no second attempt.cy.log("Build the SDK so we can use the repo-local CLI");+ cy.exec(`mkdir -p ${tmpDir}`);cy.exec(`cd "${sdkDir}" && bun install --frozen-lockfile && bun run build`,{timeout: TIMEOUT,},);- });-- beforeEach(() => {- H.restore("postgres-writable");- cy.signInAsAdmin();- H.activateToken("bleeding-edge");- H.updateSetting("csp-img-enabled", true);- H.updateSetting("custom-viz-enabled", true);cy.exec(`rm -rf "${projectDir}"`, { timeout: TIMEOUT });cy.exec(@@ -1700,8 +1701,7 @@ describe("admin > custom visualizations", () => {);});- // Install dependencies in the tmp plugin folder.- cy.exec(`cd "${projectDir}" && npm i`, { timeout: TIMEOUT });+ cy.exec(`cd "${projectDir}" && bun install`, { timeout: TIMEOUT });cy.task<{ pid: number }>("startCustomVizDevServer", {cwd: projectDir,@@ -1806,9 +1806,11 @@ describe("admin > custom visualizations", () => {cy.log("When the dev server is stopped, the visualization should revert to the default",);- cy.task("stopCustomVizDevServer", devServerPid);+ cy.task("stopCustomVizDevServer", devServerPid).then(() => {+ devServerPid = null;+ });cy.reload();- H.main().findByText("18,760").should("be.visible");+ H.main().findByText("18,760", { timeout: 15000 }).should("be.visible");});});});