Skip to content

Commit

Permalink
test: Add test for more than 32 connections (#93)
Browse files Browse the repository at this point in the history
Making sure that this works on Windows after #88
  • Loading branch information
notgull authored Mar 8, 2023
1 parent 24900fb commit f48f2c1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ features = [

[dev-dependencies]
easy-parallel = "3.1.0"
fastrand = "1.9.0"
60 changes: 60 additions & 0 deletions tests/many_connections.rs
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit f48f2c1

Please sign in to comment.