Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add worker integration test with st.write_stream() to check if the coroutine is awaited #998

Merged
merged 3 commits into from
Jun 27, 2024
Merged
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
36 changes: 34 additions & 2 deletions packages/kernel/src/worker-runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const TEST_SOURCES: {
files: Record<string, string>;
entrypoint: string;
requirements?: string[];
additionalAppTestCode?: string;
}[] = [
{
entrypoint: "data.column_config.py",
Expand Down Expand Up @@ -118,6 +119,20 @@ const TEST_SOURCES: {
),
},
},
{
// Checks if the async customization of st.write_stream() and runtime AST manipulation works.
entrypoint: "text.write_stream.py",
files: {
"text.write_stream.py": path.resolve(
__dirname,
"../../sharing-editor/public/samples/011_component_gallery/pages/text.write_stream.py",
),
},
additionalAppTestCode: `
at.button[0].click()
await at.run(timeout=10)
`,
},
];

suite("Worker intergration test running an app", async () => {
Expand Down Expand Up @@ -146,21 +161,38 @@ suite("Worker intergration test running an app", async () => {
requirements: testSource.requirements,
});

pyodide.globals.set(
"__additionalAppTestCode__",
testSource.additionalAppTestCode,
);

// The code above setting up the worker env is good enough to check if the worker is set up correctly,
// but it doesn't check the error occurred inside the Streamlit app running in the worker.
// So, we use the code below to test if the Streamlit app runs without any error.
await pyodide.runPythonAsync(`
import ast
import asyncio
import warnings
from streamlit.testing.v1 import AppTest

at = AppTest.from_file("${testSource.entrypoint}")

await at.run()
with warnings.catch_warnings(record=True) as w: # Test warning messages. Ref: https://docs.python.org/3/library/warnings.html#testing-warnings
warnings.simplefilter("always")
warnings.filterwarnings("ignore", message=r"\\n?Pyarrow will become a required dependency of pandas in the next major release of pandas") # Ignore PyArrow version warning from Pandas (https://github.com/pandas-dev/pandas/blob/v2.2.0/pandas/__init__.py#L221-L231)

await at.run()

if __additionalAppTestCode__:
bytecode = compile(__additionalAppTestCode__, "<string>", "exec", flags=ast.PyCF_ALLOW_TOP_LEVEL_AWAIT)
await eval(bytecode)

assert not at.exception, f"Exception occurred: {at.exception}"
assert len(w) == 0, f"Warning occurred: {w[0].message if w else None}"
`);
},
{
timeout: 30 * 1000,
timeout: 60 * 1000,
},
);
}
Expand Down
Loading