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
23 changes: 23 additions & 0 deletions apps/server/src/preview/ServerBrowserContexts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,29 @@ describe("ServerBrowserContexts", () => {
await pool.close();
});

it("waits for the context to close when the same profile is cleared concurrently", async () => {
const human = makeContext();
const closed = Promise.withResolvers<void>();
human.close.mockImplementation(async () => closed.promise);
launches.persistent.mockResolvedValue(human as unknown as BrowserContext);
const pool = new ServerBrowserContexts(options());
await pool.contextFor("work/team");

const first = pool.clearProfile("work/team");
const second = pool.clearProfile("work/team");
const deletionsBeforeClose = launches.rm.mock.calls.length;
closed.resolve();
await Promise.all([first, second]);

expect(deletionsBeforeClose).toBe(0);
expect(human.close).toHaveBeenCalledTimes(1);
expect(launches.rm).toHaveBeenCalledExactlyOnceWith("/test/profiles/work%2Fteam", {
recursive: true,
force: true,
});
await pool.close();
});

it("opens a profile requested during its clear only after the storage is deleted", async () => {
launches.persistent.mockImplementation(async () => makeContext() as unknown as BrowserContext);
const configuration = options();
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/preview/ServerBrowserContexts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ export class ServerBrowserContexts {
async clearProfile(profileId: string) {
if (profileId === INCOGNITO_BROWSER_PROFILE_ID) return;
const key = JSON.stringify([profileId, null]);
const existing = this.clearing.get(key);
if (existing) return existing;
const clearing = (async () => {
const pending = this.contexts.get(key);
if (pending) {
Expand Down
Loading