Skip to content

Commit 885f9d2

Browse files
committed
src/lib.rs:pub fn wait(sleep: &str)
use in tests
1 parent b523a9f commit 885f9d2

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

src/lib.rs

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,18 @@ pub fn run(args: Args) -> Result<()> {
178178
// a - b
179179
// }
180180

181-
pub fn wait(){
181+
pub fn wait(sleep: &str) {
182182
use std::process::Command;
183-
let mut child = Command::new("sleep").arg("5").spawn().unwrap();
184-
let _result = child.wait().unwrap();
185-
let _result = child.wait().unwrap();
183+
let sleep_cmd = Command::new("sleep").arg(sleep).output().expect("wait:sleep failed");
184+
let result = String::from_utf8(sleep_cmd.stdout)
185+
.map_err(|non_utf8| String::from_utf8_lossy(non_utf8.as_bytes()).into_owned())
186+
.unwrap();
187+
println!("\nwait:sleep:{:?}:{}", sleep, result);
188+
// if result.unwrap().len() <= 0 {
189+
// true
190+
// } else {
191+
// false
192+
// }
186193
}
187194

188195
#[cfg(test)]
@@ -194,81 +201,82 @@ mod tests {
194201
/// https://mempool.space/docs/api/rest
195202
/// cargo test -- --nocapture
196203
#[test]
197-
fn test_difficulty_adjustment(){
204+
fn test_difficulty_adjustment() {
198205
// GET /api/v1/difficulty-adjustment
199206
let binding = format!("v1/difficulty-adjustment").clone();
200207
let difficulty_adjustment: &str = blocking(&binding).expect("REASON");
201208
let difficulty_adjustment = generic_sys_call("difficulty_adjustment", "extraneous_arg");
202-
wait();
209+
wait("1");
203210
}
204211
#[test]
205-
fn test_price(){
212+
fn test_price() {
206213
// GET /api/v1/prices
207214
let binding = format!("v1/prices").clone();
208215
let prices: &str = blocking(&binding).expect("REASON");
209216
let prices = generic_sys_call("prices", "extraneous_arg");
210-
wait();
217+
wait("1");
211218
}
212219
#[test]
213220
fn test_historical_price() {
214221
// GET /api/v1/historical-price?currency=EUR&timestamp=1500000000
215222
let historical_price_json = historical_price(&"EUR", &"1500000000");
216223
print!("\n{{\"prices\":[{{\"time\":1499904000,\"EUR\":1964,\"USD\":2254.9}}],\"exchangeRates\":{{\"USDEUR\":0.92,\"USDGBP\":0.78,\"USDCAD\":1.38,\"USDCHF\":0.87,\"USDAUD\":1.53,\"USDJPY\":146.62}}}}\n");
217224
let historical_prices = generic_sys_call("historical_price", "USD");
218-
wait();
225+
wait("1");
219226
}
220227

221-
222228
/// Addresses
223229
#[test]
224-
fn test_address(){
230+
fn test_address() {
225231
// GET /api/address/:address
226232
let binding = format!("address/1wiz18xYmhRX6xStj2b9t1rwWX4GKUgpv").clone();
227-
let prices: &str = blocking(&binding).expect("REASON");
228-
wait();
233+
let prices: &str = blocking(&binding).expect("test_address failed");
234+
let prices = generic_sys_call("address", "1wiz18xYmhRX6xStj2b9t1rwWX4GKUgpv");
235+
wait("1");
229236
}
230237
#[test]
231-
fn test_address_txs(){
238+
fn test_address_txs() {
232239
// GET /api/address/:address/txs
233240
let binding = format!("address/1wiz18xYmhRX6xStj2b9t1rwWX4GKUgpv/txs").clone();
234241
let prices: &str = blocking(&binding).expect("REASON");
235-
wait();
242+
let prices = generic_sys_call("address_txs", "1wiz18xYmhRX6xStj2b9t1rwWX4GKUgpv");
243+
wait("1");
236244
}
237245
#[test]
238-
fn test_address_txs_chain(){
246+
fn test_address_txs_chain() {
239247
// GET /api/address/:address/txs/chain
240248
let binding = format!("address/1wiz18xYmhRX6xStj2b9t1rwWX4GKUgpv/txs/chain").clone();
241249
let prices: &str = blocking(&binding).expect("REASON");
242-
wait();
250+
wait("1");
243251
}
244252
#[test]
245-
fn test_address_txs_mempool(){
253+
fn test_address_txs_mempool() {
246254
// GET /api/address/:address/txs/mempool
247255
let binding = format!("address/1wiz18xYmhRX6xStj2b9t1rwWX4GKUgpv/txs/mempool").clone();
248256
let prices: &str = blocking(&binding).expect("REASON");
249-
wait();
257+
wait("1");
250258
}
251259
#[test]
252-
fn test_address_txs_utxo(){
260+
fn test_address_txs_utxo() {
253261
// GET /api/address/:address/utxo
254262
let binding = format!("address/1KFHE7w8BhaENAswwryaoccDb6qcT6DbYY/utxo").clone();
255263
let prices: &str = blocking(&binding).expect("REASON");
256-
wait();
264+
wait("1");
257265
}
258266
#[test]
259-
fn test_validate_address(){
267+
fn test_validate_address() {
260268
// GET /api/v1/validate-address/:address
261269
let binding = format!("v1/validate-address/1KFHE7w8BhaENAswwryaoccDb6qcT6DbYY").clone();
262270
let prices: &str = blocking(&binding).expect("REASON");
263-
wait();
271+
wait("1");
264272
}
265273

266274
/// Blocks
267275
#[test]
268276
fn test_blockheight() {
269277
let blockheight = blockheight::blockheight();
270278
assert_ne!(0 as f64, blockheight.unwrap());
271-
wait();
279+
wait("1");
272280
}
273281
/// Mining
274282
/// Fees
@@ -278,19 +286,18 @@ mod tests {
278286
/// Accelerator (Public)
279287
/// Accelerator (Authenticated)
280288
281-
282289
#[test]
283290
fn test_add() {
284291
// assert_eq!(add(1, 2), 3);
285-
wait();
292+
wait("1");
286293
}
287294

288295
#[test]
289296
fn test_bad_add() {
290297
// This assert would fire and test will fail.
291298
// Please note, that private functions can be tested too!
292299
// assert_ne!(bad_add(1, 2), 3);
293-
wait();
300+
wait("1");
294301
}
295302

296303
use std::panic::{catch_unwind, AssertUnwindSafe};
@@ -301,6 +308,6 @@ mod tests {
301308
}));
302309

303310
assert_ne!("foo panic message", *msg.unwrap_err().downcast_ref::<&str>().unwrap());
304-
wait();
311+
wait("1");
305312
}
306313
}

0 commit comments

Comments
 (0)