mz_testdrive/action/
sleep.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10use std::thread;
11use std::time::Duration;
12
13use anyhow::Context;
14
15use crate::action::ControlFlow;
16use crate::parser::BuiltinCommand;
17
18pub fn run_sleep(cmd: BuiltinCommand) -> Result<ControlFlow, anyhow::Error> {
19    run_sleep_inner(cmd, false)
20}
21
22pub fn run_random_sleep(cmd: BuiltinCommand) -> Result<ControlFlow, anyhow::Error> {
23    run_sleep_inner(cmd, true)
24}
25
26fn run_sleep_inner(mut cmd: BuiltinCommand, random: bool) -> Result<ControlFlow, anyhow::Error> {
27    let arg = cmd.args.string("duration")?;
28    cmd.args.done()?;
29    let duration = humantime::parse_duration(&arg).context("parsing duration")?;
30    let sleep = if random {
31        rand::random_range(Duration::from_secs(0)..duration)
32    } else {
33        duration
34    };
35    println!("Sleeping for {:?}", sleep);
36    thread::sleep(sleep);
37    Ok(ControlFlow::Continue)
38}