Use async tests

This commit is contained in:
Paul Hauner
2023-07-10 18:30:46 +10:00
parent ad65cf65f3
commit 8d3aa4419b

View File

@@ -31,7 +31,7 @@ use std::net::{IpAddr, Ipv4Addr};
use std::str::FromStr; use std::str::FromStr;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use task_executor::TaskExecutor; use task_executor::test_utils::TestRuntime;
use tempfile::{tempdir, TempDir}; use tempfile::{tempdir, TempDir};
use tokio::runtime::Runtime; use tokio::runtime::Runtime;
use tokio::sync::oneshot; use tokio::sync::oneshot;
@@ -48,9 +48,8 @@ struct ApiTester {
validator_store: Arc<ValidatorStore<TestingSlotClock, E>>, validator_store: Arc<ValidatorStore<TestingSlotClock, E>>,
url: SensitiveUrl, url: SensitiveUrl,
slot_clock: TestingSlotClock, slot_clock: TestingSlotClock,
_server_shutdown: oneshot::Sender<()>,
_validator_dir: TempDir, _validator_dir: TempDir,
_runtime_shutdown: exit_future::Signal, _test_runtime: TestRuntime,
} }
// Builds a runtime to be used in the testing configuration. // Builds a runtime to be used in the testing configuration.
@@ -64,7 +63,7 @@ fn build_runtime() -> Arc<Runtime> {
} }
impl ApiTester { impl ApiTester {
pub async fn new(runtime: std::sync::Weak<Runtime>) -> Self { pub async fn new() -> Self {
let log = test_logger(); let log = test_logger();
let validator_dir = tempdir().unwrap(); let validator_dir = tempdir().unwrap();
@@ -100,9 +99,7 @@ impl ApiTester {
Duration::from_secs(1), Duration::from_secs(1),
); );
let (runtime_shutdown, exit) = exit_future::signal(); let test_runtime = TestRuntime::default();
let (shutdown_tx, _) = futures::channel::mpsc::channel(1);
let executor = TaskExecutor::new(runtime.clone(), exit, log.clone(), shutdown_tx);
let validator_store = Arc::new(ValidatorStore::<_, E>::new( let validator_store = Arc::new(ValidatorStore::<_, E>::new(
initialized_validators, initialized_validators,
@@ -112,7 +109,7 @@ impl ApiTester {
Some(Arc::new(DoppelgangerService::new(log.clone()))), Some(Arc::new(DoppelgangerService::new(log.clone()))),
slot_clock.clone(), slot_clock.clone(),
&config, &config,
executor.clone(), test_runtime.task_executor.clone(),
log.clone(), log.clone(),
)); ));
@@ -123,7 +120,7 @@ impl ApiTester {
let initialized_validators = validator_store.initialized_validators(); let initialized_validators = validator_store.initialized_validators();
let context = Arc::new(Context { let context = Arc::new(Context {
task_executor: executor, task_executor: test_runtime.task_executor.clone(),
api_secret, api_secret,
validator_dir: Some(validator_dir.path().into()), validator_dir: Some(validator_dir.path().into()),
secrets_dir: Some(secrets_dir.path().into()), secrets_dir: Some(secrets_dir.path().into()),
@@ -145,12 +142,8 @@ impl ApiTester {
_phantom: PhantomData, _phantom: PhantomData,
}); });
let ctx = context.clone(); let ctx = context.clone();
let (shutdown_tx, shutdown_rx) = oneshot::channel(); let (listening_socket, server) =
let server_shutdown = async { super::serve(ctx, test_runtime.task_executor.exit()).unwrap();
// It's not really interesting why this triggered, just that it happened.
let _ = shutdown_rx.await;
};
let (listening_socket, server) = super::serve(ctx, server_shutdown).unwrap();
tokio::spawn(async { server.await }); tokio::spawn(async { server.await });
@@ -169,9 +162,8 @@ impl ApiTester {
validator_store, validator_store,
url, url,
slot_clock, slot_clock,
_server_shutdown: shutdown_tx,
_validator_dir: validator_dir, _validator_dir: validator_dir,
_runtime_shutdown: runtime_shutdown, _test_runtime: test_runtime,
} }
} }
@@ -679,25 +671,18 @@ struct Web3SignerValidatorScenario {
enabled: bool, enabled: bool,
} }
#[test] #[tokio::test]
fn invalid_pubkey() { async fn invalid_pubkey() {
let runtime = build_runtime(); ApiTester::new()
let weak_runtime = Arc::downgrade(&runtime);
runtime.block_on(async {
ApiTester::new(weak_runtime)
.await .await
.invalidate_api_token() .invalidate_api_token()
.test_get_lighthouse_version_invalid() .test_get_lighthouse_version_invalid()
.await; .await;
});
} }
#[test] #[tokio::test]
fn routes_with_invalid_auth() { async fn routes_with_invalid_auth() {
let runtime = build_runtime(); ApiTester::new()
let weak_runtime = Arc::downgrade(&runtime);
runtime.block_on(async {
ApiTester::new(weak_runtime)
.await .await
.test_with_invalid_auth(|client| async move { client.get_lighthouse_version().await }) .test_with_invalid_auth(|client| async move { client.get_lighthouse_version().await })
.await .await
@@ -707,9 +692,7 @@ fn routes_with_invalid_auth() {
client.get_lighthouse_spec::<types::Config>().await client.get_lighthouse_spec::<types::Config>().await
}) })
.await .await
.test_with_invalid_auth( .test_with_invalid_auth(|client| async move { client.get_lighthouse_validators().await })
|client| async move { client.get_lighthouse_validators().await },
)
.await .await
.test_with_invalid_auth(|client| async move { .test_with_invalid_auth(|client| async move {
client client
@@ -800,16 +783,12 @@ fn routes_with_invalid_auth() {
}) })
.await .await
}) })
.await .await;
});
} }
#[test] #[tokio::test]
fn simple_getters() { async fn simple_getters() {
let runtime = build_runtime(); ApiTester::new()
let weak_runtime = Arc::downgrade(&runtime);
runtime.block_on(async {
ApiTester::new(weak_runtime)
.await .await
.test_get_lighthouse_version() .test_get_lighthouse_version()
.await .await
@@ -817,15 +796,11 @@ fn simple_getters() {
.await .await
.test_get_lighthouse_spec() .test_get_lighthouse_spec()
.await; .await;
});
} }
#[test] #[tokio::test]
fn hd_validator_creation() { async fn hd_validator_creation() {
let runtime = build_runtime(); ApiTester::new()
let weak_runtime = Arc::downgrade(&runtime);
runtime.block_on(async {
ApiTester::new(weak_runtime)
.await .await
.assert_enabled_validators_count(0) .assert_enabled_validators_count(0)
.assert_validators_count(0) .assert_validators_count(0)
@@ -856,15 +831,11 @@ fn hd_validator_creation() {
.await .await
.assert_enabled_validators_count(2) .assert_enabled_validators_count(2)
.assert_validators_count(3); .assert_validators_count(3);
});
} }
#[test] #[tokio::test]
fn validator_exit() { async fn validator_exit() {
let runtime = build_runtime(); ApiTester::new()
let weak_runtime = Arc::downgrade(&runtime);
runtime.block_on(async {
ApiTester::new(weak_runtime)
.await .await
.create_hd_validators(HdValidatorScenario { .create_hd_validators(HdValidatorScenario {
count: 2, count: 2,
@@ -879,15 +850,11 @@ fn validator_exit() {
.await .await
.test_sign_voluntary_exits(0, Some(Epoch::new(256))) .test_sign_voluntary_exits(0, Some(Epoch::new(256)))
.await; .await;
});
} }
#[test] #[tokio::test]
fn validator_enabling() { async fn validator_enabling() {
let runtime = build_runtime(); ApiTester::new()
let weak_runtime = Arc::downgrade(&runtime);
runtime.block_on(async {
ApiTester::new(weak_runtime)
.await .await
.create_hd_validators(HdValidatorScenario { .create_hd_validators(HdValidatorScenario {
count: 2, count: 2,
@@ -906,15 +873,11 @@ fn validator_enabling() {
.await .await
.assert_enabled_validators_count(2) .assert_enabled_validators_count(2)
.assert_validators_count(2); .assert_validators_count(2);
});
} }
#[test] #[tokio::test]
fn validator_gas_limit() { async fn validator_gas_limit() {
let runtime = build_runtime(); ApiTester::new()
let weak_runtime = Arc::downgrade(&runtime);
runtime.block_on(async {
ApiTester::new(weak_runtime)
.await .await
.create_hd_validators(HdValidatorScenario { .create_hd_validators(HdValidatorScenario {
count: 2, count: 2,
@@ -940,16 +903,12 @@ fn validator_gas_limit() {
.await .await
.assert_enabled_validators_count(2) .assert_enabled_validators_count(2)
.assert_gas_limit(0, 1000) .assert_gas_limit(0, 1000)
.await .await;
});
} }
#[test] #[tokio::test]
fn validator_builder_proposals() { async fn validator_builder_proposals() {
let runtime = build_runtime(); ApiTester::new()
let weak_runtime = Arc::downgrade(&runtime);
runtime.block_on(async {
ApiTester::new(weak_runtime)
.await .await
.create_hd_validators(HdValidatorScenario { .create_hd_validators(HdValidatorScenario {
count: 2, count: 2,
@@ -973,16 +932,12 @@ fn validator_builder_proposals() {
.await .await
.assert_enabled_validators_count(2) .assert_enabled_validators_count(2)
.assert_builder_proposals(0, false) .assert_builder_proposals(0, false)
.await .await;
});
} }
#[test] #[tokio::test]
fn validator_graffiti() { async fn validator_graffiti() {
let runtime = build_runtime(); ApiTester::new()
let weak_runtime = Arc::downgrade(&runtime);
runtime.block_on(async {
ApiTester::new(weak_runtime)
.await .await
.create_hd_validators(HdValidatorScenario { .create_hd_validators(HdValidatorScenario {
count: 2, count: 2,
@@ -1008,16 +963,12 @@ fn validator_graffiti() {
.await .await
.assert_enabled_validators_count(2) .assert_enabled_validators_count(2)
.assert_graffiti(0, "Mr F was here again") .assert_graffiti(0, "Mr F was here again")
.await .await;
});
} }
#[test] #[tokio::test]
fn keystore_validator_creation() { async fn keystore_validator_creation() {
let runtime = build_runtime(); ApiTester::new()
let weak_runtime = Arc::downgrade(&runtime);
runtime.block_on(async {
ApiTester::new(weak_runtime)
.await .await
.assert_enabled_validators_count(0) .assert_enabled_validators_count(0)
.assert_validators_count(0) .assert_validators_count(0)
@@ -1042,15 +993,11 @@ fn keystore_validator_creation() {
.await .await
.assert_enabled_validators_count(1) .assert_enabled_validators_count(1)
.assert_validators_count(2); .assert_validators_count(2);
});
} }
#[test] #[tokio::test]
fn web3signer_validator_creation() { async fn web3signer_validator_creation() {
let runtime = build_runtime(); ApiTester::new()
let weak_runtime = Arc::downgrade(&runtime);
runtime.block_on(async {
ApiTester::new(weak_runtime)
.await .await
.assert_enabled_validators_count(0) .assert_enabled_validators_count(0)
.assert_validators_count(0) .assert_validators_count(0)
@@ -1061,5 +1008,4 @@ fn web3signer_validator_creation() {
.await .await
.assert_enabled_validators_count(1) .assert_enabled_validators_count(1)
.assert_validators_count(1); .assert_validators_count(1);
});
} }