Skip to content

Commit 403b039

Browse files
authored
test: add reference count assertion script (#497)
1 parent 4b97d17 commit 403b039

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ jobs:
4141
git diff
4242
exit 1
4343
fi
44+
- name: Run reference checks
45+
run: bash buildbots/test-reference-count.sh
4446
build:
4547
name: Build
4648
timeout-minutes: 30
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import asyncio
2+
import gc
3+
4+
import objgraph
5+
import pandas as pd
6+
7+
from playwright.async_api import async_playwright
8+
9+
10+
def print_memory_objects() -> None:
11+
gc.collect()
12+
13+
df_dicts = pd.DataFrame()
14+
df_dicts["dicts"] = objgraph.by_type("dict")
15+
df_dicts["pw_types"] = df_dicts["dicts"].apply(lambda x: x.get("_type"))
16+
17+
head = df_dicts["pw_types"].value_counts().head(20)
18+
19+
for class_name, reference_count in head.items():
20+
print(class_name, reference_count)
21+
22+
23+
async def main() -> None:
24+
async with async_playwright() as p:
25+
browser = await p.chromium.launch()
26+
page = await browser.new_page()
27+
await page.goto("https://example.com")
28+
29+
page.on("dialog", lambda dialog: dialog.dismiss())
30+
for _ in range(100):
31+
await page.evaluate("""async () => alert()""")
32+
33+
await page.route("**/", lambda route, request: route.fulfill(body="OK"))
34+
35+
for i in range(100):
36+
response = await page.evaluate("""async () => (await fetch("/")).text()""")
37+
assert response == "OK"
38+
39+
await browser.close()
40+
print_memory_objects()
41+
42+
print("PW-OK")
43+
44+
45+
asyncio.run(main())

buildbots/test-reference-count.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
NOT_ALLOWED_REFERENCES=("Dialog" "Request" "Route")
6+
7+
OUTPUT=$(python buildbots/assets/test-reference-count-async.py)
8+
9+
if [[ "$OUTPUT" != *"PW-OK"* ]]; then
10+
echo "Script did not run successfully!"
11+
echo "Output: $OUTPUT"
12+
exit 1
13+
fi
14+
15+
for CLASS_NAME in ${NOT_ALLOWED_REFERENCES[*]}; do
16+
echo "Checking $CLASS_NAME"
17+
if [[ "$OUTPUT" == *"$CLASS_NAME"* ]]; then
18+
echo "There are $CLASS_NAME references in the memory!"
19+
echo "Output: $OUTPUT"
20+
exit 1
21+
fi
22+
echo "Check $CLASS_NAME passed"
23+
done
24+
25+
echo "-> Reference count assertions passed!"

local-requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ flake8==3.8.3
1919
twine==3.2.0
2020
pyOpenSSL==19.1.0
2121
service_identity==18.1.0
22+
pandas==1.2.2
23+
objgraph==3.5.0

0 commit comments

Comments
 (0)