test: Add test for more than 32 connections (#93)

Making sure that this works on Windows after #88
This commit is contained in:
John Nunley 2023-03-08 10:40:23 -08:00 committed by GitHub
parent 24900fb662
commit f48f2c1a5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 0 deletions

View File

@ -49,3 +49,4 @@ features = [
[dev-dependencies]
easy-parallel = "3.1.0"
fastrand = "1.9.0"

60
tests/many_connections.rs Normal file
View File

@ -0,0 +1,60 @@
//! Tests to ensure more than 32 connections can be polled at once.
// Doesn't work on OpenBSD.
#![cfg(not(target_os = "openbsd"))]
use std::io::{self, prelude::*};
use std::net::{TcpListener, TcpStream};
use std::time::Duration;
#[test]
fn many_connections() {
// Create 100 connections.
let mut connections = Vec::new();
for i in 0..100 {
let (reader, writer) = tcp_pair().unwrap();
connections.push((i, reader, writer));
}
// Create a poller and add all the connections.
let poller = polling::Poller::new().unwrap();
for (i, reader, _) in connections.iter() {
poller.add(reader, polling::Event::readable(*i)).unwrap();
}
let mut events = vec![];
while !connections.is_empty() {
// Choose a random connection to write to.
let i = fastrand::usize(..connections.len());
let (id, mut reader, mut writer) = connections.remove(i);
// Write a byte to the connection.
writer.write_all(&[1]).unwrap();
// Wait for the connection to become readable.
poller
.wait(&mut events, Some(Duration::from_secs(10)))
.unwrap();
// Check that the connection is readable.
assert_eq!(events.len(), 1, "events: {:?}", events);
assert_eq!(events[0].key, id);
assert!(events[0].readable);
assert!(!events[0].writable);
// Read the byte from the connection.
let mut buf = [0];
reader.read_exact(&mut buf).unwrap();
assert_eq!(buf, [1]);
poller.delete(&reader).unwrap();
events.clear();
}
}
fn tcp_pair() -> io::Result<(TcpStream, TcpStream)> {
let listener = TcpListener::bind("127.0.0.1:0")?;
let a = TcpStream::connect(listener.local_addr()?)?;
let (b, _) = listener.accept()?;
Ok((a, b))
}