Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions core/crates/account/src/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,11 +745,9 @@ mod tests {
MachineId::derive("4c4f5e2a0b6d4f3c9e8a7b6c5d4e3f20\n").expect("a machine ID")
}

// Nothing listens on the port once the listener is dropped, so every request is refused.
// Port 1 is below every system's ephemeral range, so no test's listener takes it and every request is refused.
fn unreachable() -> Endpoints {
let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("a free port");
let base = format!("http://{}", listener.local_addr().expect("its address"));
Endpoints::new(&base, &format!("{base}/v2")).expect("the endpoints")
Endpoints::new("http://127.0.0.1:1", "http://127.0.0.1:1/v2").expect("the endpoints")
}

// Stands in for tablepro-cloud: `answer` gives the status and body for a method and path.
Expand Down
6 changes: 2 additions & 4 deletions core/crates/account/src/account/share_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -905,10 +905,8 @@ mod tests {
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_network_failure_never_words_the_share_token() {
let dir = tempfile::tempdir().expect("a temporary directory");
let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("a free port");
let base = format!("http://{}", listener.local_addr().expect("its address"));
drop(listener);
let endpoints = Endpoints::new(&base, &format!("{base}/v2")).expect("the endpoints");
// Port 1 is below every system's ephemeral range, so no test's listener takes it and the request is refused.
let endpoints = Endpoints::new("http://127.0.0.1:1", "http://127.0.0.1:1/v2").expect("the endpoints");
let account = signed_in(dir.path(), endpoints).await;

let failed = account.fetch_shared(TOKEN, None).await.expect_err("nothing listens");
Expand Down
6 changes: 2 additions & 4 deletions core/crates/ai/tests/providers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,10 +478,8 @@ async fn the_models_are_listed_sorted() {

#[tokio::test(flavor = "multi_thread")]
async fn a_closed_port_says_it_could_not_reach_the_host() {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let address = listener.local_addr().unwrap();
drop(listener);
let endpoint = Endpoint::new(AiProvider::OpenAiCompatible, &format!("http://{address}/v1"), "m", None).unwrap();
// Port 1 is below every system's ephemeral range, so no test's listener takes it and the request is refused.
let endpoint = Endpoint::new(AiProvider::OpenAiCompatible, "http://127.0.0.1:1/v1", "m", None).unwrap();

let models = list_models(&client(), &endpoint).await;

Expand Down
6 changes: 2 additions & 4 deletions core/crates/cli/tests/cli_over_mcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,11 +546,9 @@ async fn a_changed_token_is_refused_and_a_closed_port_is_unreachable() {
token: Some("tp_other".to_owned()),
..Machine::of(&harness)
};
let free = std::net::TcpListener::bind("127.0.0.1:0").expect("a free port");
let closed_port = free.local_addr().expect("an address").port();
drop(free);
// Port 1 is below every system's ephemeral range, so no test's listener takes it and the connect is refused.
let silent = Machine {
port: closed_port,
port: 1,
..Machine::of(&harness)
};

Expand Down
13 changes: 5 additions & 8 deletions core/crates/drivers/clickhouse/src/editor_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,12 +487,9 @@ mod tests {
)
}

/// A port nothing listens on, so a request is refused at once.
async fn closed_route() -> clickhouse::Client {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.expect("a listener");
let address = listener.local_addr().expect("an address");
drop(listener);
clickhouse::Client::default().with_url(format!("http://{address}"))
// Port 1 is below every system's ephemeral range, so no test's listener takes it and a request is refused at once.
fn closed_route() -> clickhouse::Client {
clickhouse::Client::default().with_url("http://127.0.0.1:1")
}

struct Discard;
Expand Down Expand Up @@ -526,7 +523,7 @@ mod tests {
let lingering = Arc::new(LingeringQuery::default());
lingering.left(Uuid::new_v4());
let run = EditorRun {
session: closed_route().await,
session: closed_route(),
control,
statement_timeout: None,
bounds_on_server: true,
Expand Down Expand Up @@ -558,7 +555,7 @@ mod tests {
for bounds_on_server in [false, true] {
let run = EditorRun {
session: clickhouse::Client::default().with_url(url.clone()),
control: closed_route().await,
control: closed_route(),
statement_timeout: None,
bounds_on_server,
grace: Duration::from_millis(200),
Expand Down
11 changes: 4 additions & 7 deletions core/crates/drivers/mongodb/tests/integration/replica_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use crate::support::{CALL, DATABASE, connect_to, direct, editor, first_page, pas
/// The name the set's one member gives itself.
const MEMBER: &str = "localhost.test.build.10gen.cc:27017";

// Port 1 is below every system's ephemeral range, so no test's listener takes it and the seed is refused.
const CLOSED_PORT: u16 = 1;

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[ignore = "requires docker, public DNS and host port 27017"]
async fn a_replica_set_is_found_from_its_seeds_and_read_through_its_primary() {
Expand All @@ -22,7 +25,7 @@ async fn a_replica_set_is_found_from_its_seeds_and_read_through_its_primary() {
let liveness = test_liveness();
// Only the second seed reaches the set, so the set is found from the host list or not at all.
let target = NetworkTarget {
transport: direct("127.0.0.1", closed_port(), &liveness),
transport: direct("127.0.0.1", CLOSED_PORT, &liveness),
credentials: password(&fixture.password_credentials()),
database: Some(DATABASE.to_owned()),
tls: TlsConfig::disabled(),
Expand Down Expand Up @@ -59,9 +62,3 @@ async fn a_replica_set_is_found_from_its_seeds_and_read_through_its_primary() {
session.close().await;
connection.close().await.expect("a clean close");
}

/// A port on this host that nothing listens on.
fn closed_port() -> u16 {
let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("a free port");
listener.local_addr().expect("the port bound").port()
}
5 changes: 2 additions & 3 deletions core/crates/ffi/tests/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,9 @@ async fn open(base: &str) -> Opened {
}
}

// Nothing listens once the listener is dropped, so every request is refused.
// Port 1 is below every system's ephemeral range, so no test's listener takes it and every request is refused.
fn unreachable() -> String {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
format!("http://{}", listener.local_addr().unwrap())
"http://127.0.0.1:1".to_owned()
}

// Stands in for tablepro-cloud: `answer` gives the status and body for a method, path and request body.
Expand Down
16 changes: 9 additions & 7 deletions core/crates/net/src/dial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ mod tests {
Instant::now() + liveness.connect_timeout
}

// Port 1 is below every system's ephemeral range, so no test's listener takes it and a connect is refused.
fn refused() -> SocketAddr {
SocketAddr::from(([127, 0, 0, 1], 1))
}

fn full_backlog_listener() -> (Socket, SocketAddr, Vec<std::net::TcpStream>) {
let socket = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap();
socket.bind(&SocketAddr::from(([127, 0, 0, 1], 0)).into()).unwrap();
Expand All @@ -163,14 +168,13 @@ mod tests {
}

#[tokio::test]
async fn dropped_port_then_live_listener_connects_second() {
let dropped = StdListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap();
async fn refused_port_then_live_listener_connects_second() {
let live = StdListener::bind("127.0.0.1:0").unwrap();
let live_address = live.local_addr().unwrap();
let endpoint = NetworkEndpoint::new("127.0.0.1", live_address.port()).unwrap();
let liveness = quick_liveness();

let stream = dial_addresses(vec![dropped, live_address], &endpoint, &liveness, deadline(&liveness))
let stream = dial_addresses(vec![refused(), live_address], &endpoint, &liveness, deadline(&liveness))
.await
.unwrap();
assert_eq!(stream.peer_addr().unwrap(), live_address);
Expand Down Expand Up @@ -243,13 +247,11 @@ mod tests {

#[tokio::test]
async fn every_address_refused_is_refused() {
let first = StdListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap();
let second = StdListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap();
let endpoint = NetworkEndpoint::new("127.0.0.1", first.port()).unwrap();
let endpoint = NetworkEndpoint::new("127.0.0.1", refused().port()).unwrap();
let liveness = racing_liveness();

assert_eq!(
dial_addresses(vec![first, second], &endpoint, &liveness, deadline(&liveness))
dial_addresses(vec![refused(), refused()], &endpoint, &liveness, deadline(&liveness))
.await
.err(),
Some(TransportError::Refused {
Expand Down
7 changes: 3 additions & 4 deletions core/crates/net/src/tls/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,9 @@ mod tests {

#[tokio::test]
async fn nothing_listening_does_not_serve_tls() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
drop(listener);
// Port 1 is below every system's ephemeral range, so no test's listener takes it and the connect is refused.
let refused = SocketAddr::from(([127, 0, 0, 1], 1));

assert!(!serves_tls(&transport(address), NAME, WITHIN).await);
assert!(!serves_tls(&transport(refused), NAME, WITHIN).await);
}
}
11 changes: 2 additions & 9 deletions windows/tests/TablePro.Interop.Tests/AccountTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,8 @@ private static async Task<AccountStatus> FirstChangeAsync(TableProAccount accoun
private static string Problem(int status, string code) =>
$$"""{"type":"about:blank","title":"Refused","status":{{status}},"code":"{{code}}"}""";

// Nothing listens once the listener is stopped, so every request is refused.
private static string Unreachable()
{
var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
var port = ((IPEndPoint)listener.LocalEndpoint).Port;
listener.Stop();
return $"http://127.0.0.1:{port}";
}
// Port 1 is below every system's ephemeral range, so no test's listener takes it and every request is refused.
private static string Unreachable() => "http://127.0.0.1:1";

private Task<TableProAccount> OpenAsync(string cloud, IAccountBrowser browser) =>
_core.OpenAccountAsync(Configuration(cloud), browser);
Expand Down
Loading