Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/pocket-ic/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ DEPENDENCIES = [
"@crate_index//:ic-certification",
"@crate_index//:ic-management-canister-types",
"@crate_index//:ic-transport-types",
"@crate_index//:once_cell",
"@crate_index//:reqwest",
"@crate_index//:schemars",
"@crate_index//:serde",
Expand All @@ -27,6 +28,7 @@ DEPENDENCIES = [
"@crate_index//:tracing",
"@crate_index//:tracing-appender",
"@crate_index//:tracing-subscriber",
"@crate_index//:uuid",
]

MACRO_DEPENDENCIES = [
Expand Down
2 changes: 2 additions & 0 deletions packages/pocket-ic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ hex = { workspace = true }
ic-certification = { workspace = true }
ic-management-canister-types = { workspace = true }
ic-transport-types = { workspace = true }
once_cell = "1.18.0"
reqwest = { workspace = true }
schemars = "0.8.16"
serde = { workspace = true }
Expand All @@ -44,6 +45,7 @@ tokio = { workspace = true }
tracing = { workspace = true }
tracing-appender = { workspace = true }
tracing-subscriber = { workspace = true }
uuid = { workspace = true }

[target.'cfg(windows)'.dependencies]
wslpath = "0.0.2"
Expand Down
10 changes: 7 additions & 3 deletions packages/pocket-ic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ use ic_management_canister_types::{
Snapshot,
};
use ic_transport_types::SubnetMetrics;
use once_cell::sync::Lazy;
use reqwest::Url;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
Expand All @@ -92,6 +93,7 @@ use tempfile::TempDir;
use thiserror::Error;
use tokio::runtime::Runtime;
use tracing::{instrument, warn};
use uuid::Uuid;
#[cfg(windows)]
use wslpath::windows_to_wsl;

Expand Down Expand Up @@ -150,6 +152,8 @@ impl PocketIcState {
}
}

pub static TEST_DRIVER_UUID: Lazy<Uuid> = Lazy::new(Uuid::new_v4);

pub struct PocketIcBuilder {
config: Option<ExtendedSubnetConfigSet>,
server_binary: Option<PathBuf>,
Expand Down Expand Up @@ -1796,10 +1800,10 @@ pub(crate) async fn start_or_reuse_server(server_binary: Option<PathBuf>) -> Url
}
}

// We use the test driver's process ID to share the PocketIC server between multiple tests
// We use the TEST_DRIVER_UUID to share the PocketIC server between multiple tests
// launched by the same test driver.
let test_driver_pid = std::process::id();
let port_file_path = std::env::temp_dir().join(format!("pocket_ic_{}.port", test_driver_pid));
let port_file_path =
std::env::temp_dir().join(format!("pocket_ic_{}.port", &*TEST_DRIVER_UUID));
let mut cmd = pocket_ic_server_cmd(&bin_path);
cmd.arg("--port-file");
#[cfg(windows)]
Expand Down
13 changes: 6 additions & 7 deletions packages/pocket-ic/src/nonblocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::common::rest::{
pub use crate::DefaultEffectiveCanisterIdError;
use crate::{
copy_dir, start_or_reuse_server, IngressStatusResult, PocketIcBuilder, PocketIcState,
RejectResponse,
RejectResponse, TEST_DRIVER_UUID,
};
use backoff::backoff::Backoff;
use backoff::{ExponentialBackoff, ExponentialBackoffBuilder};
Expand Down Expand Up @@ -44,6 +44,7 @@ use std::time::{Duration, SystemTime};
use tracing::{debug, instrument, warn};
use tracing_appender::non_blocking::WorkerGuard;
use tracing_subscriber::EnvFilter;
use uuid::Uuid;

// wait time between polling requests
const POLLING_PERIOD_MS: u64 = 10;
Expand Down Expand Up @@ -109,8 +110,7 @@ impl PocketIc {
instance_id: InstanceId,
max_request_time_ms: Option<u64>,
) -> Self {
let test_driver_pid = std::process::id();
let log_guard = setup_tracing(test_driver_pid);
let log_guard = setup_tracing(&TEST_DRIVER_UUID);

let reqwest_client = reqwest::Client::new();
debug!("instance_id={} Reusing existing instance.", instance_id);
Expand Down Expand Up @@ -190,8 +190,7 @@ impl PocketIc {
bitcoind_addr,
};

let test_driver_pid = std::process::id();
let log_guard = setup_tracing(test_driver_pid);
let log_guard = setup_tracing(&TEST_DRIVER_UUID);

let reqwest_client = reqwest::Client::new();
let instance_id = match reqwest_client
Expand Down Expand Up @@ -1804,13 +1803,13 @@ where
})
}

