gradio/js/app/test/unload_event_test.spec.ts
Freddy Boulton 6a4bf7abe2
Delete user state when they close the tab. Add an unload event for the demo and a delete_callback on gr.State to let developers control how resources are cleaned up (#7829)
* Delete state

* add changeset

* Delete state

* WIP

* Add load event

* Working ttl

* unload e2e test

* Clean up

* add changeset

* Fix notebook

* add changeset

* Connect to heartbeat in python client

* 15 second heartbeat

* Demo for unload

* Add notebook

* add changeset

* Fix docs

* revert demo changes

* Add docstrings

* lint 🙄

* Edit

* handle shutdown issue

* state comments

* client test

* Fix:

* Fix e2e test

* 3.11 incompatibility

* delete after one hour

* lint + highlight

* Update .changeset/better-tires-shave.md

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>

* Update .changeset/better-tires-shave.md

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
2024-04-01 18:31:56 -04:00

35 lines
1020 B
TypeScript

import { test, expect } from "@gradio/tootils";
import { readFileSync } from "fs";
test("when a user closes the page, the unload event should be triggered", async ({
page
}) => {
const increment = await page.locator("button", {
hasText: /Increment/
});
// if you click too fast, the page may close before the event is processed
await increment.click();
await page.waitForTimeout(100);
await increment.click();
await page.waitForTimeout(100);
await increment.click();
await page.waitForTimeout(100);
await increment.click();
await expect(page.getByLabel("Number")).toHaveValue("4");
await page.close();
await new Promise((resolve) => setTimeout(resolve, 5000));
const data = readFileSync(
"../../demo/unload_event_test/output_log.txt",
"utf-8"
);
expect(data).toContain("incremented 0");
expect(data).toContain("incremented 1");
expect(data).toContain("incremented 2");
expect(data).toContain("incremented 3");
expect(data).toContain("deleted 4");
expect(data).toContain("unloading");
});