| 572 | |
| 573 | #[test] |
| 574 | fn test_rpc_now() { |
| 575 | let _lock = PERF_LOCK.lock().unwrap_or_else(|e| e.into_inner()); |
| 576 | let mut analyzer = None; |
| 577 | assert!(handle_rpc_request(init_request(true), &mut analyzer) |
| 578 | .error |
| 579 | .is_none()); |
| 580 | let now_us = |analyzer: &mut Option<Analyzer>| { |
| 581 | let req = RpcRequest { |
| 582 | id: 3, |
| 583 | method: "now".to_string(), |
| 584 | params: serde_json::Value::Null, |
| 585 | }; |
| 586 | let res = handle_rpc_request(req, analyzer).result.unwrap(); |
| 587 | res["nowUs"].as_f64().unwrap() |
| 588 | }; |
| 589 | // Same clock as the events: a synchronous span recorded between two samples lands |
| 590 | // between them. `Initialize` runs on this thread, so unlike the pool-driven analysis |
| 591 | // spans it is always closed by the time `takePerf` drains. |
| 592 | let first = now_us(&mut analyzer); |
| 593 | assert!(handle_rpc_request(init_request(true), &mut analyzer) |
| 594 | .error |
| 595 | .is_none()); |
| 596 | let second = now_us(&mut analyzer); |
| 597 | let take = take_perf(&mut analyzer).result.unwrap(); |
| 598 | let init = take["events"] |
| 599 | .as_array() |
| 600 | .unwrap() |
| 601 | .iter() |
| 602 | .filter(|e| e["ph"] == "X" && e["name"] == "ngp.rust.Initialize") |
| 603 | .map(|e| { |
| 604 | let ts = e["ts"].as_f64().unwrap(); |
| 605 | (ts, ts + e["dur"].as_f64().unwrap()) |
| 606 | }) |
| 607 | .find(|&(start, _)| start >= first) |
| 608 | .unwrap(); |
| 609 | assert!(init.1 <= second); |
| 610 | // Unlike `takePerf`, it works while not recording: the clock exists once installed. |
| 611 | assert!(handle_rpc_request(init_request(false), &mut analyzer) |
| 612 | .error |
| 613 | .is_none()); |
| 614 | assert!(now_us(&mut analyzer) >= second); |
| 615 | } |
| 616 | } |