Skip to content

Commit 95a600a

Browse files
committed
fix: flaky email tests
for email test switching to a polling approach is the right fix, like the todo says. for outbox api it's dicier due to a race condition. We
1 parent b4c35a7 commit 95a600a

2 files changed

Lines changed: 39 additions & 42 deletions

File tree

apps/e2e/tests/backend/endpoints/api/v1/emails/outbox-api.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ const slowTemplate = deindent`
3838
3939
// Artificial delay to make the email slow to render
4040
const startTime = performance.now();
41-
while (performance.now() - startTime < 500) {
42-
// Busy wait - 500ms delay
41+
while (performance.now() - startTime < 2000) {
42+
// Busy wait - 2000ms delay
4343
}
4444
4545
export function EmailTemplate({ user, project }) {
@@ -648,8 +648,8 @@ describe("email outbox API", () => {
648648
649649
// Artificial delay to make the email slow to render
650650
const startTime = performance.now();
651-
while (performance.now() - startTime < 500) {
652-
// Busy wait - 500ms delay
651+
while (performance.now() - startTime < 2000) {
652+
// Busy wait - 2000ms delay
653653
}
654654
655655
export function EmailTemplate({ user, project }) {
@@ -670,12 +670,12 @@ describe("email outbox API", () => {
670670
`);
671671
break;
672672
} else {
673-
if (i >= 20) {
673+
if (i >= 50) {
674674
throw new StackAssertionError(`Timeout waiting for email in the outbox`, {
675675
outboxEmails: await getOutboxEmails(),
676676
});
677677
}
678-
await wait(25);
678+
await wait(100);
679679
}
680680
}
681681

@@ -884,7 +884,7 @@ describe("email outbox API", () => {
884884
let emailId: string | null = null;
885885
let pauseSucceeded = false;
886886

887-
for (let i = 0; i < 20; i++) {
887+
for (let i = 0; i < 50; i++) {
888888
const listResponse = await niceBackendFetch("/api/v1/emails/outbox", {
889889
method: "GET",
890890
accessType: "server",
@@ -908,7 +908,7 @@ describe("email outbox API", () => {
908908
}
909909
}
910910

911-
await wait(25);
911+
await wait(100);
912912
}
913913

914914
// These assertions must always run - test fails if we couldn't pause
@@ -938,7 +938,7 @@ describe("email outbox API", () => {
938938
expect(unpauseResponse.body.status).not.toBe("paused");
939939

940940
// Wait for the email to be sent (since we unpaused it)
941-
await wait(7_000);
941+
await wait(10_000);
942942

943943
// Verify the email was eventually sent
944944
const finalGetResponse = await niceBackendFetch(`/api/v1/emails/outbox/${emailId}`, {
@@ -1000,7 +1000,7 @@ describe("email outbox API", () => {
10001000
let emailId: string | null = null;
10011001
let pauseSucceeded = false;
10021002

1003-
for (let i = 0; i < 20; i++) {
1003+
for (let i = 0; i < 50; i++) {
10041004
const listResponse = await niceBackendFetch("/api/v1/emails/outbox", {
10051005
method: "GET",
10061006
accessType: "server",
@@ -1024,7 +1024,7 @@ describe("email outbox API", () => {
10241024
}
10251025
}
10261026

1027-
await wait(25);
1027+
await wait(100);
10281028
}
10291029

10301030
// We need to have successfully paused the email to test cancel

apps/e2e/tests/js/email.test.ts

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -187,39 +187,36 @@ it("should provide delivery statistics", async ({ expect }) => {
187187
subject: "Stats",
188188
});
189189

190-
// wait until the email is sent
191-
// TODO: use the equivalent of waitForMessagesWithSubject
192-
await wait(10_000);
193-
194-
const info = await serverApp.getEmailDeliveryStats();
195-
196-
expect(info).toMatchInlineSnapshot(`
190+
let info: any;
191+
for (let i = 0; i < 40; i++) {
192+
info = await serverApp.getEmailDeliveryStats();
193+
if (info.stats.hour.sent >= 1) break;
194+
await wait(500);
195+
}
196+
197+
expect(info.capacity.penalty_factor).toBe(1);
198+
expect(info.capacity.rate_per_second).toBeCloseTo(2.7778, 2);
199+
expect(info.stats).toMatchInlineSnapshot(`
197200
{
198-
"capacity": {
199-
"penalty_factor": 1,
200-
"rate_per_second": 2.7777793209876545,
201+
"day": {
202+
"bounced": 0,
203+
"marked_as_spam": 0,
204+
"sent": 1,
205+
},
206+
"hour": {
207+
"bounced": 0,
208+
"marked_as_spam": 0,
209+
"sent": 1,
210+
},
211+
"month": {
212+
"bounced": 0,
213+
"marked_as_spam": 0,
214+
"sent": 1,
201215
},
202-
"stats": {
203-
"day": {
204-
"bounced": 0,
205-
"marked_as_spam": 0,
206-
"sent": 1,
207-
},
208-
"hour": {
209-
"bounced": 0,
210-
"marked_as_spam": 0,
211-
"sent": 1,
212-
},
213-
"month": {
214-
"bounced": 0,
215-
"marked_as_spam": 0,
216-
"sent": 1,
217-
},
218-
"week": {
219-
"bounced": 0,
220-
"marked_as_spam": 0,
221-
"sent": 1,
222-
},
216+
"week": {
217+
"bounced": 0,
218+
"marked_as_spam": 0,
219+
"sent": 1,
223220
},
224221
}
225222
`);

0 commit comments

Comments
 (0)