Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(gtest): make gtest thread safe #4032

Closed
wants to merge 6 commits into from
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
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion examples/waiter/tests/mx_lock_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn access_mx_lock_guard_from_different_msg_fails(
));
}

fn init_fixture(system: &System) -> (Program<'_>, MessageId) {
fn init_fixture(system: &System) -> (Program, MessageId) {
system.init_logger_with_default_filter("");
let program = Program::current(system);
program.send_bytes(USER_ID, []);
Expand Down
2 changes: 1 addition & 1 deletion examples/waiter/tests/rw_lock_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn access_rw_lock_guard_from_different_msg_fails(
));
}

fn init_fixture(system: &System, lock_type: RwLockType) -> (Program<'_>, MessageId) {
fn init_fixture(system: &System, lock_type: RwLockType) -> (Program, MessageId) {
system.init_logger_with_default_filter("");
let program = Program::current(system);
program.send_bytes(USER_ID, []);
Expand Down
2 changes: 2 additions & 0 deletions gtest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ log.workspace = true
indexmap.workspace = true
cargo_toml.workspace = true
etc.workspace = true
parking_lot.workspace = true
once_cell.workspace = true

[dev-dependencies]
demo-custom.workspace = true
Expand Down
84 changes: 36 additions & 48 deletions gtest/src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,18 @@

//! Block timestamp and height management.

use crate::BLOCK_DURATION_IN_MSECS;
use core_processor::configs::BlockInfo;
use once_cell::sync::Lazy;
use parking_lot::RwLock;
use std::{
cell::RefCell,
rc::Rc,
sync::Arc,
time::{SystemTime, UNIX_EPOCH},
};

use crate::BLOCK_DURATION_IN_MSECS;

type BlockInfoStorageInner = Rc<RefCell<Option<BlockInfo>>>;
type BlockInfoStorageInner = Arc<RwLock<Option<BlockInfo>>>;

thread_local! {
/// Definition of the storage value storing block info (timestamp and height).
static BLOCK_INFO_STORAGE: BlockInfoStorageInner = Rc::new(RefCell::new(None));
}
static BLOCK_INFO_STORAGE: Lazy<BlockInfoStorageInner> = Lazy::new(|| Arc::new(RwLock::new(None)));

#[derive(Debug)]
pub(crate) struct BlocksManager {
Expand All @@ -43,32 +40,27 @@ impl BlocksManager {
/// Create block info storage manager with a further initialization of the
/// storage.
pub(crate) fn new() -> Self {
let unused = BLOCK_INFO_STORAGE.with(|bi_rc| {
let mut ref_mut = bi_rc.borrow_mut();
if ref_mut.is_none() {
let info = BlockInfo {
height: 0,
timestamp: now(),
};

*ref_mut = Some(info);
}
let mut bi = BLOCK_INFO_STORAGE.write();
if bi.is_none() {
let info = BlockInfo {
height: 0,
timestamp: now(),
};

Rc::clone(bi_rc)
});
*bi = Some(info);
}

Self { _unused: unused }
Self {
_unused: BLOCK_INFO_STORAGE.clone(),
}
}

/// Get current block info.
pub(crate) fn get(&self) -> BlockInfo {
BLOCK_INFO_STORAGE.with(|bi_rc| {
bi_rc
.borrow()
.as_ref()
.copied()
.expect("instance always initialized")
})
BLOCK_INFO_STORAGE
.read()
.clone()
.expect("instance always initialized")
}

/// Move blocks by one.
Expand All @@ -78,17 +70,15 @@ impl BlocksManager {

/// Adjusts blocks info by moving blocks by `amount`.
pub(crate) fn move_blocks_by(&self, amount: u32) -> BlockInfo {
BLOCK_INFO_STORAGE.with(|bi_rc| {
let mut bi_ref_mut = bi_rc.borrow_mut();
let Some(block_info) = bi_ref_mut.as_mut() else {
panic!("instance always initialized");
};
block_info.height += amount;
let duration = BLOCK_DURATION_IN_MSECS.saturating_mul(amount as u64);
block_info.timestamp += duration;

*block_info
})
let mut bi_ref_mut = BLOCK_INFO_STORAGE.write();
let Some(block_info) = bi_ref_mut.as_mut() else {
panic!("instance always initialized");
};
block_info.height += amount;
let duration = BLOCK_DURATION_IN_MSECS.saturating_mul(amount as u64);
block_info.timestamp += duration;

*block_info
}
}

Expand All @@ -100,11 +90,9 @@ impl Default for BlocksManager {

impl Drop for BlocksManager {
fn drop(&mut self) {
BLOCK_INFO_STORAGE.with(|bi_rc| {
if Rc::strong_count(bi_rc) == 2 {
*bi_rc.borrow_mut() = None;
}
});
if Arc::strong_count(&BLOCK_INFO_STORAGE) == 2 {
*BLOCK_INFO_STORAGE.write() = None;
}
}
}

Expand All @@ -129,17 +117,17 @@ mod tests {

// Assert all instance use same data;
assert_eq!(second_instance.get().height, 2);
BLOCK_INFO_STORAGE.with(|bi_rc| bi_rc.borrow().is_some());
assert!(BLOCK_INFO_STORAGE.read().is_some());

// Drop first instance and check whether data is removed.
drop(first_instance);
assert_eq!(second_instance.get().height, 2);

second_instance.next_block();
assert_eq!(second_instance.get().height, 3);
BLOCK_INFO_STORAGE.with(|bi_rc| bi_rc.borrow().is_some());
assert!(BLOCK_INFO_STORAGE.read().is_some());

drop(second_instance);
BLOCK_INFO_STORAGE.with(|bi_rc| bi_rc.borrow().is_none());
assert!(BLOCK_INFO_STORAGE.read().is_none());
}
}
24 changes: 11 additions & 13 deletions gtest/src/mailbox/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,20 @@ use gear_core::{
ids::{prelude::MessageIdExt, MessageId, ProgramId},
message::{ReplyMessage, ReplyPacket},
};
use std::{cell::RefCell, convert::TryInto};
use parking_lot::RwLock;
use std::{convert::TryInto, sync::Arc};

/// Interface to a particular user mailbox.
///
/// Gives a simplified interface to perform some operations
/// over a particular user mailbox.
pub struct ActorMailbox<'a> {
manager: &'a RefCell<ExtManager>,
pub struct ActorMailbox {
manager: Arc<RwLock<ExtManager>>,
user_id: ProgramId,
}

impl<'a> ActorMailbox<'a> {
pub(crate) fn new(user_id: ProgramId, manager: &'a RefCell<ExtManager>) -> ActorMailbox<'a> {
impl ActorMailbox {
pub(crate) fn new(user_id: ProgramId, manager: Arc<RwLock<ExtManager>>) -> ActorMailbox {
ActorMailbox { user_id, manager }
}

Expand Down Expand Up @@ -70,7 +71,7 @@ impl<'a> ActorMailbox<'a> {
.find_message_by_log(&log)
.ok_or(MailboxErrorImpl::ElementNotFound)?;
self.manager
.borrow()
.read()
.mailbox
.remove(self.user_id, mailboxed_msg.id())?;

Expand All @@ -90,10 +91,7 @@ impl<'a> ActorMailbox<'a> {
reply_message.into_dispatch(self.user_id, mailboxed_msg.source(), mailboxed_msg.id())
};

Ok(self
.manager
.borrow_mut()
.validate_and_run_dispatch(dispatch))
Ok(self.manager.write().validate_and_run_dispatch(dispatch))
}

/// Claims value from a message in mailbox.
Expand All @@ -105,7 +103,7 @@ impl<'a> ActorMailbox<'a> {
.find_message_by_log(&log.into())
.ok_or(MailboxErrorImpl::ElementNotFound)?;
self.manager
.borrow_mut()
.write()
.claim_value_from_mailbox(self.user_id, mailboxed_msg.id())
.unwrap_or_else(|e| unreachable!("Unexpected mailbox error: {e:?}"));

Expand All @@ -118,7 +116,7 @@ impl<'a> ActorMailbox<'a> {
}

fn get_user_mailbox(&self) -> impl Iterator<Item = (MailboxedMessage, Interval<BlockNumber>)> {
self.manager.borrow().mailbox.iter_key(self.user_id)
self.manager.read().mailbox.iter_key(self.user_id)
}
}

Expand All @@ -136,7 +134,7 @@ mod tests {
};
use std::convert::TryInto;

fn prepare_program(system: &System) -> (Program<'_>, ([u8; 32], Vec<u8>, Log)) {
fn prepare_program(system: &System) -> (Program, ([u8; 32], Vec<u8>, Log)) {
let program = Program::from_binary_with_id(system, 121, WASM_BINARY);

let sender = ProgramId::from(42).into_bytes();
Expand Down
24 changes: 14 additions & 10 deletions gtest/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,19 @@ use gear_core_errors::{ErrorReplyReason, SignalCode, SimpleExecutionError};
use gear_lazy_pages_common::LazyPagesCosts;
use gear_lazy_pages_native_interface::LazyPagesNative;
use gear_wasm_instrument::gas_metering::Schedule;
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use rand::{rngs::StdRng, RngCore, SeedableRng};
use std::{
cell::{Ref, RefCell, RefMut},
collections::{BTreeMap, HashMap, VecDeque},
convert::TryInto,
rc::Rc,
sync::Arc,
};

const OUTGOING_LIMIT: u32 = 1024;
const OUTGOING_BYTES_LIMIT: u32 = 64 * 1024 * 1024;

pub(crate) type ExtManagerPointer = Arc<RwLock<ExtManager>>;

pub(crate) type Balance = u128;

#[derive(Debug)]
Expand Down Expand Up @@ -185,31 +187,33 @@ impl Program {
}

#[derive(Default, Debug, Clone)]
pub(crate) struct Actors(Rc<RefCell<BTreeMap<ProgramId, (TestActor, Balance)>>>);
pub(crate) struct Actors(Arc<RwLock<BTreeMap<ProgramId, (TestActor, Balance)>>>);

impl Actors {
pub fn borrow(&self) -> Ref<'_, BTreeMap<ProgramId, (TestActor, Balance)>> {
self.0.borrow()
pub fn borrow(&self) -> RwLockReadGuard<'_, BTreeMap<ProgramId, (TestActor, Balance)>> {
self.0.read()
}

pub fn borrow_mut(&mut self) -> RefMut<'_, BTreeMap<ProgramId, (TestActor, Balance)>> {
self.0.borrow_mut()
pub fn borrow_mut(
&mut self,
) -> RwLockWriteGuard<'_, BTreeMap<ProgramId, (TestActor, Balance)>> {
self.0.write()
}

fn insert(
&mut self,
program_id: ProgramId,
actor_and_balance: (TestActor, Balance),
) -> Option<(TestActor, Balance)> {
self.0.borrow_mut().insert(program_id, actor_and_balance)
self.0.write().insert(program_id, actor_and_balance)
}

pub fn contains_key(&self, program_id: &ProgramId) -> bool {
self.0.borrow().contains_key(program_id)
self.0.read().contains_key(program_id)
}

fn remove(&mut self, program_id: &ProgramId) -> Option<(TestActor, Balance)> {
self.0.borrow_mut().remove(program_id)
self.0.write().remove(program_id)
}
}

Expand Down
Loading
Loading