-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add test for more than 32 connections (#93)
Making sure that this works on Windows after #88
- Loading branch information
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,3 +49,4 @@ features = [ | |
|
||
[dev-dependencies] | ||
easy-parallel = "3.1.0" | ||
fastrand = "1.9.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |