Skip to content

Commit

Permalink
run examples in wasm in CI (#4818)
Browse files Browse the repository at this point in the history
# Objective

- Run examples in WASM in CI
- Fix #4817 

## Solution

- on feature `bevy_ci_testing`
  - add an extra log message before exiting
  - when building for wasm, read CI config file at compile time
- add a simple [playwright](https://playwright.dev) test script that opens the browser then waits for the success log, and takes a screenshot
- add a CI job that runs the playwright test for Chromium and Firefox on one example (lighting) and save the screenshots
  - Firefox screenshot is good (with some clusters visible)
  - Chromium screenshot is gray, I don't know why but it's logging `GPU stall due to ReadPixels`
  - Webkit is not enabled for now, to revisit once https://bugs.webkit.org/show_bug.cgi?id=234926 is fixed or worked around
- the CI job only runs on bors validation

example run: https://github.com/mockersf/bevy/actions/runs/2361673465. The screenshots can be downloaded
  • Loading branch information
mockersf committed Jun 6, 2022
1 parent 85cd0eb commit 292cd03
Show file tree
Hide file tree
Showing 10 changed files with 395 additions and 20 deletions.
1 change: 1 addition & 0 deletions .github/bors.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ status = [
"build-android",
"markdownlint",
"run-examples",
"run-examples-on-wasm",
"check-doc",
"check-missing-examples-in-docs",
"check-unused-dependencies",
Expand Down
4 changes: 4 additions & 0 deletions .github/start-wasm-example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
/test-results/
/playwright-report/
/playwright/.cache/
76 changes: 76 additions & 0 deletions .github/start-wasm-example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .github/start-wasm-example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "start-wasm-example",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@playwright/test": "^1.22.1"
},
"dependencies": {
"dotenv": "^16.0.1"
}
}
107 changes: 107 additions & 0 deletions .github/start-wasm-example/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import type { PlaywrightTestConfig } from '@playwright/test';
import { devices } from '@playwright/test';

/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
require('dotenv').config();

/**
* See https://playwright.dev/docs/test-configuration.
*/
const config: PlaywrightTestConfig = {
testDir: './tests',
/* Maximum time one test can run for. */
timeout: 300_000,
expect: {
/**
* Maximum time expect() should wait for the condition to be met.
* For example in `await expect(locator).toHaveText();`
*/
timeout: 5000
},
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: 0,
/* Opt out of parallel tests on CI. */
workers: 1,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: 'list',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
actionTimeout: 0,
/* Base URL to use in actions like `await page.goto('/')`. */
// baseURL: 'http://localhost:3000',

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},

/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: {
...devices['Desktop Chrome'],
},
},

{
name: 'firefox',
use: {
...devices['Desktop Firefox'],
},
},

{
name: 'webkit',
use: {
...devices['Desktop Safari'],
},
},

/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: {
// ...devices['Pixel 5'],
// },
// },
// {
// name: 'Mobile Safari',
// use: {
// ...devices['iPhone 12'],
// },
// },

/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: {
// channel: 'msedge',
// },
// },
// {
// name: 'Google Chrome',
// use: {
// channel: 'chrome',
// },
// },
],

/* Folder for test artifacts such as screenshots, videos, traces, etc. */
// outputDir: 'test-results/',

/* Run your local dev server before starting the tests */
// webServer: {
// command: 'npm run start',
// port: 3000,
// },
};

export default config;
51 changes: 51 additions & 0 deletions .github/start-wasm-example/tests/wasm_example.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { test, expect, Page } from '@playwright/test';

test.beforeEach(async ({ page }) => {
await page.goto('http://localhost:8000/');
});

const MAX_TIMEOUT_FOR_TEST = 300_000;

test.describe('WASM example', () => {
test('Wait for success', async ({ page }, test_info) => {
let start = new Date().getTime();

let found = false;
while (new Date().getTime() - start < MAX_TIMEOUT_FOR_TEST) {
let msg = await promise_with_timeout(100, on_console(page), "no log found");
if (msg.includes("no log found")) {
continue;
}
console.log(msg);
if (msg.includes("Test successful")) {
let prefix = process.env.SCREENSHOT_PREFIX === undefined ? "screenshot" : process.env.SCREENSHOT_PREFIX;
await page.screenshot({ path: `${prefix}-${test_info.project.name}.png`, fullPage: true });
found = true;
break;
}
}

expect(found).toBe(true);
});

});

