2023-06-29 22:24:36 +03:00
|
|
|
mod common;
|
|
|
|
|
2023-07-13 00:23:39 +03:00
|
|
|
use crate::common::{anvil::TestAnvil, mysql::TestMysql, TestApp};
|
2023-06-29 22:24:36 +03:00
|
|
|
use ethers::prelude::U256;
|
2023-06-29 23:12:35 +03:00
|
|
|
use http::StatusCode;
|
2023-06-29 22:24:36 +03:00
|
|
|
use std::time::Duration;
|
2023-06-29 23:12:35 +03:00
|
|
|
use tokio::{
|
|
|
|
task::yield_now,
|
|
|
|
time::{sleep, Instant},
|
|
|
|
};
|
2023-06-29 22:24:36 +03:00
|
|
|
use web3_proxy::rpcs::blockchain::ArcBlock;
|
|
|
|
|
2023-07-06 05:24:21 +03:00
|
|
|
#[cfg_attr(not(feature = "tests-needing-docker"), ignore)]
|
2023-06-30 07:28:31 +03:00
|
|
|
#[test_log::test(tokio::test)]
|
|
|
|
async fn it_migrates_the_db() {
|
2023-07-13 00:23:39 +03:00
|
|
|
let a = TestAnvil::spawn(31337).await;
|
|
|
|
let db = TestMysql::spawn().await;
|
|
|
|
|
2023-07-22 11:38:54 +03:00
|
|
|
let x = TestApp::spawn(&a, Some(&db), None, None).await;
|
2023-07-06 05:24:21 +03:00
|
|
|
|
|
|
|
// we call flush stats more to be sure it works than because we expect it to save any stats
|
|
|
|
x.flush_stats().await.unwrap();
|
2023-07-22 02:18:33 +03:00
|
|
|
|
|
|
|
// drop x first to avoid spurious warnings about anvil/influx/mysql shutting down before the app
|
|
|
|
drop(x);
|
2023-06-30 07:28:31 +03:00
|
|
|
}
|
|
|
|
|
2023-06-29 22:24:36 +03:00
|
|
|
#[test_log::test(tokio::test)]
|
2023-06-29 22:41:21 +03:00
|
|
|
async fn it_starts_and_stops() {
|
2023-07-13 00:23:39 +03:00
|
|
|
let a = TestAnvil::spawn(31337).await;
|
|
|
|
|
2023-07-22 11:38:54 +03:00
|
|
|
let x = TestApp::spawn(&a, None, None, None).await;
|
2023-06-29 22:24:36 +03:00
|
|
|
|
2023-07-13 00:42:47 +03:00
|
|
|
let anvil_provider = &a.provider;
|
2023-06-29 22:24:36 +03:00
|
|
|
let proxy_provider = &x.proxy_provider;
|
|
|
|
|
|
|
|
let anvil_result = anvil_provider
|
|
|
|
.request::<_, Option<ArcBlock>>("eth_getBlockByNumber", ("latest", false))
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
let proxy_result = proxy_provider
|
|
|
|
.request::<_, Option<ArcBlock>>("eth_getBlockByNumber", ("latest", false))
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(anvil_result, proxy_result);
|
|
|
|
|
2023-06-29 23:12:35 +03:00
|
|
|
// check the /health page
|
|
|
|
let proxy_url = x.proxy_provider.url();
|
|
|
|
let health_response = reqwest::get(format!("{}health", proxy_url)).await;
|
|
|
|
dbg!(&health_response);
|
|
|
|
assert_eq!(health_response.unwrap().status(), StatusCode::OK);
|
|
|
|
|
|
|
|
// check the /status page
|
|
|
|
let status_response = reqwest::get(format!("{}status", proxy_url)).await;
|
|
|
|
dbg!(&status_response);
|
|
|
|
assert_eq!(status_response.unwrap().status(), StatusCode::OK);
|
|
|
|
|
2023-06-29 22:24:36 +03:00
|
|
|
let first_block_num = anvil_result.number.unwrap();
|
|
|
|
|
|
|
|
// mine a block
|
|
|
|
let _: U256 = anvil_provider.request("evm_mine", ()).await.unwrap();
|
|
|
|
|
|
|
|
// make sure the block advanced
|
|
|
|
let anvil_result = anvil_provider
|
|
|
|
.request::<_, Option<ArcBlock>>("eth_getBlockByNumber", ("latest", false))
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let second_block_num = anvil_result.number.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(first_block_num, second_block_num - 1);
|
|
|
|
|
2023-06-29 23:12:35 +03:00
|
|
|
yield_now().await;
|
|
|
|
|
2023-06-29 22:24:36 +03:00
|
|
|
let mut proxy_result;
|
|
|
|
let start = Instant::now();
|
|
|
|
loop {
|
|
|
|
if start.elapsed() > Duration::from_secs(1) {
|
|
|
|
panic!("took too long to sync!");
|
|
|
|
}
|
|
|
|
|
|
|
|
proxy_result = proxy_provider
|
|
|
|
.request::<_, Option<ArcBlock>>("eth_getBlockByNumber", ("latest", false))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
if let Some(ref proxy_result) = proxy_result {
|
2023-06-29 23:12:35 +03:00
|
|
|
if proxy_result.number == Some(second_block_num) {
|
2023-06-29 22:24:36 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep(Duration::from_millis(10)).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(anvil_result, proxy_result.unwrap());
|
|
|
|
|
2023-07-06 05:24:21 +03:00
|
|
|
// this won't do anything since stats aren't tracked when there isn't a db
|
2023-07-15 04:30:01 +03:00
|
|
|
let flushed = x.flush_stats().await.unwrap();
|
|
|
|
assert_eq!(flushed.relational, 0);
|
|
|
|
assert_eq!(flushed.timeseries, 0);
|
2023-07-06 05:24:21 +03:00
|
|
|
|
2023-06-29 22:41:21 +03:00
|
|
|
// most tests won't need to wait, but we should wait here to be sure all the shutdown logic works properly
|
2023-07-15 04:30:01 +03:00
|
|
|
x.wait_for_stop();
|
2023-06-29 22:24:36 +03:00
|
|
|
}
|