From d2f15ca1cc942b8830644cce0cfcbe9c67e8da11 Mon Sep 17 00:00:00 2001 From: Martin Raszyk Date: Fri, 25 Oct 2024 11:16:46 +0200 Subject: [PATCH] spawning_block --- packages/pocket-ic/tests/tests.rs | 4 ++-- rs/pocket_ic_server/src/pocket_ic.rs | 6 +++--- rs/pocket_ic_server/src/state_api/routes.rs | 4 ++-- rs/pocket_ic_server/src/state_api/state.rs | 24 ++++++++++----------- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/pocket-ic/tests/tests.rs b/packages/pocket-ic/tests/tests.rs index f6e4259704a..a26aeb65de2 100644 --- a/packages/pocket-ic/tests/tests.rs +++ b/packages/pocket-ic/tests/tests.rs @@ -378,7 +378,7 @@ fn test_get_set_cycle_balance() { assert_eq!(balance, initial_balance + 420); } -//#[test] +#[test] fn test_create_and_drop_instances() { let pic = PocketIc::new(); let id = pic.instance_id(); @@ -622,7 +622,7 @@ fn test_too_large_call() { .unwrap_err(); } -//#[tokio::test] +#[tokio::test] async fn test_create_and_drop_instances_async() { let pic = pocket_ic::nonblocking::PocketIc::new().await; let id = pic.instance_id; diff --git a/rs/pocket_ic_server/src/pocket_ic.rs b/rs/pocket_ic_server/src/pocket_ic.rs index 4dc13301948..f93d5496556 100644 --- a/rs/pocket_ic_server/src/pocket_ic.rs +++ b/rs/pocket_ic_server/src/pocket_ic.rs @@ -1,6 +1,6 @@ #![allow(clippy::disallowed_types)] use crate::state_api::state::{HasStateLabel, OpOut, PocketIcError, StateLabel}; -use crate::{async_trait, copy_dir, BlobStore, InstanceId, OpId, Operation}; +use crate::{async_trait, copy_dir, BlobStore, OpId, Operation}; use askama::Template; use axum::{ extract::State, @@ -386,7 +386,7 @@ impl PocketIc { pub(crate) fn new( runtime: Arc, - instance_id: InstanceId, + seed: u64, subnet_configs: ExtendedSubnetConfigSet, state_dir: Option, nonmainnet_features: bool, @@ -688,7 +688,7 @@ impl PocketIc { default_effective_canister_id, }; - let state_label = StateLabel::new(instance_id); + let state_label = StateLabel::new(seed); Self { state_dir, diff --git a/rs/pocket_ic_server/src/state_api/routes.rs b/rs/pocket_ic_server/src/state_api/routes.rs index 27b1c5b09d2..a2d196f5cf0 100644 --- a/rs/pocket_ic_server/src/state_api/routes.rs +++ b/rs/pocket_ic_server/src/state_api/routes.rs @@ -1113,10 +1113,10 @@ pub async fn create_instance( }; let (instance_id, topology) = api_state - .add_instance(move |instance_id| { + .add_instance(move |seed| { PocketIc::new( runtime, - instance_id, + seed, subnet_configs, instance_config.state_dir, instance_config.nonmainnet_features, diff --git a/rs/pocket_ic_server/src/state_api/state.rs b/rs/pocket_ic_server/src/state_api/state.rs index 70cae4418f3..2e72c7e254c 100644 --- a/rs/pocket_ic_server/src/state_api/state.rs +++ b/rs/pocket_ic_server/src/state_api/state.rs @@ -52,7 +52,7 @@ use pocket_ic::common::rest::{ }; use pocket_ic::{ErrorCode, UserError, WasmResult}; use serde::{Deserialize, Serialize}; -use std::{collections::HashMap, fmt, path::PathBuf, str::FromStr, sync::Arc, time::Duration}; +use std::{collections::HashMap, fmt, path::PathBuf, str::FromStr, sync::Arc, sync::atomic::AtomicU64, time::Duration}; use tokio::{ sync::mpsc::error::TryRecvError, sync::mpsc::Receiver, @@ -81,8 +81,8 @@ pub const STATE_LABEL_HASH_SIZE: usize = 16; pub struct StateLabel(pub [u8; STATE_LABEL_HASH_SIZE]); impl StateLabel { - pub fn new(instance_id: InstanceId) -> Self { - let mut seq_no: u128 = instance_id.try_into().unwrap(); + pub fn new(seed: u64) -> Self { + let mut seq_no: u128 = seed.try_into().unwrap(); seq_no <<= 64; Self(seq_no.to_le_bytes()) } @@ -142,6 +142,7 @@ pub struct ApiState { // impl note: If locks are acquired on both fields, acquire first on `instances` and then on `graph`. instances: Arc>>>, graph: Arc>>, + seed: AtomicU64, sync_wait_time: Duration, // PocketIC server port port: Option, @@ -208,6 +209,7 @@ impl PocketIcApiStateBuilder { Arc::new(ApiState { instances, graph, + seed: AtomicU64::new(0), sync_wait_time, port: self.port, http_gateways: Arc::new(RwLock::new(Vec::new())), @@ -727,17 +729,18 @@ impl ApiState { pub async fn add_instance(&self, f: F) -> (InstanceId, Topology) where - F: FnOnce(InstanceId) -> PocketIc + std::marker::Send + 'static, + F: FnOnce(u64) -> PocketIc + std::marker::Send + 'static, { + let seed = self.seed.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let instance = tokio::task::spawn_blocking(move || f(seed)) + .await + .expect("Failed to create PocketIC instance"); + let topology = instance.topology(); trace!("add_instance:start"); let mut instances = self.instances.lock().await; trace!("add_instance:locked"); let instance_id = instances.len(); - let instance = tokio::task::spawn_blocking(move || f(instance_id)) - .await - .expect("Failed to create PocketIC instance"); trace!("add_instance:done_blocking"); - let topology = instance.topology(); instances.push(Mutex::new(Instance { progress_thread: None, state: InstanceState::Available(instance), @@ -1308,7 +1311,6 @@ impl ApiState { } pub async fn list_instance_states(&self) -> Vec { - panic!(""); let instances = self.instances.lock().await; let mut res = vec![]; @@ -1542,8 +1544,7 @@ impl std::fmt::Debug for InstanceState { impl std::fmt::Debug for ApiState { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - /* - let instances = self.instances.blocking_read(); + let instances = self.instances.blocking_lock(); let graph = self.graph.blocking_read(); writeln!(f, "Instances:")?; @@ -1555,7 +1556,6 @@ impl std::fmt::Debug for ApiState { for (k, v) in graph.iter() { writeln!(f, " {k:?} => {v:?}")?; } - */ Ok(()) } }