Skip to main content

ic_db_cli/
host.rs

1//! Local probes borrow an explicitly selected server; the launcher owns its process.
2
3use ic_testkit::pocket_ic::{self, PocketIcBuilder, nonblocking::PocketIc};
4use ic_wire::Limits;
5use std::time::Duration;
6
7/// Upgrade a local fixture only after the IC has completed its stop request.
8/// Platform's suspended futures are heap-local; they are never migrated.
9/// Restart also follows a refused atomic upgrade so callers can inspect the
10/// preserved predecessor. PocketIC owns each management operation.
11/// # Errors
12/// Returns stop/start or upgrade refusal without retrying the upgrade.
13pub async fn upgrade_stopped(
14    pic: &PocketIc,
15    canister: pocket_ic::CanisterId,
16    wasm: Vec<u8>,
17    args: Vec<u8>,
18    sender: Option<pocket_ic::CanisterId>,
19) -> Result<(), pocket_ic::RejectResponse> {
20    pic.stop_canister(canister, sender).await?;
21    let result = pic.upgrade_canister(canister, wasm, args, sender).await;
22    pic.start_canister(canister, sender).await?;
23
24    result
25}
26
27/// Transport bounds for the fixed probe controls.
28pub const LIMITS: Limits = Limits {
29    bytes: 65_536,
30    allocation: 8 * 1024 * 1024,
31    nodes: 65_536,
32    depth: 32,
33};
34
35/// Create an instance on the explicitly supplied external server.
36/// Never discovers/downloads a binary, starts a server or creates a Tokio runtime.
37/// Call the upstream async `drop` explicitly to attempt deletion. The external
38/// launcher owns shutdown, including after a test panic; deletion is best-effort.
39///
40/// # Errors
41/// Refuses absent/invalid configuration or creation timeout. Timeout/cancellation
42/// can leave an instance on the external server; no successful cleanup is implied.
43/// # Panics
44/// Requires the caller's Tokio runtime; upstream construction panics propagate.
45pub async fn instance() -> Result<PocketIc, String> {
46    let endpoint = std::env::var("IC_TESTKIT_POCKET_IC_URL").map_err(|_| {
47        "run through scripts/with-pocketic.sh or supply IC_TESTKIT_POCKET_IC_URL".to_owned()
48    })?;
49    let url = endpoint
50        .parse()
51        .map_err(|_| "invalid PocketIC endpoint".to_owned())?;
52
53    tokio::time::timeout(
54        Duration::from_secs(30),
55        PocketIcBuilder::new()
56            .with_application_subnet()
57            .with_server_url(url)
58            .with_max_request_time_ms(Some(30_000))
59            .build_async(),
60    )
61    .await
62    .map_err(|_| {
63        "PocketIC instance creation timed out; remote creation may have occurred".to_owned()
64    })
65}