function on_console(page) {
return new Promise(resolve => {
page.on('console', msg => resolve(msg.text()));
});
}

async function promise_with_timeout(time_limit, task, failure_value) {
let timeout;
const timeout_promise = new Promise((resolve, reject) => {
timeout = setTimeout(() => {
resolve(failure_value);
}, time_limit);
});
const response = await Promise.race([task, timeout_promise]);
if (timeout) {
clearTimeout(timeout);
}
return response;
}
58 changes: 58 additions & 0 deletions .github/workflows/validation-jobs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,61 @@ jobs:
time CI_TESTING_CONFIG=$example cargo run --example $example_name --features "bevy_ci_testing"
sleep 10
done
run-examples-on-wasm:
runs-on: ubuntu-latest
timeout-minutes: 60
steps:
- uses: actions/checkout@v3

- uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: wasm32-unknown-unknown
override: true

- uses: actions/cache@v3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
~/.github/start-wasm-example/node_modules
target/
key: ${{ runner.os }}-wasm-run-examples-${{ hashFiles('**/Cargo.toml') }}

- name: install xvfb, llvmpipe and lavapipe
run: |
sudo apt-get update -y -qq
sudo add-apt-repository ppa:oibaf/graphics-drivers -y
sudo apt-get update
sudo apt install -y xvfb libegl1-mesa libgl1-mesa-dri libxcb-xfixes0-dev mesa-vulkan-drivers
- name: Install wasm-bindgen
run: cargo install --force wasm-bindgen-cli

- name: Setup playwright
run: |
cd .github/start-wasm-example
npm install
npx playwright install --with-deps
cd ../..
- name: First WASM build
run: |
cargo build --release --example ui --target wasm32-unknown-unknown
- name: Run examples
shell: bash
run: |
# start a webserver
python3 -m http.server --directory examples/wasm &
xvfb-run cargo run -p build-wasm-example -- --browsers chromium --browsers firefox --frames 25 --test shapes lighting text_debug breakout
- name: Save screenshots
uses: actions/upload-artifact@v3
with:
name: screenshots
path: .github/start-wasm-example/screenshot-*.png
25 changes: 19 additions & 6 deletions crates/bevy_app/src/ci_testing.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::{app::AppExit, App};
use serde::Deserialize;

use bevy_utils::tracing::info;

/// A configuration struct for automated CI testing.
///
/// It gets used when the `bevy_ci_testing` feature is enabled to automatically
Expand All @@ -20,18 +22,29 @@ fn ci_testing_exit_after(
if let Some(exit_after) = ci_testing_config.exit_after {
if *current_frame > exit_after {
app_exit_events.send(AppExit);
info!("Exiting after {} frames. Test successful!", exit_after);
}
}
*current_frame += 1;
}

pub(crate) fn setup_app(app: &mut App) -> &mut App {
let filename =
std::env::var("CI_TESTING_CONFIG").unwrap_or_else(|_| "ci_testing_config.ron".to_string());
let config: CiTestingConfig = ron::from_str(
&std::fs::read_to_string(filename).expect("error reading CI testing configuration file"),
)
.expect("error deserializing CI testing configuration file");
#[cfg(not(target_arch = "wasm32"))]
let config: CiTestingConfig = {
let filename = std::env::var("CI_TESTING_CONFIG")
.unwrap_or_else(|_| "ci_testing_config.ron".to_string());
ron::from_str(
&std::fs::read_to_string(filename)
.expect("error reading CI testing configuration file"),
)
.expect("error deserializing CI testing configuration file")
};
#[cfg(target_arch = "wasm32")]
let config: CiTestingConfig = {
let config = include_str!("../../../ci_testing_config.ron");
ron::from_str(config).expect("error deserializing CI testing configuration file")
};

app.insert_resource(config)
.add_system(ci_testing_exit_after);

Expand Down
1 change: 1 addition & 0 deletions tools/build-wasm-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ license = "MIT OR Apache-2.0"

[dependencies]
xshell = "0.2"
clap = { version = "3.1.12", features = ["derive"] }
Loading

0 comments on commit 292cd03

Please sign in to comment.