Skip to content

Commit

Permalink
Integration Test: wait rpc ready before test logic
Browse files Browse the repository at this point in the history
Signed-off-by: Eval EXEC <execvy@gmail.com>
  • Loading branch information
eval-exec committed Sep 19, 2024
1 parent f125662 commit 121540a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
14 changes: 14 additions & 0 deletions test/src/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ impl Miner {
}
}

pub fn wait_rpc_ready(&self) {
let header = self.rpc.lock().unwrap().get_tip_header();
let now = std::time::Instant::now();
while now.elapsed().as_secs() < 60 {
if header.is_ok() {
log::info!("rpc is ready");
return;
}
log::info!("waiting rpc ready...");
thread::sleep(Duration::from_secs(1));
}
panic!("rpc not ready")
}

pub fn generate_block(&self) -> H256 {
self.rpc
.lock()
Expand Down
2 changes: 1 addition & 1 deletion test/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Setup {
let ckb_child_process = ckb_cmd
.env("RUST_BACKTRACE", "full")
.stdin(Stdio::null())
.stdout(Stdio::null())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()
.expect("Run `ckb run` failed");
Expand Down
24 changes: 15 additions & 9 deletions test/src/spec/dao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ const LOCK_PERIOD_EPOCHES: u64 = 180;

pub struct DaoPrepareOne;

fn generate_lock_period_epochs(setup: &mut Setup) {
(0..LOCK_PERIOD_EPOCHES).for_each(|i| {
setup.miner().generate_epochs(1, EPOCH_LENGTH);
});
}

impl Spec for DaoPrepareOne {
fn run(&self, setup: &mut Setup) {
setup.miner().wait_rpc_ready();

let privkey_path = setup.miner().privkey_path().to_string();
assert_eq!(deposited_capacity(setup), 0);
assert_eq!(prepared_capacity(setup), 0);
Expand All @@ -37,9 +45,7 @@ impl Spec for DaoPrepareOne {
assert!(output.contains("Immature"));

// Drive the chain until since mature and then withdraw
setup
.miner()
.generate_epochs(LOCK_PERIOD_EPOCHES, EPOCH_LENGTH);
generate_lock_period_epochs(setup);
let out_points = vec![new_out_point(prepare_tx_hash, 0)];
let _withdraw_tx_hash = withdraw(setup, &out_points);
assert_eq!(deposited_capacity(setup), 0);
Expand All @@ -61,6 +67,8 @@ pub struct DaoPrepareMultiple;

impl Spec for DaoPrepareMultiple {
fn run(&self, setup: &mut Setup) {
setup.miner().wait_rpc_ready();

let privkey_path = setup.miner().privkey_path().to_string();
assert_eq!(deposited_capacity(setup), 0);
assert_eq!(prepared_capacity(setup), 0);
Expand All @@ -87,9 +95,7 @@ impl Spec for DaoPrepareMultiple {
assert!(output.contains("Immature"));

// Drive the chain until since mature and then withdraw
setup
.miner()
.generate_epochs(LOCK_PERIOD_EPOCHES, EPOCH_LENGTH);
generate_lock_period_epochs(setup);
let out_points = (0..shannons.len())
.map(|i| new_out_point(&prepare_tx_hash, i))
.collect::<Vec<_>>();
Expand All @@ -114,6 +120,8 @@ pub struct DaoWithdrawMultiple;
impl Spec for DaoWithdrawMultiple {
#[allow(clippy::needless_collect)]
fn run(&self, setup: &mut Setup) {
setup.miner().wait_rpc_ready();

assert_eq!(deposited_capacity(setup), 0);
assert_eq!(prepared_capacity(setup), 0);

Expand All @@ -133,9 +141,7 @@ impl Spec for DaoWithdrawMultiple {
assert_eq!(prepared_capacity(setup), 40_010_336_948_502);

// Drive the chain until since mature and then withdraw
setup
.miner()
.generate_epochs(LOCK_PERIOD_EPOCHES, EPOCH_LENGTH);
generate_lock_period_epochs(setup);
let out_points = prepare_tx_hashes
.into_iter()
.map(|hash| new_out_point(hash, 0))
Expand Down

0 comments on commit 121540a

Please sign in to comment.