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

fix(test): Fix iota-storage::key_value_tests simtests::test_multi_fetch #3784

Merged
merged 7 commits into from
Oct 31, 2024
54 changes: 24 additions & 30 deletions crates/iota-storage/tests/key_value_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ async fn test_get_tx_from_fallback() {
#[cfg(msim)]
mod simtests {
use std::{
net::SocketAddr,
sync::Mutex,
time::{Duration, Instant},
};
Expand All @@ -439,60 +440,53 @@ mod simtests {
use iota_simulator::configs::constant_latency_ms;
use iota_storage::http_key_value_store::*;
use rustls::crypto::{CryptoProvider, ring};
use tokio::net::TcpListener;
use tracing::info;

use super::*;

async fn svc(
State(state): State<Arc<Mutex<HashMap<String, Vec<u8>>>>>,
request: Request<Body>,
) -> Response {
let path = request.uri().path().to_string();
let key = path.trim_start_matches('/');
let value = state.lock().unwrap().get(key).cloned();
info!("Got request for key: {:?}, value: {:?}", key, value);
match value {
Some(v) => Response::new(Body::from(v)),
None => Response::builder()
.status(hyper::StatusCode::NOT_FOUND)
.body(Body::empty())
.unwrap(),
}
}

async fn test_server(data: Arc<Mutex<HashMap<String, Vec<u8>>>>) {
let handle = iota_simulator::runtime::Handle::current();
let builder = handle.create_node();
let (startup_sender, mut startup_receiver) = tokio::sync::watch::channel(false);
let startup_sender = Arc::new(startup_sender);
let (sender, _) = tokio::sync::broadcast::channel::<()>(1);

async fn get_data(
data: State<Arc<Mutex<HashMap<String, Vec<u8>>>>>,
req: Request,
) -> Result<Vec<u8>, String> {
let path = req.uri().path().to_string();
let key = path.trim_start_matches('/');
let value = data.lock().unwrap().get(key).cloned();
info!("Got request for key: {:?}, value: {:?}", key, value);
value.ok_or_else(|| "no value".to_owned())
}

let sender_clone = sender.clone();
let _node = builder
.ip("10.10.10.10".parse().unwrap())
.name("server")
.init(move || {
info!("Server started");
let data = data.clone();
let startup_sender = startup_sender.clone();
let mut receiver = sender_clone.subscribe();
async move {
let app = axum::Router::new()
.route("/", get(get_data))
.with_state(data);

let addr = TcpListener::bind(("10.10.10.10", 8080)).await.unwrap();

tokio::spawn(async move {
axum::serve(addr, app)
.with_graceful_shutdown(async move {
receiver.recv().await.ok();
})
.await
.unwrap()
let router = get(svc).with_state(data);
let addr = SocketAddr::from(([10, 10, 10, 10], 8080));
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();

tokio::spawn(async {
axum::serve(listener, router).await.unwrap();
});

startup_sender.send(true).ok();
}
})
.build();
startup_receiver.changed().await.unwrap();
sender.send(()).ok();
}

#[sim_test(config = "constant_latency_ms(250)")]
Expand Down
Loading