Skip to content

Commit

Permalink
fix token test
Browse files Browse the repository at this point in the history
  • Loading branch information
Autoparallel committed Apr 17, 2024
1 parent 45a5bfb commit aaebac4
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 87 deletions.
2 changes: 1 addition & 1 deletion kit/src/behaviors/allocate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

trait AllocateType<E>: Debug + Serialize + Clone
pub trait AllocateType<E>: Debug + Serialize + Clone
where
E: Send + 'static,
{
Expand Down
7 changes: 3 additions & 4 deletions kit/src/behaviors/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Behavior<Message> for TokenAdmin<Config> {
messager: Messager,
) -> Result<Option<(Self::Processor, EventStream<Message>)>> {
let mut tokens = HashMap::new();
for token_data in &self.data.token_data.clone() {
for token_data in self.data.token_data.drain(..) {
let token = ArbiterToken::deploy(
client.clone(),
(
Expand All @@ -51,10 +51,10 @@ impl Behavior<Message> for TokenAdmin<Config> {
.send()
.await
.unwrap();
tokens.insert(token_data.name.clone(), (token_data.clone(), token));
tokens.insert(token_data.name.clone(), (token_data, token));
}

debug!("Tokens deployed {:?}", tokens);
debug!("Tokens deployed {:#?}", tokens);

let process = Self::Processor {
data: Processing {
Expand All @@ -65,7 +65,6 @@ impl Behavior<Message> for TokenAdmin<Config> {
};

let stream = process.data.messager.clone().stream()?;
debug!("Token Admin completed");
Ok(Some((process, stream)))
}
}
Expand Down
8 changes: 4 additions & 4 deletions kit/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use dfmm_kit::{
},
TokenData,
};
use ethers::types::{Address, U256 as eU256};
use ethers::types::{Address as eAddress, U256 as eU256};
use tracing::Level;
use tracing_subscriber::FmtSubscriber;

Expand All @@ -38,10 +38,10 @@ pub const RESERVE_Y: eU256 = WAD;

pub const TARGET_TIMESTAMP: eU256 = WAD;

pub fn log() {
pub fn log(level: Level) {
tracing::subscriber::set_global_default(
FmtSubscriber::builder()
.with_max_level(Level::DEBUG)
.with_max_level(level)
.pretty()
.finish(),
)
Expand Down Expand Up @@ -81,7 +81,7 @@ pub fn spawn_constant_sum_creator(world: &mut World) {
params: ConstantSumParams {
price: PRICE,
swap_fee: ethers::utils::parse_ether(0.003).unwrap(),
controller: Address::zero(),
controller: eAddress::zero(),
},
token_list: vec![TOKEN_X_NAME.to_owned(), TOKEN_Y_NAME.to_owned()],
base_config: BaseConfig {
Expand Down
4 changes: 2 additions & 2 deletions kit/tests/creator_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ include!("common.rs");

#[tokio::test(flavor = "multi_thread", worker_threads = 5)]
async fn run_creator_constant_sum() {
log();
log(Level::DEBUG);

let mut world = World::new("test");
let mut messager = world.messager.clone();
Expand All @@ -29,7 +29,7 @@ async fn run_creator_constant_sum() {
params: ConstantSumParams {
price: WAD,
swap_fee: ethers::utils::parse_ether(0.003).unwrap(),
controller: Address::zero(),
controller: eAddress::zero(),
},
allocation_data: ConstantSumAllocationData {
reserve_x: RESERVE_X,
Expand Down
3 changes: 1 addition & 2 deletions kit/tests/deployer.rs → kit/tests/deploy_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ include!("common.rs");
use std::str::FromStr;

use dfmm_kit::behaviors::deploy::DeploymentData;
use ethers::types::Address as eAddress;
use tracing::info;

#[tokio::test(flavor = "multi_thread", worker_threads = 5)]
async fn run_deployer() {
log();
log(Level::DEBUG);

let mut world = World::new("test");
let mut messager = world.messager.clone();
Expand Down
74 changes: 0 additions & 74 deletions kit/tests/token_admin_integration.rs

This file was deleted.

86 changes: 86 additions & 0 deletions kit/tests/token_integration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use std::time::Duration;

use arbiter_engine::messager::To;
use dfmm_kit::behaviors::TokenAdminQuery;
use futures_util::StreamExt;
use tracing::info;

include!("common.rs");

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn run_token_admin() {
log(Level::DEBUG);

let mut world = World::new("test");
let messager = world.messager.for_agent("test");

spawn_deployer(&mut world);
spawn_token_admin(&mut world);

let world_task = world.run();

let task: tokio::task::JoinHandle<()> = tokio::spawn(async move {
// Sleep because the world needs to give all of the agents time to build their
// receivers. TODO: This is a bit of a hack and we could honestly make
// the `World::run` better to handle this, but this works for now.
tokio::time::sleep(Duration::from_millis(2000)).await;
messager
.send(
To::Agent(TOKEN_ADMIN.to_owned()),
TokenAdminQuery::GetAssetUniverse,
)
.await
.unwrap();
let mut stream = messager.stream().unwrap();
while let Some(message) = stream.next().await {
info!("Saw message: {:#?}", message);
match serde_json::from_str::<Vec<(TokenData, eAddress)>>(&message.data) {
Ok(data) => {
info!("Saw data: {:#?}", data);
let token_x = TokenData {
name: TOKEN_X_NAME.to_owned(),
symbol: TOKEN_X_SYMBOL.to_owned(),
decimals: TOKEN_X_DECIMALS,
address: None,
};
let token_y = TokenData {
name: TOKEN_Y_NAME.to_owned(),
symbol: TOKEN_Y_SYMBOL.to_owned(),
decimals: TOKEN_Y_DECIMALS,
address: None,
};
let mock_data = token::Config {
token_data: vec![token_x, token_y],
};
assert_eq!(data[0].0.name, mock_data.token_data[0].name);
assert_eq!(data[0].0.symbol, mock_data.token_data[0].symbol);
assert_eq!(data[1].0.name, mock_data.token_data[1].name);
assert_eq!(data[1].0.symbol, mock_data.token_data[1].symbol);
info!("Asserts passed!");
break;
}
Err(_) => {
continue;
}
}
}
});

// Setup a timeout for the test to ensure it does not run indefinitely.
let timeout_duration = Duration::from_secs(10); // Adjust the timeout as needed.

tokio::select! {
_ = world_task => {
panic!("World run unexpectedly completed");
},
result = task => {
match result {
Ok(_) => println!("Task completed successfully and test should pass."),
Err(e) => panic!("Task encountered an error: {:?}", e),
}
},
_ = tokio::time::sleep(timeout_duration) => {
panic!("Test timed out");
}
}
}

0 comments on commit aaebac4

Please sign in to comment.