Skip to content

Commit 513ee58

Browse files
fix(e2e): add ClickHouse polling to userCount metrics test
The /internal/metrics endpoint was migrated to read from ClickHouse, which has eventual consistency. The test was immediately asserting total_users after signup and deletion without waiting for ClickHouse to sync. CI evidence: Freestyle mock run showed 'expected 1 to be +0' at projects.test.ts:1597 — the user delete hadn't propagated to ClickHouse yet. Fix: Poll with 500ms intervals (30s timeout) for both increment and decrement assertions. Co-Authored-By: Konstantin Wohlwend <n2d4xc@gmail.com>
1 parent 14e1d9f commit 513ee58

1 file changed

Lines changed: 30 additions & 8 deletions

File tree

apps/e2e/tests/backend/endpoints/api/v1/projects.test.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { isBase64Url } from "@stackframe/stack-shared/dist/utils/bytes";
2+
import { wait } from "@stackframe/stack-shared/dist/utils/promises";
23
import { it } from "../../../../helpers";
34
import { Auth, InternalApiKey, InternalProjectKeys, Project, backendContext, niceBackendFetch } from "../../../backend-helpers";
5+
import { performance } from "perf_hooks";
46

57

68
// TODO some of the tests here test /api/v1/projects/current, the others test /api/v1/internal/projects/current. We should split them into different test files
@@ -1579,10 +1581,20 @@ it("should increment and decrement userCount when a user is added to a project",
15791581
// Create a new user in the project
15801582
await Auth.Password.signUpWithEmail();
15811583

1582-
// Check that the userCount has been incremented
1583-
const updatedProjectResponse = await niceBackendFetch("/api/v1/internal/metrics", { accessType: "admin" });
1584-
expect(updatedProjectResponse.status).toBe(200);
1585-
expect(updatedProjectResponse.body.total_users).toBe(1);
1584+
// The metrics endpoint reads from ClickHouse (eventual consistency).
1585+
// Poll until the new user is visible.
1586+
const incrementStart = performance.now();
1587+
while (true) {
1588+
const updatedProjectResponse = await niceBackendFetch("/api/v1/internal/metrics", { accessType: "admin" });
1589+
expect(updatedProjectResponse.status).toBe(200);
1590+
if (updatedProjectResponse.body.total_users === 1) {
1591+
break;
1592+
}
1593+
if (performance.now() - incrementStart > 30_000) {
1594+
expect(updatedProjectResponse.body.total_users).toBe(1);
1595+
}
1596+
await wait(500);
1597+
}
15861598

15871599
// Delete the user
15881600
const deleteRes = await niceBackendFetch("/api/v1/users/me", {
@@ -1591,10 +1603,20 @@ it("should increment and decrement userCount when a user is added to a project",
15911603
});
15921604
expect(deleteRes.status).toBe(200);
15931605

1594-
// Check that the userCount has been decremented
1595-
const finalProjectResponse = await niceBackendFetch("/api/v1/internal/metrics", { accessType: "admin" });
1596-
expect(finalProjectResponse.status).toBe(200);
1597-
expect(finalProjectResponse.body.total_users).toBe(0);
1606+
// The metrics endpoint now reads from ClickHouse, which has eventual
1607+
// consistency. Poll until the delete has propagated.
1608+
const startedAt = performance.now();
1609+
while (true) {
1610+
const finalProjectResponse = await niceBackendFetch("/api/v1/internal/metrics", { accessType: "admin" });
1611+
expect(finalProjectResponse.status).toBe(200);
1612+
if (finalProjectResponse.body.total_users === 0) {
1613+
break;
1614+
}
1615+
if (performance.now() - startedAt > 30_000) {
1616+
expect(finalProjectResponse.body.total_users).toBe(0);
1617+
}
1618+
await wait(500);
1619+
}
15981620

15991621
});
16001622

0 commit comments

Comments
 (0)