From 1a5861e50b422ac34e093263ac087f0caea1335a Mon Sep 17 00:00:00 2001 From: Leonid Tyurin Date: Tue, 10 Oct 2023 15:35:20 +0400 Subject: [PATCH 01/18] Add launcher test utils --- libs/blockscout-service-launcher/Cargo.toml | 11 ++- .../src/database.rs | 12 +-- libs/blockscout-service-launcher/src/lib.rs | 3 + .../src/test/database.rs | 82 +++++++++++++++++++ .../src/test/mod.rs | 9 ++ .../src/test/server.rs | 76 +++++++++++++++++ 6 files changed, 186 insertions(+), 7 deletions(-) create mode 100644 libs/blockscout-service-launcher/src/test/database.rs create mode 100644 libs/blockscout-service-launcher/src/test/mod.rs create mode 100644 libs/blockscout-service-launcher/src/test/server.rs diff --git a/libs/blockscout-service-launcher/Cargo.toml b/libs/blockscout-service-launcher/Cargo.toml index 700fef400..5e4151fe3 100644 --- a/libs/blockscout-service-launcher/Cargo.toml +++ b/libs/blockscout-service-launcher/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "blockscout-service-launcher" -version = "0.8.0" +version = "0.8.1" description = "Allows to launch blazingly fast blockscout rust services" license = "MIT" repository = "https://github.com/blockscout/blockscout-rs" @@ -17,9 +17,12 @@ anyhow = { version = "1.0", optional = true } config = { version = "0.13", optional = true } futures = { version = "0.3", optional = true } cfg-if = { version = "1.0.0", optional = true } +keccak-hash = { version = "0.10.0", optional = true } opentelemetry = { version = "0.19", optional = true } opentelemetry-jaeger = { version = "0.18", features = ["rt-tokio"], optional = true } prometheus = { version = "0.13", optional = true } +rand = {version = "0.8", optional = true } +reqwest = { version = "0.11", features = ["json"], optional = true } serde = { version = "1.0", features = ["derive"], optional = true } tokio = { version = "1", optional = true } tonic = { version = ">= 0.8, < 0.10", optional = true } @@ -85,3 +88,9 @@ database-0_10 = [ "dep:sea-orm-0_10", "dep:sea-orm-migration-0_10", ] + +test = [ + "dep:rand", + "dep:reqwest", + "dep:keccak-hash", +] \ No newline at end of file diff --git a/libs/blockscout-service-launcher/src/database.rs b/libs/blockscout-service-launcher/src/database.rs index c1b0ee473..19c4cba11 100644 --- a/libs/blockscout-service-launcher/src/database.rs +++ b/libs/blockscout-service-launcher/src/database.rs @@ -3,14 +3,14 @@ use std::str::FromStr; cfg_if::cfg_if! { if #[cfg(feature = "database-0_12")] { - use sea_orm_0_12::{ConnectOptions, ConnectionTrait, Database, DatabaseBackend, Statement}; - use sea_orm_migration_0_12::MigratorTrait; + pub use sea_orm_0_12::{ConnectOptions, ConnectionTrait, Database, DatabaseBackend, Statement, DatabaseConnection, DbErr}; + pub use sea_orm_migration_0_12::MigratorTrait; } else if #[cfg(feature = "database-0_11")] { - use sea_orm_0_11::{ConnectOptions, ConnectionTrait, Database, DatabaseBackend, Statement}; - use sea_orm_migration_0_11::MigratorTrait; + pub use sea_orm_0_11::{ConnectOptions, ConnectionTrait, Database, DatabaseBackend, Statement, DatabaseConnection, DbErr}; + pub use sea_orm_migration_0_11::MigratorTrait; } else if #[cfg(feature = "database-0_10")] { - use sea_orm_0_10::{ConnectOptions, ConnectionTrait, Database, DatabaseBackend, Statement}; - use sea_orm_migration_0_10::MigratorTrait; + pub use sea_orm_0_10::{ConnectOptions, ConnectionTrait, Database, DatabaseBackend, Statement, DatabaseConnection, DbErr}; + pub use sea_orm_migration_0_10::MigratorTrait; } else { compile_error!( "one of the features ['database-0_12', 'database-0_11', 'database-0_10'] \ diff --git a/libs/blockscout-service-launcher/src/lib.rs b/libs/blockscout-service-launcher/src/lib.rs index 025000c7e..9c938c3e6 100644 --- a/libs/blockscout-service-launcher/src/lib.rs +++ b/libs/blockscout-service-launcher/src/lib.rs @@ -6,3 +6,6 @@ pub mod launcher; #[cfg(feature = "tracing")] pub mod tracing; + +#[cfg(feature = "test")] +pub mod test; diff --git a/libs/blockscout-service-launcher/src/test/database.rs b/libs/blockscout-service-launcher/src/test/database.rs new file mode 100644 index 000000000..ead98ec1b --- /dev/null +++ b/libs/blockscout-service-launcher/src/test/database.rs @@ -0,0 +1,82 @@ +use crate::database::{ + ConnectionTrait, Database, DatabaseConnection, DbErr, MigratorTrait, Statement, +}; +use std::sync::Arc; + +#[derive(Clone, Debug)] +pub struct TestDbGuard { + conn_with_db: Arc, + db_url: String, +} + +impl TestDbGuard { + pub async fn new(db_name: &str, db_url: String) -> Self { + // We use a hash, as the name itself may be quite long and be trimmed. + let db_name = format!("_{:x}", keccak_hash::keccak(db_name)); + let (db_url, conn_with_db) = Self::init_database::(&db_url, &db_name).await; + + TestDbGuard { + conn_with_db: Arc::new(conn_with_db), + db_url, + } + } + + pub fn client(&self) -> Arc { + self.conn_with_db.clone() + } + + pub fn db_url(&self) -> &str { + &self.db_url + } + + async fn init_database( + base_db_url: &str, + db_name: &str, + ) -> (String, DatabaseConnection) { + // Create database + let conn_without_db = Database::connect(base_db_url) + .await + .expect("Connection to postgres (without database) failed"); + Self::drop_database(&conn_without_db, db_name) + .await + .expect("Database drop failed"); + Self::create_database(&conn_without_db, db_name) + .await + .expect("Database creation failed"); + + // Migrate database + let db_url = format!("{base_db_url}/{db_name}"); + let conn_with_db = Database::connect(&db_url) + .await + .expect("Connection to postgres (with database) failed"); + Self::run_migrations::(&conn_with_db) + .await + .expect("Database migration failed"); + + (db_url, conn_with_db) + } + + async fn create_database(db: &DatabaseConnection, db_name: &str) -> Result<(), DbErr> { + tracing::info!(name = db_name, "creating database"); + db.execute(Statement::from_string( + db.get_database_backend(), + format!("CREATE DATABASE {db_name}"), + )) + .await?; + Ok(()) + } + + async fn drop_database(db: &DatabaseConnection, db_name: &str) -> Result<(), DbErr> { + tracing::info!(name = db_name, "dropping database"); + db.execute(Statement::from_string( + db.get_database_backend(), + format!("DROP DATABASE IF EXISTS {db_name} WITH (FORCE)"), + )) + .await?; + Ok(()) + } + + async fn run_migrations(db: &DatabaseConnection) -> Result<(), DbErr> { + Migrator::up(db, None).await + } +} diff --git a/libs/blockscout-service-launcher/src/test/mod.rs b/libs/blockscout-service-launcher/src/test/mod.rs new file mode 100644 index 000000000..fed33111f --- /dev/null +++ b/libs/blockscout-service-launcher/src/test/mod.rs @@ -0,0 +1,9 @@ +mod server; + +#[cfg(feature = "database")] +mod database; + +pub use server::{get_test_server_settings, init_server, send_request}; + +#[cfg(feature = "database")] +pub use database::TestDbGuard; diff --git a/libs/blockscout-service-launcher/src/test/server.rs b/libs/blockscout-service-launcher/src/test/server.rs new file mode 100644 index 000000000..e3082701e --- /dev/null +++ b/libs/blockscout-service-launcher/src/test/server.rs @@ -0,0 +1,76 @@ +use crate::launcher::ServerSettings; +use rand; +use reqwest::Url; +use std::{future::Future, net::SocketAddr, str::FromStr}; + +pub fn get_test_server_settings() -> (ServerSettings, Url) { + let mut server = ServerSettings::default(); + // Take a random port in range [10000..65535] + let port = (rand::random::() % 55535) + 10000; + server.http.addr = SocketAddr::from_str(&format!("127.0.0.1:{port}")).unwrap(); + server.grpc.enabled = false; + let base = Url::parse(&format!("http://{}", server.http.addr)).unwrap(); + (server, base) +} + +pub async fn init_server(run: F, base: &Url, health_check_service: &str) +where + F: FnOnce() -> R + Send + 'static, + R: Future + Send, +{ + tokio::spawn(async move { run().await }); + + let client = reqwest::Client::new(); + + let health_endpoint = base.join("health").unwrap(); + // Wait for the server to start + loop { + if let Ok(_response) = client + .get(health_endpoint.clone()) + .query(&[("service", health_check_service)]) + .send() + .await + { + break; + } + } +} + +async fn send_annotated_request< + Request: serde::Serialize, + Response: for<'a> serde::Deserialize<'a>, +>( + url: &Url, + route: &str, + request: &Request, + annotation: Option<&str>, +) -> Response { + let annotation = annotation.map(|v| format!("({v}) ")).unwrap_or_default(); + + let response = reqwest::Client::new() + .post(url.join(route).unwrap()) + .json(&request) + .send() + .await + .unwrap_or_else(|_| panic!("{annotation}Failed to send request")); + + // Assert that status code is success + if !response.status().is_success() { + let status = response.status(); + let message = response.text().await.expect("Read body as text"); + panic!("({annotation})Invalid status code (success expected). Status: {status}. Message: {message}") + } + + response + .json() + .await + .unwrap_or_else(|_| panic!("({annotation})Response deserialization failed")) +} + +pub async fn send_request serde::Deserialize<'a>>( + url: &Url, + route: &str, + request: &Request, +) -> Response { + send_annotated_request(url, route, request, None).await +} From 8a6ba68953563db6819ae3d51cdf189b25d7a69a Mon Sep 17 00:00:00 2001 From: Leonid Tyurin Date: Tue, 10 Oct 2023 17:37:46 +0400 Subject: [PATCH 02/18] Add timeout & reliable port assigning --- libs/blockscout-service-launcher/Cargo.toml | 2 - .../src/test/server.rs | 42 ++++++++++++------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/libs/blockscout-service-launcher/Cargo.toml b/libs/blockscout-service-launcher/Cargo.toml index 5e4151fe3..d1f0ddc72 100644 --- a/libs/blockscout-service-launcher/Cargo.toml +++ b/libs/blockscout-service-launcher/Cargo.toml @@ -21,7 +21,6 @@ keccak-hash = { version = "0.10.0", optional = true } opentelemetry = { version = "0.19", optional = true } opentelemetry-jaeger = { version = "0.18", features = ["rt-tokio"], optional = true } prometheus = { version = "0.13", optional = true } -rand = {version = "0.8", optional = true } reqwest = { version = "0.11", features = ["json"], optional = true } serde = { version = "1.0", features = ["derive"], optional = true } tokio = { version = "1", optional = true } @@ -90,7 +89,6 @@ database-0_10 = [ ] test = [ - "dep:rand", "dep:reqwest", "dep:keccak-hash", ] \ No newline at end of file diff --git a/libs/blockscout-service-launcher/src/test/server.rs b/libs/blockscout-service-launcher/src/test/server.rs index e3082701e..db993bc29 100644 --- a/libs/blockscout-service-launcher/src/test/server.rs +++ b/libs/blockscout-service-launcher/src/test/server.rs @@ -1,12 +1,21 @@ use crate::launcher::ServerSettings; -use rand; use reqwest::Url; -use std::{future::Future, net::SocketAddr, str::FromStr}; +use std::{ + future::Future, + net::{SocketAddr, TcpListener}, + str::FromStr, + time::Duration, +}; +use tokio::time::timeout; + +fn get_free_port() -> u16 { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + listener.local_addr().unwrap().port() +} pub fn get_test_server_settings() -> (ServerSettings, Url) { let mut server = ServerSettings::default(); - // Take a random port in range [10000..65535] - let port = (rand::random::() % 55535) + 10000; + let port = get_free_port(); server.http.addr = SocketAddr::from_str(&format!("127.0.0.1:{port}")).unwrap(); server.grpc.enabled = false; let base = Url::parse(&format!("http://{}", server.http.addr)).unwrap(); @@ -21,18 +30,23 @@ where tokio::spawn(async move { run().await }); let client = reqwest::Client::new(); - let health_endpoint = base.join("health").unwrap(); - // Wait for the server to start - loop { - if let Ok(_response) = client - .get(health_endpoint.clone()) - .query(&[("service", health_check_service)]) - .send() - .await - { - break; + + let wait_health_check = async { + loop { + if let Ok(_response) = client + .get(health_endpoint.clone()) + .query(&[("service", health_check_service)]) + .send() + .await + { + break; + } } + }; + // Wait for the server to start + if (timeout(Duration::from_secs(10), wait_health_check).await).is_err() { + panic!("Server did not start in time"); } } From b2772a54c3dbbe60177a334867ec6ce64d72096f Mon Sep 17 00:00:00 2001 From: Leonid Tyurin Date: Tue, 10 Oct 2023 17:43:25 +0400 Subject: [PATCH 03/18] Fix clippy error --- libs/solidity-metadata/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/solidity-metadata/src/lib.rs b/libs/solidity-metadata/src/lib.rs index e809da353..6fb9a0ebf 100644 --- a/libs/solidity-metadata/src/lib.rs +++ b/libs/solidity-metadata/src/lib.rs @@ -49,7 +49,7 @@ impl<'b> Decode<'b, DecodeContext> for MetadataHash { for _ in 0..number_of_elements { // try to parse the key match d.str() { - Ok(s) if s == "solc" => { + Ok("solc") => { if solc.is_some() { // duplicate keys are not allowed in CBOR (RFC 8949) return Err(Error::custom(ParseMetadataHashError::DuplicateKeys)); From 5a90028b2504c102ac22b4dd01c067d03e609c45 Mon Sep 17 00:00:00 2001 From: Leonid Tyurin Date: Wed, 11 Oct 2023 14:59:28 +0400 Subject: [PATCH 04/18] Extract test_database module --- libs/blockscout-service-launcher/Cargo.toml | 6 +++++- libs/blockscout-service-launcher/src/lib.rs | 7 +++++-- libs/blockscout-service-launcher/src/test/mod.rs | 9 --------- .../src/{test => test_database}/database.rs | 0 .../blockscout-service-launcher/src/test_database/mod.rs | 3 +++ libs/blockscout-service-launcher/src/test_server/mod.rs | 3 +++ .../src/{test => test_server}/server.rs | 0 7 files changed, 16 insertions(+), 12 deletions(-) delete mode 100644 libs/blockscout-service-launcher/src/test/mod.rs rename libs/blockscout-service-launcher/src/{test => test_database}/database.rs (100%) create mode 100644 libs/blockscout-service-launcher/src/test_database/mod.rs create mode 100644 libs/blockscout-service-launcher/src/test_server/mod.rs rename libs/blockscout-service-launcher/src/{test => test_server}/server.rs (100%) diff --git a/libs/blockscout-service-launcher/Cargo.toml b/libs/blockscout-service-launcher/Cargo.toml index d1f0ddc72..3def920b1 100644 --- a/libs/blockscout-service-launcher/Cargo.toml +++ b/libs/blockscout-service-launcher/Cargo.toml @@ -88,7 +88,11 @@ database-0_10 = [ "dep:sea-orm-migration-0_10", ] -test = [ +test-server = [ "dep:reqwest", +] + +test-database = [ + "database", "dep:keccak-hash", ] \ No newline at end of file diff --git a/libs/blockscout-service-launcher/src/lib.rs b/libs/blockscout-service-launcher/src/lib.rs index 9c938c3e6..d7100cf53 100644 --- a/libs/blockscout-service-launcher/src/lib.rs +++ b/libs/blockscout-service-launcher/src/lib.rs @@ -7,5 +7,8 @@ pub mod launcher; #[cfg(feature = "tracing")] pub mod tracing; -#[cfg(feature = "test")] -pub mod test; +#[cfg(feature = "test-server")] +pub mod test_server; + +#[cfg(feature = "test-database")] +pub mod test_database; diff --git a/libs/blockscout-service-launcher/src/test/mod.rs b/libs/blockscout-service-launcher/src/test/mod.rs deleted file mode 100644 index fed33111f..000000000 --- a/libs/blockscout-service-launcher/src/test/mod.rs +++ /dev/null @@ -1,9 +0,0 @@ -mod server; - -#[cfg(feature = "database")] -mod database; - -pub use server::{get_test_server_settings, init_server, send_request}; - -#[cfg(feature = "database")] -pub use database::TestDbGuard; diff --git a/libs/blockscout-service-launcher/src/test/database.rs b/libs/blockscout-service-launcher/src/test_database/database.rs similarity index 100% rename from libs/blockscout-service-launcher/src/test/database.rs rename to libs/blockscout-service-launcher/src/test_database/database.rs diff --git a/libs/blockscout-service-launcher/src/test_database/mod.rs b/libs/blockscout-service-launcher/src/test_database/mod.rs new file mode 100644 index 000000000..290729300 --- /dev/null +++ b/libs/blockscout-service-launcher/src/test_database/mod.rs @@ -0,0 +1,3 @@ +mod database; + +pub use database::TestDbGuard; diff --git a/libs/blockscout-service-launcher/src/test_server/mod.rs b/libs/blockscout-service-launcher/src/test_server/mod.rs new file mode 100644 index 000000000..acf0009b7 --- /dev/null +++ b/libs/blockscout-service-launcher/src/test_server/mod.rs @@ -0,0 +1,3 @@ +mod server; + +pub use server::{get_test_server_settings, init_server, send_request}; diff --git a/libs/blockscout-service-launcher/src/test/server.rs b/libs/blockscout-service-launcher/src/test_server/server.rs similarity index 100% rename from libs/blockscout-service-launcher/src/test/server.rs rename to libs/blockscout-service-launcher/src/test_server/server.rs From 321ef5a58dc26623c485ba2307922267961e7514 Mon Sep 17 00:00:00 2001 From: Leonid Tyurin Date: Wed, 11 Oct 2023 20:45:20 +0400 Subject: [PATCH 05/18] Change file structure --- .../src/{test_database/database.rs => test_database.rs} | 0 libs/blockscout-service-launcher/src/test_database/mod.rs | 3 --- .../src/{test_server/server.rs => test_server.rs} | 0 libs/blockscout-service-launcher/src/test_server/mod.rs | 3 --- 4 files changed, 6 deletions(-) rename libs/blockscout-service-launcher/src/{test_database/database.rs => test_database.rs} (100%) delete mode 100644 libs/blockscout-service-launcher/src/test_database/mod.rs rename libs/blockscout-service-launcher/src/{test_server/server.rs => test_server.rs} (100%) delete mode 100644 libs/blockscout-service-launcher/src/test_server/mod.rs diff --git a/libs/blockscout-service-launcher/src/test_database/database.rs b/libs/blockscout-service-launcher/src/test_database.rs similarity index 100% rename from libs/blockscout-service-launcher/src/test_database/database.rs rename to libs/blockscout-service-launcher/src/test_database.rs diff --git a/libs/blockscout-service-launcher/src/test_database/mod.rs b/libs/blockscout-service-launcher/src/test_database/mod.rs deleted file mode 100644 index 290729300..000000000 --- a/libs/blockscout-service-launcher/src/test_database/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -mod database; - -pub use database::TestDbGuard; diff --git a/libs/blockscout-service-launcher/src/test_server/server.rs b/libs/blockscout-service-launcher/src/test_server.rs similarity index 100% rename from libs/blockscout-service-launcher/src/test_server/server.rs rename to libs/blockscout-service-launcher/src/test_server.rs diff --git a/libs/blockscout-service-launcher/src/test_server/mod.rs b/libs/blockscout-service-launcher/src/test_server/mod.rs deleted file mode 100644 index acf0009b7..000000000 --- a/libs/blockscout-service-launcher/src/test_server/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -mod server; - -pub use server::{get_test_server_settings, init_server, send_request}; From ea9ebdbbd8387400b2f41687eee7c5b51962cbb7 Mon Sep 17 00:00:00 2001 From: Leonid Tyurin Date: Wed, 11 Oct 2023 20:49:21 +0400 Subject: [PATCH 06/18] Remove db_url argument --- libs/blockscout-service-launcher/src/test_database.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libs/blockscout-service-launcher/src/test_database.rs b/libs/blockscout-service-launcher/src/test_database.rs index ead98ec1b..7525ba089 100644 --- a/libs/blockscout-service-launcher/src/test_database.rs +++ b/libs/blockscout-service-launcher/src/test_database.rs @@ -10,7 +10,9 @@ pub struct TestDbGuard { } impl TestDbGuard { - pub async fn new(db_name: &str, db_url: String) -> Self { + pub async fn new(db_name: &str) -> Self { + let db_url = std::env::var("DATABASE_URL") + .expect("Database url must be set to initialize a test database"); // We use a hash, as the name itself may be quite long and be trimmed. let db_name = format!("_{:x}", keccak_hash::keccak(db_name)); let (db_url, conn_with_db) = Self::init_database::(&db_url, &db_name).await; From 86983f4e4bb1ada2612188ab638054102f3ce7a7 Mon Sep 17 00:00:00 2001 From: Leonid Tyurin Date: Wed, 11 Oct 2023 20:50:25 +0400 Subject: [PATCH 07/18] Add launcher dep --- libs/blockscout-service-launcher/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/libs/blockscout-service-launcher/Cargo.toml b/libs/blockscout-service-launcher/Cargo.toml index 3def920b1..4e95c2054 100644 --- a/libs/blockscout-service-launcher/Cargo.toml +++ b/libs/blockscout-service-launcher/Cargo.toml @@ -89,6 +89,7 @@ database-0_10 = [ ] test-server = [ + "launcher", "dep:reqwest", ] From bc2e1bcb598665e0a6205c20e1085256913e78cc Mon Sep 17 00:00:00 2001 From: Leonid Tyurin Date: Wed, 11 Oct 2023 20:52:38 +0400 Subject: [PATCH 08/18] Use empty service name for health check --- libs/blockscout-service-launcher/src/test_server.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/blockscout-service-launcher/src/test_server.rs b/libs/blockscout-service-launcher/src/test_server.rs index db993bc29..571bd85bb 100644 --- a/libs/blockscout-service-launcher/src/test_server.rs +++ b/libs/blockscout-service-launcher/src/test_server.rs @@ -22,7 +22,7 @@ pub fn get_test_server_settings() -> (ServerSettings, Url) { (server, base) } -pub async fn init_server(run: F, base: &Url, health_check_service: &str) +pub async fn init_server(run: F, base: &Url) where F: FnOnce() -> R + Send + 'static, R: Future + Send, @@ -36,7 +36,7 @@ where loop { if let Ok(_response) = client .get(health_endpoint.clone()) - .query(&[("service", health_check_service)]) + .query(&[("service", "")]) .send() .await { From e58fe568259e5a871e0805f5ce09c95cba7cb27d Mon Sep 17 00:00:00 2001 From: Leonid Tyurin Date: Thu, 12 Oct 2023 18:11:55 +0400 Subject: [PATCH 09/18] Add get request util --- libs/blockscout-service-launcher/Cargo.toml | 2 +- .../src/test_server.rs | 30 +++++++++++-------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/libs/blockscout-service-launcher/Cargo.toml b/libs/blockscout-service-launcher/Cargo.toml index 4e95c2054..4731a250f 100644 --- a/libs/blockscout-service-launcher/Cargo.toml +++ b/libs/blockscout-service-launcher/Cargo.toml @@ -24,7 +24,7 @@ prometheus = { version = "0.13", optional = true } reqwest = { version = "0.11", features = ["json"], optional = true } serde = { version = "1.0", features = ["derive"], optional = true } tokio = { version = "1", optional = true } -tonic = { version = ">= 0.8, < 0.10", optional = true } +tonic = { version = "0.8", optional = true } tracing = { version = "0.1", optional = true } tracing-opentelemetry = { version = "0.19", optional = true } tracing-subscriber = { version = "0.3", features = ["env-filter", "json"], optional = true } diff --git a/libs/blockscout-service-launcher/src/test_server.rs b/libs/blockscout-service-launcher/src/test_server.rs index 571bd85bb..7a4247b51 100644 --- a/libs/blockscout-service-launcher/src/test_server.rs +++ b/libs/blockscout-service-launcher/src/test_server.rs @@ -25,7 +25,7 @@ pub fn get_test_server_settings() -> (ServerSettings, Url) { pub async fn init_server(run: F, base: &Url) where F: FnOnce() -> R + Send + 'static, - R: Future + Send, + R: Future> + Send, { tokio::spawn(async move { run().await }); @@ -50,20 +50,20 @@ where } } -async fn send_annotated_request< - Request: serde::Serialize, - Response: for<'a> serde::Deserialize<'a>, ->( +async fn send_annotated_request serde::Deserialize<'a>>( url: &Url, route: &str, - request: &Request, + method: reqwest::Method, + payload: Option<&impl serde::Serialize>, annotation: Option<&str>, ) -> Response { let annotation = annotation.map(|v| format!("({v}) ")).unwrap_or_default(); - let response = reqwest::Client::new() - .post(url.join(route).unwrap()) - .json(&request) + let mut request = reqwest::Client::new().request(method, url.join(route).unwrap()); + if let Some(p) = payload { + request = request.json(p); + }; + let response = request .send() .await .unwrap_or_else(|_| panic!("{annotation}Failed to send request")); @@ -80,11 +80,17 @@ async fn send_annotated_request< .await .unwrap_or_else(|_| panic!("({annotation})Response deserialization failed")) } +pub async fn send_post_request serde::Deserialize<'a>>( + url: &Url, + route: &str, + payload: &impl serde::Serialize, +) -> Response { + send_annotated_request(url, route, reqwest::Method::POST, Some(payload), None).await +} -pub async fn send_request serde::Deserialize<'a>>( +pub async fn send_get_request serde::Deserialize<'a>>( url: &Url, route: &str, - request: &Request, ) -> Response { - send_annotated_request(url, route, request, None).await + send_annotated_request(url, route, reqwest::Method::GET, None::<&()>, None).await } From 35fbeefcfd91b4a20727a12294e48bb8e36139c9 Mon Sep 17 00:00:00 2001 From: Leonid Tyurin Date: Fri, 13 Oct 2023 16:18:40 +0400 Subject: [PATCH 10/18] Add Deref impl --- libs/blockscout-service-launcher/src/test_database.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/libs/blockscout-service-launcher/src/test_database.rs b/libs/blockscout-service-launcher/src/test_database.rs index 7525ba089..c2a675db9 100644 --- a/libs/blockscout-service-launcher/src/test_database.rs +++ b/libs/blockscout-service-launcher/src/test_database.rs @@ -1,7 +1,7 @@ use crate::database::{ ConnectionTrait, Database, DatabaseConnection, DbErr, MigratorTrait, Statement, }; -use std::sync::Arc; +use std::{ops::Deref, sync::Arc}; #[derive(Clone, Debug)] pub struct TestDbGuard { @@ -82,3 +82,10 @@ impl TestDbGuard { Migrator::up(db, None).await } } + +impl Deref for TestDbGuard { + type Target = DatabaseConnection; + fn deref(&self) -> &Self::Target { + &*self.conn_with_db + } +} From 6233072f7cd59f09f08c719525460af12516cac8 Mon Sep 17 00:00:00 2001 From: Leonid Tyurin Date: Fri, 13 Oct 2023 17:15:27 +0400 Subject: [PATCH 11/18] Fix warning --- libs/blockscout-service-launcher/src/test_database.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/blockscout-service-launcher/src/test_database.rs b/libs/blockscout-service-launcher/src/test_database.rs index c2a675db9..fc1fd0029 100644 --- a/libs/blockscout-service-launcher/src/test_database.rs +++ b/libs/blockscout-service-launcher/src/test_database.rs @@ -86,6 +86,6 @@ impl TestDbGuard { impl Deref for TestDbGuard { type Target = DatabaseConnection; fn deref(&self) -> &Self::Target { - &*self.conn_with_db + &self.conn_with_db } } From 1779d66dd609e6b08d7ab114ab027d103c34fd85 Mon Sep 17 00:00:00 2001 From: Leonid Tyurin Date: Sun, 15 Oct 2023 15:09:00 +0400 Subject: [PATCH 12/18] Implement async db drop --- libs/blockscout-service-launcher/Cargo.toml | 5 ++ .../src/test_database.rs | 50 ++++++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/libs/blockscout-service-launcher/Cargo.toml b/libs/blockscout-service-launcher/Cargo.toml index 4731a250f..f348c4374 100644 --- a/libs/blockscout-service-launcher/Cargo.toml +++ b/libs/blockscout-service-launcher/Cargo.toml @@ -41,6 +41,9 @@ sea-orm-migration-0_11 = { package = "sea-orm-migration", version = "0.11", opti sea-orm-0_12 = { package = "sea-orm", version = "0.12.2", optional = true } sea-orm-migration-0_12 = { package = "sea-orm-migration", version = "0.12.2", optional = true } +async-dropper = { version = "0.2", features = [ "tokio" ], optional = true } +async-trait = { version = "0.1", optional = true } + [features] default = ["launcher", "tracing"] launcher = [ @@ -96,4 +99,6 @@ test-server = [ test-database = [ "database", "dep:keccak-hash", + "dep:async-dropper", + "dep:async-trait", ] \ No newline at end of file diff --git a/libs/blockscout-service-launcher/src/test_database.rs b/libs/blockscout-service-launcher/src/test_database.rs index fc1fd0029..7ba9df7a0 100644 --- a/libs/blockscout-service-launcher/src/test_database.rs +++ b/libs/blockscout-service-launcher/src/test_database.rs @@ -1,14 +1,34 @@ use crate::database::{ ConnectionTrait, Database, DatabaseConnection, DbErr, MigratorTrait, Statement, }; -use std::{ops::Deref, sync::Arc}; +use async_dropper::derive::AsyncDrop; +use async_trait::async_trait; +use std::{ops::Deref, sync::Arc, time::Duration}; -#[derive(Clone, Debug)] +#[derive(AsyncDrop, Clone, Debug, Default)] pub struct TestDbGuard { conn_with_db: Arc, db_url: String, + db_name: String, + should_drop: bool, } +impl PartialEq for TestDbGuard { + fn eq(&self, other: &Self) -> bool { + self.db_url == other.db_url + && self.db_name == other.db_name + && matches!( + (self.conn_with_db.deref(), other.conn_with_db.deref()), + ( + DatabaseConnection::Disconnected, + DatabaseConnection::Disconnected + ) + ) + } +} + +impl Eq for TestDbGuard {} + impl TestDbGuard { pub async fn new(db_name: &str) -> Self { let db_url = std::env::var("DATABASE_URL") @@ -20,9 +40,16 @@ impl TestDbGuard { TestDbGuard { conn_with_db: Arc::new(conn_with_db), db_url, + db_name, + should_drop: true, } } + pub fn without_drop(mut self) -> Self { + self.should_drop = false; + self + } + pub fn client(&self) -> Arc { self.conn_with_db.clone() } @@ -89,3 +116,22 @@ impl Deref for TestDbGuard { &self.conn_with_db } } + +#[async_trait] +impl AsyncDrop for TestDbGuard { + async fn async_drop(&mut self) -> Result<(), AsyncDropError> { + if self.should_drop { + // Workaround for postgres error `cannot drop the currently open database` + self.conn_with_db.drop(); + let conn_without_db = Database::connect(self.db_url()) + .await + .expect("Connection to postgres (without database) failed"); + self.conn_with_db = Arc::new(conn_without_db); + + TestDbGuard::drop_database(self, self.db_name.as_str()) + .await + .expect("Failed to drop database"); + } + Ok(()) + } +} From 41654517386620fba70684953c010b46cc1d8882 Mon Sep 17 00:00:00 2001 From: Leonid Tyurin Date: Sun, 15 Oct 2023 21:54:20 +0400 Subject: [PATCH 13/18] Fix drop panic --- .../src/test_database.rs | 77 +++++++++++-------- 1 file changed, 44 insertions(+), 33 deletions(-) diff --git a/libs/blockscout-service-launcher/src/test_database.rs b/libs/blockscout-service-launcher/src/test_database.rs index 7ba9df7a0..bbc3f33e8 100644 --- a/libs/blockscout-service-launcher/src/test_database.rs +++ b/libs/blockscout-service-launcher/src/test_database.rs @@ -1,6 +1,7 @@ use crate::database::{ ConnectionTrait, Database, DatabaseConnection, DbErr, MigratorTrait, Statement, }; +use anyhow::anyhow; use async_dropper::derive::AsyncDrop; use async_trait::async_trait; use std::{ops::Deref, sync::Arc, time::Duration}; @@ -8,14 +9,14 @@ use std::{ops::Deref, sync::Arc, time::Duration}; #[derive(AsyncDrop, Clone, Debug, Default)] pub struct TestDbGuard { conn_with_db: Arc, - db_url: String, + base_db_url: String, db_name: String, should_drop: bool, } impl PartialEq for TestDbGuard { fn eq(&self, other: &Self) -> bool { - self.db_url == other.db_url + self.base_db_url == other.base_db_url && self.db_name == other.db_name && matches!( (self.conn_with_db.deref(), other.conn_with_db.deref()), @@ -31,18 +32,19 @@ impl Eq for TestDbGuard {} impl TestDbGuard { pub async fn new(db_name: &str) -> Self { - let db_url = std::env::var("DATABASE_URL") + let base_db_url = std::env::var("DATABASE_URL") .expect("Database url must be set to initialize a test database"); // We use a hash, as the name itself may be quite long and be trimmed. let db_name = format!("_{:x}", keccak_hash::keccak(db_name)); - let (db_url, conn_with_db) = Self::init_database::(&db_url, &db_name).await; - - TestDbGuard { - conn_with_db: Arc::new(conn_with_db), - db_url, + let mut guard = TestDbGuard { + conn_with_db: Arc::new(DatabaseConnection::Disconnected), + base_db_url, db_name, should_drop: true, - } + }; + + guard.init_database::().await; + guard } pub fn without_drop(mut self) -> Self { @@ -54,35 +56,31 @@ impl TestDbGuard { self.conn_with_db.clone() } - pub fn db_url(&self) -> &str { - &self.db_url + pub fn db_url(&self) -> String { + format!("{}/{}", self.base_db_url, self.db_name) } - async fn init_database( - base_db_url: &str, - db_name: &str, - ) -> (String, DatabaseConnection) { + async fn init_database(&mut self) { // Create database - let conn_without_db = Database::connect(base_db_url) + let conn_without_db = Database::connect(&self.base_db_url) .await .expect("Connection to postgres (without database) failed"); - Self::drop_database(&conn_without_db, db_name) + Self::drop_database(&conn_without_db, &self.db_name) .await .expect("Database drop failed"); - Self::create_database(&conn_without_db, db_name) + Self::create_database(&conn_without_db, &self.db_name) .await .expect("Database creation failed"); // Migrate database - let db_url = format!("{base_db_url}/{db_name}"); + let db_url = self.db_url(); let conn_with_db = Database::connect(&db_url) .await .expect("Connection to postgres (with database) failed"); Self::run_migrations::(&conn_with_db) .await .expect("Database migration failed"); - - (db_url, conn_with_db) + self.conn_with_db = Arc::new(conn_with_db); } async fn create_database(db: &DatabaseConnection, db_name: &str) -> Result<(), DbErr> { @@ -108,6 +106,20 @@ impl TestDbGuard { async fn run_migrations(db: &DatabaseConnection) -> Result<(), DbErr> { Migrator::up(db, None).await } + + async fn drop_internal(&mut self) -> Result<(), anyhow::Error> { + if self.should_drop { + // Workaround for postgres error `cannot drop the currently open database`: + // We need to create another connection without the database to drop it + let conn_without_db = Database::connect(self.base_db_url.as_str()) + .await + .map_err(|_| anyhow!("Failed to connect to the database"))?; + TestDbGuard::drop_database(&conn_without_db, self.db_name.as_str()) + .await + .map_err(|e| anyhow!("Failed to drop the database: {}", e))?; + } + Ok(()) + } } impl Deref for TestDbGuard { @@ -120,18 +132,17 @@ impl Deref for TestDbGuard { #[async_trait] impl AsyncDrop for TestDbGuard { async fn async_drop(&mut self) -> Result<(), AsyncDropError> { - if self.should_drop { - // Workaround for postgres error `cannot drop the currently open database` - self.conn_with_db.drop(); - let conn_without_db = Database::connect(self.db_url()) - .await - .expect("Connection to postgres (without database) failed"); - self.conn_with_db = Arc::new(conn_without_db); + self.drop_internal().await.map_err(|e| { + eprintln!("Failed to drop TestDbGuard: {:?}", e); + AsyncDropError::UnexpectedError(e.into()) + }) + } - TestDbGuard::drop_database(self, self.db_name.as_str()) - .await - .expect("Failed to drop database"); - } - Ok(()) + fn drop_fail_action(&self) -> DropFailAction { + DropFailAction::Panic + } + + fn drop_timeout(&self) -> Duration { + Duration::from_secs(1) } } From 7911c99b6b1bc0e562a536ca0619911116dc86e3 Mon Sep 17 00:00:00 2001 From: Leonid Tyurin Date: Mon, 16 Oct 2023 14:28:31 +0400 Subject: [PATCH 14/18] Implement background db drop --- libs/blockscout-service-launcher/Cargo.toml | 5 -- .../src/test_database.rs | 68 ++++++------------- 2 files changed, 21 insertions(+), 52 deletions(-) diff --git a/libs/blockscout-service-launcher/Cargo.toml b/libs/blockscout-service-launcher/Cargo.toml index f348c4374..4731a250f 100644 --- a/libs/blockscout-service-launcher/Cargo.toml +++ b/libs/blockscout-service-launcher/Cargo.toml @@ -41,9 +41,6 @@ sea-orm-migration-0_11 = { package = "sea-orm-migration", version = "0.11", opti sea-orm-0_12 = { package = "sea-orm", version = "0.12.2", optional = true } sea-orm-migration-0_12 = { package = "sea-orm-migration", version = "0.12.2", optional = true } -async-dropper = { version = "0.2", features = [ "tokio" ], optional = true } -async-trait = { version = "0.1", optional = true } - [features] default = ["launcher", "tracing"] launcher = [ @@ -99,6 +96,4 @@ test-server = [ test-database = [ "database", "dep:keccak-hash", - "dep:async-dropper", - "dep:async-trait", ] \ No newline at end of file diff --git a/libs/blockscout-service-launcher/src/test_database.rs b/libs/blockscout-service-launcher/src/test_database.rs index bbc3f33e8..c36cf8d61 100644 --- a/libs/blockscout-service-launcher/src/test_database.rs +++ b/libs/blockscout-service-launcher/src/test_database.rs @@ -2,11 +2,9 @@ use crate::database::{ ConnectionTrait, Database, DatabaseConnection, DbErr, MigratorTrait, Statement, }; use anyhow::anyhow; -use async_dropper::derive::AsyncDrop; -use async_trait::async_trait; -use std::{ops::Deref, sync::Arc, time::Duration}; +use std::{ops::Deref, sync::Arc}; -#[derive(AsyncDrop, Clone, Debug, Default)] +#[derive(Clone, Debug)] pub struct TestDbGuard { conn_with_db: Arc, base_db_url: String, @@ -14,22 +12,6 @@ pub struct TestDbGuard { should_drop: bool, } -impl PartialEq for TestDbGuard { - fn eq(&self, other: &Self) -> bool { - self.base_db_url == other.base_db_url - && self.db_name == other.db_name - && matches!( - (self.conn_with_db.deref(), other.conn_with_db.deref()), - ( - DatabaseConnection::Disconnected, - DatabaseConnection::Disconnected - ) - ) - } -} - -impl Eq for TestDbGuard {} - impl TestDbGuard { pub async fn new(db_name: &str) -> Self { let base_db_url = std::env::var("DATABASE_URL") @@ -106,20 +88,6 @@ impl TestDbGuard { async fn run_migrations(db: &DatabaseConnection) -> Result<(), DbErr> { Migrator::up(db, None).await } - - async fn drop_internal(&mut self) -> Result<(), anyhow::Error> { - if self.should_drop { - // Workaround for postgres error `cannot drop the currently open database`: - // We need to create another connection without the database to drop it - let conn_without_db = Database::connect(self.base_db_url.as_str()) - .await - .map_err(|_| anyhow!("Failed to connect to the database"))?; - TestDbGuard::drop_database(&conn_without_db, self.db_name.as_str()) - .await - .map_err(|e| anyhow!("Failed to drop the database: {}", e))?; - } - Ok(()) - } } impl Deref for TestDbGuard { @@ -129,20 +97,26 @@ impl Deref for TestDbGuard { } } -#[async_trait] -impl AsyncDrop for TestDbGuard { - async fn async_drop(&mut self) -> Result<(), AsyncDropError> { - self.drop_internal().await.map_err(|e| { - eprintln!("Failed to drop TestDbGuard: {:?}", e); - AsyncDropError::UnexpectedError(e.into()) - }) - } +impl Drop for TestDbGuard { + fn drop(&mut self) { + if !self.should_drop { + return; + } - fn drop_fail_action(&self) -> DropFailAction { - DropFailAction::Panic - } + let base_db_url = self.base_db_url.clone(); + let db_name = self.db_name.clone(); - fn drop_timeout(&self) -> Duration { - Duration::from_secs(1) + tokio::spawn(async move { + // Workaround for postgres error `cannot drop the currently open database`: + // We need to create another connection without the database to drop it + let conn_without_db = Database::connect(base_db_url.as_str()) + .await + .map_err(|_| anyhow!("Failed to connect to the database")) + .unwrap(); + TestDbGuard::drop_database(&conn_without_db, db_name.as_str()) + .await + .map_err(|e| anyhow!("Failed to drop the database: {}", e)) + .unwrap(); + }); } } From 2af5711750742c8793d53e832e4596833ad2d86d Mon Sep 17 00:00:00 2001 From: Leonid Tyurin Date: Mon, 16 Oct 2023 22:21:05 +0400 Subject: [PATCH 15/18] Remove Drop & refactor --- .../src/test_database.rs | 79 +++++++------------ 1 file changed, 28 insertions(+), 51 deletions(-) diff --git a/libs/blockscout-service-launcher/src/test_database.rs b/libs/blockscout-service-launcher/src/test_database.rs index c36cf8d61..77a9d64f2 100644 --- a/libs/blockscout-service-launcher/src/test_database.rs +++ b/libs/blockscout-service-launcher/src/test_database.rs @@ -1,39 +1,37 @@ use crate::database::{ ConnectionTrait, Database, DatabaseConnection, DbErr, MigratorTrait, Statement, }; -use anyhow::anyhow; use std::{ops::Deref, sync::Arc}; #[derive(Clone, Debug)] pub struct TestDbGuard { conn_with_db: Arc, + conn_without_db: Arc, base_db_url: String, db_name: String, - should_drop: bool, } impl TestDbGuard { pub async fn new(db_name: &str) -> Self { let base_db_url = std::env::var("DATABASE_URL") .expect("Database url must be set to initialize a test database"); + let conn_without_db = Database::connect(&base_db_url) + .await + .expect("Connection to postgres (without database) failed"); // We use a hash, as the name itself may be quite long and be trimmed. let db_name = format!("_{:x}", keccak_hash::keccak(db_name)); let mut guard = TestDbGuard { conn_with_db: Arc::new(DatabaseConnection::Disconnected), + conn_without_db: Arc::new(conn_without_db), base_db_url, db_name, - should_drop: true, }; - guard.init_database::().await; + guard.init_database().await; + guard.run_migrations::().await; guard } - pub fn without_drop(mut self) -> Self { - self.should_drop = false; - self - } - pub fn client(&self) -> Arc { self.conn_with_db.clone() } @@ -42,30 +40,31 @@ impl TestDbGuard { format!("{}/{}", self.base_db_url, self.db_name) } - async fn init_database(&mut self) { + async fn init_database(&mut self) { // Create database - let conn_without_db = Database::connect(&self.base_db_url) - .await - .expect("Connection to postgres (without database) failed"); - Self::drop_database(&conn_without_db, &self.db_name) - .await - .expect("Database drop failed"); - Self::create_database(&conn_without_db, &self.db_name) - .await - .expect("Database creation failed"); + self.drop_database().await; + self.create_database().await; - // Migrate database let db_url = self.db_url(); let conn_with_db = Database::connect(&db_url) .await .expect("Connection to postgres (with database) failed"); - Self::run_migrations::(&conn_with_db) - .await - .expect("Database migration failed"); self.conn_with_db = Arc::new(conn_with_db); } - async fn create_database(db: &DatabaseConnection, db_name: &str) -> Result<(), DbErr> { + pub async fn drop_database(&self) { + Self::drop_database_internal(&self.conn_without_db, &self.db_name) + .await + .expect("Database drop failed"); + } + + async fn create_database(&self) { + Self::create_database_internal(&self.conn_without_db, &self.db_name) + .await + .expect("Database creation failed"); + } + + async fn create_database_internal(db: &DatabaseConnection, db_name: &str) -> Result<(), DbErr> { tracing::info!(name = db_name, "creating database"); db.execute(Statement::from_string( db.get_database_backend(), @@ -75,7 +74,7 @@ impl TestDbGuard { Ok(()) } - async fn drop_database(db: &DatabaseConnection, db_name: &str) -> Result<(), DbErr> { + async fn drop_database_internal(db: &DatabaseConnection, db_name: &str) -> Result<(), DbErr> { tracing::info!(name = db_name, "dropping database"); db.execute(Statement::from_string( db.get_database_backend(), @@ -85,8 +84,10 @@ impl TestDbGuard { Ok(()) } - async fn run_migrations(db: &DatabaseConnection) -> Result<(), DbErr> { - Migrator::up(db, None).await + async fn run_migrations(&self) { + Migrator::up(self.conn_with_db.as_ref(), None) + .await + .expect("Database migration failed"); } } @@ -96,27 +97,3 @@ impl Deref for TestDbGuard { &self.conn_with_db } } - -impl Drop for TestDbGuard { - fn drop(&mut self) { - if !self.should_drop { - return; - } - - let base_db_url = self.base_db_url.clone(); - let db_name = self.db_name.clone(); - - tokio::spawn(async move { - // Workaround for postgres error `cannot drop the currently open database`: - // We need to create another connection without the database to drop it - let conn_without_db = Database::connect(base_db_url.as_str()) - .await - .map_err(|_| anyhow!("Failed to connect to the database")) - .unwrap(); - TestDbGuard::drop_database(&conn_without_db, db_name.as_str()) - .await - .map_err(|e| anyhow!("Failed to drop the database: {}", e)) - .unwrap(); - }); - } -} From b06d99c1cf2bf08e8f2622dee4628f33004cda7f Mon Sep 17 00:00:00 2001 From: Leonid Tyurin Date: Mon, 16 Oct 2023 22:25:35 +0400 Subject: [PATCH 16/18] Update version --- libs/blockscout-service-launcher/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/blockscout-service-launcher/Cargo.toml b/libs/blockscout-service-launcher/Cargo.toml index 4731a250f..ca9d1ca3f 100644 --- a/libs/blockscout-service-launcher/Cargo.toml +++ b/libs/blockscout-service-launcher/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "blockscout-service-launcher" -version = "0.8.1" +version = "0.9.0" description = "Allows to launch blazingly fast blockscout rust services" license = "MIT" repository = "https://github.com/blockscout/blockscout-rs" @@ -96,4 +96,4 @@ test-server = [ test-database = [ "database", "dep:keccak-hash", -] \ No newline at end of file +] From b14d2e10fcf215cc20819ebb319c4aabda9b98e1 Mon Sep 17 00:00:00 2001 From: Leonid Tyurin Date: Mon, 16 Oct 2023 22:44:43 +0400 Subject: [PATCH 17/18] Restore tonic version --- libs/blockscout-service-launcher/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/blockscout-service-launcher/Cargo.toml b/libs/blockscout-service-launcher/Cargo.toml index ca9d1ca3f..c52691668 100644 --- a/libs/blockscout-service-launcher/Cargo.toml +++ b/libs/blockscout-service-launcher/Cargo.toml @@ -24,7 +24,7 @@ prometheus = { version = "0.13", optional = true } reqwest = { version = "0.11", features = ["json"], optional = true } serde = { version = "1.0", features = ["derive"], optional = true } tokio = { version = "1", optional = true } -tonic = { version = "0.8", optional = true } +tonic = { version = ">= 0.8, < 0.10", optional = true } tracing = { version = "0.1", optional = true } tracing-opentelemetry = { version = "0.19", optional = true } tracing-subscriber = { version = "0.3", features = ["env-filter", "json"], optional = true } From 02ccfdcb1e5566bd06044119fc2ef47c8996b088 Mon Sep 17 00:00:00 2001 From: Leonid Tyurin Date: Mon, 16 Oct 2023 23:05:16 +0400 Subject: [PATCH 18/18] Add annotated request versions --- .../src/test_server.rs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/libs/blockscout-service-launcher/src/test_server.rs b/libs/blockscout-service-launcher/src/test_server.rs index 7a4247b51..8628fc4f0 100644 --- a/libs/blockscout-service-launcher/src/test_server.rs +++ b/libs/blockscout-service-launcher/src/test_server.rs @@ -80,6 +80,23 @@ async fn send_annotated_request serde::Deserialize<'a>>( .await .unwrap_or_else(|_| panic!("({annotation})Response deserialization failed")) } + +pub async fn send_annotated_post_request serde::Deserialize<'a>>( + url: &Url, + route: &str, + payload: &impl serde::Serialize, + annotation: &str, +) -> Response { + send_annotated_request( + url, + route, + reqwest::Method::POST, + Some(payload), + Some(annotation), + ) + .await +} + pub async fn send_post_request serde::Deserialize<'a>>( url: &Url, route: &str, @@ -88,6 +105,21 @@ pub async fn send_post_request serde::Deserialize<'a>>( send_annotated_request(url, route, reqwest::Method::POST, Some(payload), None).await } +pub async fn send_annotated_get_request serde::Deserialize<'a>>( + url: &Url, + route: &str, + annotation: &str, +) -> Response { + send_annotated_request( + url, + route, + reqwest::Method::GET, + None::<&()>, + Some(annotation), + ) + .await +} + pub async fn send_get_request serde::Deserialize<'a>>( url: &Url, route: &str,