Skip to content

Commit f5d983a

Browse files
committed
fix(kms-connector): reduce port collision in tests
1 parent 9141432 commit f5d983a

File tree

3 files changed

+10
-17
lines changed
  • kms-connector/crates

3 files changed

+10
-17
lines changed

kms-connector/crates/gw-listener/tests/common/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub async fn start_test_listener(
5656

5757
// Wait for all gw-listener event filters to be ready + 2 anvil blocks
5858
for _ in 0..NB_EVENT_TYPE {
59-
test_instance.wait_for_log("Waiting for next").await;
59+
test_instance.wait_for_log("Subscribed to ").await;
6060
}
6161
tokio::time::sleep(2 * test_instance.anvil_block_time()).await;
6262

kms-connector/crates/utils/src/tests/setup/db.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ use sqlx::{Pool, Postgres};
22
use testcontainers::{ContainerAsync, GenericImage, ImageExt, core::WaitFor, runners::AsyncRunner};
33
use tracing::info;
44

5-
use crate::tests::setup::pick_free_port;
6-
75
const POSTGRES_PORT: u16 = 5432;
86

97
pub struct DbInstance {
@@ -15,20 +13,19 @@ pub struct DbInstance {
1513

1614
impl DbInstance {
1715
pub async fn setup() -> anyhow::Result<Self> {
18-
let host_port = pick_free_port();
1916
info!("Starting Postgres container...");
2017
let container = GenericImage::new("postgres", "17.5")
2118
.with_wait_for(WaitFor::message_on_stderr(
2219
"database system is ready to accept connections",
2320
))
24-
.with_mapped_port(host_port, POSTGRES_PORT.into())
2521
.with_env_var("POSTGRES_USER", "postgres")
2622
.with_env_var("POSTGRES_PASSWORD", "postgres")
2723
.start()
2824
.await?;
2925
info!("Postgres container ready!");
3026

3127
let cont_host = container.get_host().await?;
28+
let host_port = container.get_host_port_ipv4(POSTGRES_PORT).await?;
3229
let admin_db_url =
3330
format!("postgresql://postgres:postgres@{cont_host}:{host_port}/postgres");
3431
let db_url =

kms-connector/crates/utils/src/tests/setup/gw.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{
22
config::KmsWallet,
33
conn::WalletGatewayProvider,
44
provider::{FillersWithoutNonceManagement, NonceManagedProvider},
5-
tests::setup::{ROOT_CARGO_TOML, pick_free_port},
5+
tests::setup::ROOT_CARGO_TOML,
66
};
77
use alloy::{
88
primitives::{Address, ChainId, FixedBytes},
@@ -17,7 +17,7 @@ use fhevm_gateway_bindings::{
1717
use std::{sync::LazyLock, time::Duration};
1818
use testcontainers::{
1919
ContainerAsync, GenericImage, ImageExt,
20-
core::{WaitFor, client::docker_client_instance},
20+
core::{ContainerPort, WaitFor, client::docker_client_instance},
2121
runners::AsyncRunner,
2222
};
2323
use tracing::info;
@@ -78,9 +78,10 @@ impl GatewayInstance {
7878

7979
pub async fn setup() -> anyhow::Result<Self> {
8080
let block_time = 1;
81-
let anvil_host_port = pick_free_port();
82-
let anvil: ContainerAsync<GenericImage> =
83-
setup_anvil_gateway(anvil_host_port, block_time).await?;
81+
82+
let anvil = setup_anvil_gateway(block_time).await?;
83+
let anvil_host_port = anvil.get_host_port_ipv4(ANVIL_PORT).await?;
84+
8485
let wallet = KmsWallet::from_private_key_str(
8586
DEPLOYER_PRIVATE_KEY,
8687
Some(ChainId::from(*CHAIN_ID as u64)),
@@ -118,27 +119,22 @@ impl GatewayInstance {
118119
}
119120
}
120121

121-
pub async fn setup_anvil_gateway(
122-
host_port: u16,
123-
block_time: u64,
124-
) -> anyhow::Result<ContainerAsync<GenericImage>> {
122+
pub async fn setup_anvil_gateway(block_time: u64) -> anyhow::Result<ContainerAsync<GenericImage>> {
125123
info!("Starting Anvil...");
126124
let anvil = GenericImage::new("ghcr.io/foundry-rs/foundry", "v1.3.5")
125+
.with_exposed_port(ContainerPort::Tcp(ANVIL_PORT))
127126
.with_wait_for(WaitFor::message_on_stdout("Listening"))
128127
.with_entrypoint("anvil")
129128
.with_cmd([
130129
"--host",
131130
"0.0.0.0",
132-
"--port",
133-
ANVIL_PORT.to_string().as_str(),
134131
"--chain-id",
135132
CHAIN_ID.to_string().as_str(),
136133
"--mnemonic",
137134
TEST_MNEMONIC,
138135
"--block-time",
139136
&format!("{block_time}"),
140137
])
141-
.with_mapped_port(host_port, ANVIL_PORT.into())
142138
.start()
143139
.await?;
144140

0 commit comments

Comments
 (0)