Skip to content

Commit

Permalink
spawning_block
Browse files Browse the repository at this point in the history
  • Loading branch information
mraszyk committed Oct 25, 2024
1 parent 9a127d7 commit d2f15ca
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages/pocket-ic/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions rs/pocket_ic_server/src/pocket_ic.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -386,7 +386,7 @@ impl PocketIc {

pub(crate) fn new(
runtime: Arc<Runtime>,
instance_id: InstanceId,
seed: u64,
subnet_configs: ExtendedSubnetConfigSet,
state_dir: Option<PathBuf>,
nonmainnet_features: bool,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions rs/pocket_ic_server/src/state_api/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 12 additions & 12 deletions rs/pocket_ic_server/src/state_api/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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<Mutex<Vec<Mutex<Instance>>>>,
graph: Arc<RwLock<HashMap<StateLabel, Computations>>>,
seed: AtomicU64,
sync_wait_time: Duration,
// PocketIC server port
port: Option<u16>,
Expand Down Expand Up @@ -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())),
Expand Down Expand Up @@ -727,17 +729,18 @@ impl ApiState {

pub async fn add_instance<F>(&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),
Expand Down Expand Up @@ -1308,7 +1311,6 @@ impl ApiState {
}

pub async fn list_instance_states(&self) -> Vec<String> {
panic!("");
let instances = self.instances.lock().await;
let mut res = vec![];

Expand Down Expand Up @@ -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:")?;
Expand All @@ -1555,7 +1556,6 @@ impl std::fmt::Debug for ApiState {
for (k, v) in graph.iter() {
writeln!(f, " {k:?} => {v:?}")?;
}
*/
Ok(())
}
}

0 comments on commit d2f15ca

Please sign in to comment.