Skip to content

Commit ab92218

Browse files
authored
Add test for offchain-worker (#139)
Closes #10
1 parent 5e7a667 commit ab92218

1 file changed

Lines changed: 140 additions & 0 deletions

File tree

‎cli/tests/offchain_worker.rs‎

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// This file is part of try-runtime-cli.
2+
3+
// Copyright (C) Parity Technologies (UK) Ltd.
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
#![cfg(unix)]
19+
#![allow(deprecated)]
20+
21+
use std::time::Duration;
22+
23+
use assert_cmd::cargo_bin;
24+
use regex::Regex;
25+
use substrate_cli_test_utils as common;
26+
use tokio::process::Command;
27+
28+
/*
29+
* Test that the offchain-worker command works as expected.
30+
* It spawns a local dev node, and then runs the offchain-worker command against it.
31+
* The test covers two cases;
32+
* 1. The offchain-worker command is run with a live state, and should complete successfully.
33+
* 2. The offchain-worker command is run with a snap state, and should terminate with an error.
34+
*/
35+
#[tokio::test]
36+
async fn offchain_worker_works() {
37+
let port = 45789;
38+
39+
// Spawn a dev node.
40+
let _ = std::thread::spawn(move || {
41+
match common::start_node_inline(vec![
42+
"--no-hardware-benchmarks",
43+
"--dev",
44+
format!("--rpc-port={}", port).as_str(),
45+
]) {
46+
Ok(_) => {}
47+
Err(e) => {
48+
panic!("Node exited with error: {}", e);
49+
}
50+
}
51+
});
52+
53+
// Wait some time to ensure that the node is warmed up.
54+
std::thread::sleep(Duration::from_secs(90));
55+
56+
// 1. Test that the command runs successfully with a live state.
57+
common::run_with_timeout(Duration::from_secs(60), async move {
58+
let ws_url = format!("ws://localhost:{}", port);
59+
60+
fn run_offchain_worker(ws_url: &str) -> tokio::process::Child {
61+
let path = cargo_bin!("try-runtime");
62+
63+
assert!(
64+
path.exists(),
65+
"try-runtime binary not found at path: {}",
66+
path.display()
67+
);
68+
69+
Command::new(path)
70+
.stdout(std::process::Stdio::piped())
71+
.stderr(std::process::Stdio::piped())
72+
.arg("--runtime=existing")
73+
.arg("offchain-worker")
74+
.args(["live", format!("--uri={}", ws_url).as_str()])
75+
.kill_on_drop(true)
76+
.spawn()
77+
.unwrap()
78+
}
79+
80+
// Try to run offchain-worker command
81+
let offchain_execution = run_offchain_worker(&ws_url);
82+
83+
// Assert that command runs to completion successfully
84+
assert!(offchain_execution
85+
.wait_with_output()
86+
.await
87+
.unwrap()
88+
.status
89+
.success());
90+
})
91+
.await;
92+
93+
// 2. Test attempting to use non-live state. Should terminate with error.
94+
common::run_with_timeout(Duration::from_secs(60), async move {
95+
let ws_url = format!("ws://localhost:{}", port);
96+
97+
fn run_offchain_worker_snap_state(ws_url: &str) -> tokio::process::Child {
98+
let path = cargo_bin!("try-runtime");
99+
100+
assert!(
101+
path.exists(),
102+
"try-runtime binary not found at path: {}",
103+
path.display()
104+
);
105+
106+
let project_root = env!("CARGO_MANIFEST_DIR");
107+
let snap_file_path = format!("{}/tests/snaps/rococo-people.snap", project_root);
108+
109+
Command::new(path)
110+
.stdout(std::process::Stdio::piped())
111+
.stderr(std::process::Stdio::piped())
112+
.arg("--runtime=existing")
113+
.arg("offchain-worker")
114+
.args(["--header-ws-uri", ws_url])
115+
.args(["snap", "--path", snap_file_path.as_str()])
116+
.kill_on_drop(true)
117+
.spawn()
118+
.unwrap()
119+
}
120+
121+
// Try to execute command
122+
let mut offchain_execution = run_offchain_worker_snap_state(&ws_url);
123+
let expected_output = r"execute block currently only supports Live state";
124+
let re = Regex::new(expected_output).unwrap();
125+
let matched =
126+
common::wait_for_stream_pattern_match(offchain_execution.stderr.take().unwrap(), re)
127+
.await;
128+
129+
assert!(matched.is_ok());
130+
131+
// Assert that the command did not exit successfully
132+
assert!(!offchain_execution
133+
.wait_with_output()
134+
.await
135+
.unwrap()
136+
.status
137+
.success());
138+
})
139+
.await;
140+
}

0 commit comments

Comments
 (0)