Skip to content
Merged
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
17 changes: 6 additions & 11 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,22 +215,17 @@ pub(crate) fn random_storage_path() -> PathBuf {
temp_path
}

pub(crate) fn random_port() -> u16 {
let mut rng = rng();
rng.random_range(5000..32768)
}

pub(crate) fn random_listening_addresses() -> Vec<SocketAddress> {
let num_addresses = 2;
let mut listening_addresses = Vec::with_capacity(num_addresses);
let mut listening_addresses = HashSet::new();

for _ in 0..num_addresses {
let rand_port = random_port();
let address: SocketAddress = format!("127.0.0.1:{}", rand_port).parse().unwrap();
listening_addresses.push(address);
while listening_addresses.len() < num_addresses {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the former for loop can remain?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I'm not sure that there is a guarantee that we don't get the same port back if we drop socket each iteration. I guess we could push them to a Vec to keep them alive while we're still iterating, but this seemed easier.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm although obvious, I didn't realize that we don't actually use the socket. In that case, it definitely seems better to keep the sockets alive?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm although obvious, I didn't realize that we don't actually use the socket. In that case, it definitely seems better to keep the sockets alive?

No, why? We def. don't want to keep it alive to allow the Node setup a new one?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean during the iteration, for the reason you brought up?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but the OS likely gives back random available ports, and we dedup via the HashSet, so we're good?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idk if that is always the case for all OSes? Seems there is endless loop potential here. It's just test, fine to leave as is, but it doesn't feel 100% solid.

let socket = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let address: SocketAddress = socket.local_addr().unwrap().into();
listening_addresses.insert(address);
}

listening_addresses
listening_addresses.into_iter().collect()
}

pub(crate) fn random_node_alias() -> Option<NodeAlias> {
Expand Down