-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test emscripten-32wasm in the browser (#943)
added emscripten-wasm32 tests
- Loading branch information
1 parent
61d389c
commit d5d58db
Showing
10 changed files
with
252 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
name: Emscripten build | ||
on: [push, pull_request] | ||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.job }}-${{ github.ref }} | ||
cancel-in-progress: true | ||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- uses: mamba-org/setup-micromamba@v1 | ||
with: | ||
environment-name: xsimd | ||
create-args: >- | ||
microsoft::playwright | ||
python | ||
init-shell: bash | ||
|
||
|
||
|
||
- name: Build script | ||
shell: bash -el {0} | ||
run: | | ||
echo "Build script for wasm" | ||
playwright install | ||
./test/test_wasm/test_wasm.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>TEST_TITLE</title> | ||
<link rel="stylesheet" href="style.css"> | ||
<script src="test_xsimd.js"></script> | ||
</head> | ||
<body> | ||
<!-- page content --> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
# this dir | ||
TEST_WASM_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" | ||
SRC_DIR=$TEST_WASM_DIR/../.. | ||
|
||
|
||
# the emsdk dir can be passed as optional argument | ||
# if not passed, it will be downloaded in the current dir | ||
if [ $# -eq 0 ] | ||
then | ||
git clone https://github.com/emscripten-core/emsdk | ||
cd emsdk | ||
./emsdk install latest | ||
./emsdk activate latest | ||
source ./emsdk_env.sh | ||
|
||
else | ||
EMSCRIPTEN_DIR=$1 | ||
source $EMSCRIPTEN_DIR/emsdk_env.sh | ||
fi | ||
|
||
|
||
export LDFLAGS="" | ||
export CFLAGS="" | ||
export CXXFLAGS="" | ||
|
||
# build wasm | ||
mkdir -p build | ||
cd build | ||
emcmake cmake \ | ||
-DBUILD_TESTS=ON \ | ||
-DCMAKE_BUILD_TYPE=Release \ | ||
-DCMAKE_CXX_STANDARD=14 \ | ||
-DDOWNLOAD_DOCTEST=ON \ | ||
$SRC_DIR | ||
|
||
emmake make -j4 | ||
cd .. | ||
|
||
# run tests in browser | ||
python $TEST_WASM_DIR/test_wasm_playwright.py build/test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
|
||
from tempfile import TemporaryDirectory | ||
import shutil | ||
import socket | ||
import threading | ||
from contextlib import closing, contextmanager | ||
from http.server import HTTPServer, SimpleHTTPRequestHandler | ||
import os | ||
import asyncio | ||
from pathlib import Path | ||
from playwright.async_api import async_playwright | ||
|
||
THIS_DIR = os.path.dirname(os.path.realpath(__file__)) | ||
WORK_DIR = os.path.join(THIS_DIR, "work_dir") | ||
|
||
|
||
def find_free_port(): | ||
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s: | ||
s.bind(("", 0)) | ||
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | ||
return s.getsockname()[1] | ||
|
||
|
||
def start_server(work_dir, port): | ||
class Handler(SimpleHTTPRequestHandler): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, directory=work_dir, **kwargs) | ||
|
||
def log_message(self, fmt, *args): | ||
return | ||
|
||
httpd = HTTPServer(("127.0.0.1", port), Handler) | ||
|
||
thread = threading.Thread(target=httpd.serve_forever) | ||
thread.start() | ||
return thread, httpd | ||
|
||
|
||
@contextmanager | ||
def server_context(work_dir, port): | ||
thread, server = start_server(work_dir=work_dir, port=port) | ||
try: | ||
yield server, f"http://127.0.0.1:{port}" | ||
finally: | ||
server.shutdown() | ||
thread.join() | ||
|
||
async def playwright_run_page(page_url, headless=True, slow_mo=None): | ||
async with async_playwright() as p: | ||
if slow_mo is None: | ||
browser = await p.chromium.launch(headless=headless) | ||
else: | ||
browser = await p.chromium.launch( | ||
headless=headless, slow_mo=slow_mo | ||
) | ||
page = await browser.new_page() | ||
await page.goto(page_url) | ||
# n min = n_min * 60 * 1000 ms | ||
n_min = 4 | ||
page.set_default_timeout(n_min * 60 * 1000) | ||
|
||
async def handle_console(msg): | ||
txt = str(msg) | ||
print(txt) | ||
|
||
page.on("console", handle_console) | ||
|
||
|
||
status = await page.evaluate( | ||
f"""async () => {{ | ||
let test_module = await test_xsimd_wasm(); | ||
console.log("\\n\\n************************************************************"); | ||
console.log("XSIMD WASM TESTS:"); | ||
console.log("************************************************************"); | ||
let r = test_module.run_tests(); | ||
if (r == 0) {{ | ||
console.log("\\n\\n************************************************************"); | ||
console.log("XSIMD WASM TESTS PASSED"); | ||
console.log("************************************************************"); | ||
return r; | ||
}} | ||
else {{ | ||
console.log("************************************************************"); | ||
console.log("XSIMD WASM TESTS FAILED"); | ||
console.log("************************************************************"); | ||
return r; | ||
}} | ||
}}""" | ||
) | ||
return_code = int(status) | ||
return return_code | ||
def main(build_dir): | ||
|
||
work_dir = WORK_DIR# TemporaryDirectory() | ||
|
||
with TemporaryDirectory() as temp_dir: | ||
work_dir = Path(temp_dir) | ||
|
||
|
||
shutil.copy(f"{build_dir}/test_xsimd.wasm", work_dir) | ||
shutil.copy(f"{build_dir}/test_xsimd.js", work_dir) | ||
shutil.copy(f"{THIS_DIR}/browser_main.html", work_dir) | ||
|
||
port = find_free_port() | ||
with server_context(work_dir=work_dir, port=port) as (server, url): | ||
page_url = f"{url}/browser_main.html" | ||
ret = asyncio.run(playwright_run_page(page_url=page_url)) | ||
|
||
return ret | ||
|
||
|
||
|
||
if __name__ == "__main__": | ||
import sys | ||
|
||
# get arg from args | ||
build_dir = sys.argv[1] | ||
|
||
print(f"build_dir: {build_dir}") | ||
|
||
ret_code = main(build_dir) | ||
sys.exit(ret_code) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters