Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 9207eda

Browse files
expensestomaka
andauthored
Add a crate to test the in-browser light client. (#4887)
Co-Authored-By: Pierre Krieger <[email protected]>
1 parent b9a9589 commit 9207eda

File tree

7 files changed

+168
-2
lines changed

7 files changed

+168
-2
lines changed

.gitlab-ci.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ variables:
4040
# FIXME set to release
4141
CARGO_UNLEASH_INSTALL_PARAMS: "--version 1.0.0-alpha.10"
4242
CARGO_UNLEASH_PKG_DEF: "--skip node node-* pallet-template pallet-example pallet-example-* subkey chain-spec-builder sp-arithmetic-fuzzer"
43+
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER: "wasm-bindgen-test-runner"
44+
WASM_BINDGEN_TEST_TIMEOUT: 120
45+
CHROMEDRIVER_ARGS: "--log-level=INFO --whitelisted-ips=127.0.0.1"
4346

4447

4548
.collect-artifacts: &collect-artifacts
@@ -312,7 +315,6 @@ test-linux-stable-int:
312315
paths:
313316
- ${CI_COMMIT_SHORT_SHA}_int_failure.log
314317

315-
316318
check-web-wasm:
317319
stage: test
318320
<<: *docker-env
@@ -354,6 +356,15 @@ test-full-crypto-feature:
354356

355357
#### stage: build
356358

359+
test-browser-node:
360+
stage: build
361+
<<: *docker-env
362+
needs:
363+
- job: check-web-wasm
364+
artifacts: false
365+
script:
366+
- cargo +nightly test --target wasm32-unknown-unknown -p node-browser-testing -Z features=itarget
367+
357368
build-linux-substrate: &build-binary
358369
stage: build
359370
<<: *collect-artifacts

Cargo.lock

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ members = [
44
"bin/node-template/runtime",
55
"bin/node-template/pallets/template",
66
"bin/node/bench",
7+
"bin/node/browser-testing",
78
"bin/node/cli",
89
"bin/node/executor",
910
"bin/node/primitives",

bin/node/browser-testing/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "node-browser-testing"
3+
version = "2.0.0"
4+
authors = ["Parity Technologies <[email protected]>"]
5+
description = "Tests for the in-browser light client."
6+
edition = "2018"
7+
license = "GPL-3.0"
8+
9+
[dependencies]
10+
futures-timer = "3.0.2"
11+
libp2p = { version = "0.18.0", default-features = false }
12+
jsonrpc-core = "14.0.5"
13+
serde = "1.0.106"
14+
serde_json = "1.0.48"
15+
wasm-bindgen = { version = "0.2.60", features = ["serde-serialize"] }
16+
wasm-bindgen-futures = "0.4.10"
17+
wasm-bindgen-test = "0.3.10"
18+
futures = "0.3.4"
19+
20+
node-cli = { path = "../cli", default-features = false, features = ["browser"] }
21+
sc-rpc-api = { path = "../../../client/rpc-api" }

bin/node/browser-testing/src/lib.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright 2020 Parity Technologies (UK) Ltd.
2+
// This file is part of Substrate.
3+
4+
// Substrate is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Substrate is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
16+
17+
//! # Running
18+
//! Running this test can be done with
19+
//! ```text
20+
//! wasm-pack test --firefox --release --headless bin/node/browser-testing
21+
//! ```
22+
//! or (without `wasm-pack`)
23+
//! ```text
24+
//! CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER=wasm-bindgen-test-runner WASM_BINDGEN_TEST_TIMEOUT=60 cargo test --target wasm32-unknown-unknown
25+
//! ```
26+
//! For debug infomation, such as the informant, run without the `--headless`
27+
//! flag and open a browser to the url that `wasm-pack test` outputs.
28+
//! For more infomation see https://rustwasm.github.io/docs/wasm-pack/.
29+
30+
use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure};
31+
use wasm_bindgen_futures::JsFuture;
32+
use wasm_bindgen::JsValue;
33+
use jsonrpc_core::types::{MethodCall, Success, Version, Params, Id};
34+
use serde::de::DeserializeOwned;
35+
use futures_timer::Delay;
36+
use std::time::Duration;
37+
use futures::FutureExt;
38+
39+
wasm_bindgen_test_configure!(run_in_browser);
40+
41+
const CHAIN_SPEC: &str = include_str!("../../cli/res/flaming-fir.json");
42+
43+
fn rpc_call(method: &str) -> String {
44+
serde_json::to_string(&MethodCall {
45+
jsonrpc: Some(Version::V2),
46+
method: method.into(),
47+
params: Params::None,
48+
id: Id::Num(1)
49+
}).unwrap()
50+
}
51+
52+
fn deserialize_rpc_result<T: DeserializeOwned>(js_value: JsValue) -> T {
53+
let string = js_value.as_string().unwrap();
54+
let value = serde_json::from_str::<Success>(&string).unwrap().result;
55+
// We need to convert a `Value::Object` into a proper type.
56+
let value_string = serde_json::to_string(&value).unwrap();
57+
serde_json::from_str(&value_string).unwrap()
58+
}
59+
60+
#[wasm_bindgen_test]
61+
async fn runs() {
62+
let mut client = node_cli::start_client(CHAIN_SPEC.into(), "info".into())
63+
.await
64+
.unwrap();
65+
66+
let mut test_timeout = Delay::new(Duration::from_secs(45));
67+
loop {
68+
// Check that timeout hasn't expired.
69+
assert!((&mut test_timeout).now_or_never().is_none(), "Test timed out.");
70+
71+
// Let the node do a bit of work.
72+
Delay::new(Duration::from_secs(5)).await;
73+
74+
let state: sc_rpc_api::system::Health = deserialize_rpc_result(
75+
JsFuture::from(client.rpc_send(&rpc_call("system_health")))
76+
.await
77+
.unwrap()
78+
);
79+
80+
if state.should_have_peers && state.peers > 0 && state.is_syncing {
81+
break;
82+
}
83+
}
84+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"goog:chromeOptions": {
3+
"args": [
4+
"--whitelisted-ips=127.0.0.1"
5+
]
6+
}
7+
}

bin/node/cli/src/browser.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use crate::chain_spec::ChainSpec;
1818
use log::info;
1919
use wasm_bindgen::prelude::*;
20-
use sc_service::Configuration;
2120
use browser_utils::{
2221
Client,
2322
browser_configuration, set_console_error_panic_hook, init_console_log,

0 commit comments

Comments
 (0)