fn setup_tracing(pid: u32) -> Option<WorkerGuard> {
fn setup_tracing(uuid: &Uuid) -> Option<WorkerGuard> {
use tracing_subscriber::prelude::*;
match std::env::var(LOG_DIR_PATH_ENV_NAME).map(std::path::PathBuf::from) {
Ok(p) => {
std::fs::create_dir_all(&p).expect("Could not create directory");

let file_name = format!("pocket_ic_client_{pid}.log");
let file_name = format!("pocket_ic_client_{uuid}.log");
let appender = tracing_appender::rolling::never(&p, file_name);
let (non_blocking_appender, guard) = tracing_appender::non_blocking(appender);
let log_dir_filter: EnvFilter =
Expand Down
6 changes: 3 additions & 3 deletions packages/pocket-ic/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use pocket_ic::{
RawEffectivePrincipal, RawMessageId, SubnetKind,
},
query_candid, update_candid, DefaultEffectiveCanisterIdError, ErrorCode, IngressStatusResult,
PocketIc, PocketIcBuilder, PocketIcState, RejectCode,
PocketIc, PocketIcBuilder, PocketIcState, RejectCode, TEST_DRIVER_UUID,
};
use reqwest::blocking::Client;
use reqwest::header::CONTENT_LENGTH;
Expand Down Expand Up @@ -1808,8 +1808,8 @@ fn test_get_default_effective_canister_id_invalid_url() {
.with_application_subnet()
.build();

let test_driver_pid = std::process::id();
let port_file_path = std::env::temp_dir().join(format!("pocket_ic_{}.port", test_driver_pid));
let port_file_path =
std::env::temp_dir().join(format!("pocket_ic_{}.port", &*TEST_DRIVER_UUID));
let port = std::fs::read_to_string(port_file_path).unwrap();

let server_url = format!("http://localhost:{}", port);
Expand Down
1 change: 1 addition & 0 deletions rs/pocket_ic_server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ TEST_DEPENDENCIES = [
"@crate_index//:slog",
"@crate_index//:tempfile",
"@crate_index//:tokio",
"@crate_index//:uuid",
"@crate_index//:wat",
]

Expand Down
1 change: 1 addition & 0 deletions rs/pocket_ic_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ tower-http = { workspace = true }
tracing = { workspace = true }
tracing-appender = { workspace = true }
tracing-subscriber = { workspace = true }
uuid = { workspace = true }
wat = { workspace = true }

[dev-dependencies]
Expand Down
12 changes: 6 additions & 6 deletions rs/pocket_ic_server/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use pocket_ic::common::rest::{
CreateHttpGatewayResponse, HttpGatewayBackend, HttpGatewayConfig, HttpGatewayDetails,
HttpsConfig, InstanceConfig, SubnetConfigSet, SubnetKind, Topology,
};
use pocket_ic::{update_candid, PocketIc, PocketIcBuilder, PocketIcState};
use pocket_ic::{update_candid, PocketIc, PocketIcBuilder, PocketIcState, TEST_DRIVER_UUID};
use rcgen::{CertificateParams, KeyPair};
use registry_canister::init::RegistryCanisterInitPayload;
use reqwest::blocking::Client;
Expand All @@ -30,18 +30,19 @@ use std::path::PathBuf;
use std::process::{Child, Command};
use std::time::Duration;
use tempfile::{NamedTempFile, TempDir};
use uuid::Uuid;

pub const LOCALHOST: &str = "127.0.0.1";

fn start_server_helper(
test_driver_pid: Option<u32>,
test_driver_uuid: Option<&Uuid>,
log_levels: Option<String>,
capture_stdout: bool,
capture_stderr: bool,
) -> (Url, Child) {
let bin_path = std::env::var_os("POCKET_IC_BIN").expect("Missing PocketIC binary");
let port_file_path = if let Some(test_driver_pid) = test_driver_pid {
std::env::temp_dir().join(format!("pocket_ic_{}.port", test_driver_pid))
let port_file_path = if let Some(test_driver_uuid) = test_driver_uuid {
std::env::temp_dir().join(format!("pocket_ic_{}.port", test_driver_uuid))
} else {
NamedTempFile::new().unwrap().into_temp_path().to_path_buf()
};
Expand Down Expand Up @@ -77,8 +78,7 @@ fn start_server_helper(
}

pub fn start_server() -> Url {
let test_driver_pid = std::process::id();
start_server_helper(Some(test_driver_pid), None, false, false).0
start_server_helper(Some(&TEST_DRIVER_UUID), None, false, false).0
}

const COUNTER_WAT: &str = r#"
Expand Down