2014-07-11 16:29:15 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-09-01 00:10:45 -05:00
|
|
|
// ignore-macos osx really doesn't like cycling through large numbers of
|
|
|
|
// sockets as calls to connect() will start returning EADDRNOTAVAIL
|
|
|
|
// quite quickly and it takes a few seconds for the sockets to get
|
|
|
|
// recycled.
|
|
|
|
|
2014-07-11 16:29:15 -05:00
|
|
|
use std::io::{TcpListener, Listener, Acceptor, EndOfFile, TcpStream};
|
2014-07-15 14:42:40 -05:00
|
|
|
use std::sync::{atomic, Arc};
|
2014-07-11 16:29:15 -05:00
|
|
|
|
|
|
|
static N: uint = 8;
|
2014-09-01 00:10:45 -05:00
|
|
|
static M: uint = 20;
|
2014-07-11 16:29:15 -05:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
test();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test() {
|
|
|
|
let mut l = TcpListener::bind("127.0.0.1", 0).unwrap();
|
|
|
|
let addr = l.socket_name().unwrap();
|
|
|
|
let mut a = l.listen().unwrap();
|
2014-07-15 14:42:40 -05:00
|
|
|
let cnt = Arc::new(atomic::AtomicUint::new(0));
|
2014-07-11 16:29:15 -05:00
|
|
|
|
2014-09-01 00:10:45 -05:00
|
|
|
let (srv_tx, srv_rx) = channel();
|
|
|
|
let (cli_tx, cli_rx) = channel();
|
2014-07-11 16:29:15 -05:00
|
|
|
for _ in range(0, N) {
|
|
|
|
let a = a.clone();
|
|
|
|
let cnt = cnt.clone();
|
2014-09-01 00:10:45 -05:00
|
|
|
let srv_tx = srv_tx.clone();
|
2014-07-11 16:29:15 -05:00
|
|
|
spawn(proc() {
|
|
|
|
let mut a = a;
|
|
|
|
loop {
|
|
|
|
match a.accept() {
|
|
|
|
Ok(..) => {
|
2014-07-15 14:42:40 -05:00
|
|
|
if cnt.fetch_add(1, atomic::SeqCst) == N * M - 1 {
|
2014-07-11 16:29:15 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(ref e) if e.kind == EndOfFile => break,
|
2014-10-09 14:17:22 -05:00
|
|
|
Err(e) => panic!("{}", e),
|
2014-07-11 16:29:15 -05:00
|
|
|
}
|
|
|
|
}
|
2014-09-01 00:10:45 -05:00
|
|
|
srv_tx.send(());
|
2014-07-11 16:29:15 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for _ in range(0, N) {
|
2014-09-01 00:10:45 -05:00
|
|
|
let cli_tx = cli_tx.clone();
|
2014-07-11 16:29:15 -05:00
|
|
|
spawn(proc() {
|
|
|
|
for _ in range(0, M) {
|
|
|
|
let _s = TcpStream::connect(addr.ip.to_string().as_slice(),
|
|
|
|
addr.port).unwrap();
|
|
|
|
}
|
2014-09-01 00:10:45 -05:00
|
|
|
cli_tx.send(());
|
2014-07-11 16:29:15 -05:00
|
|
|
});
|
|
|
|
}
|
2014-09-01 00:10:45 -05:00
|
|
|
drop((cli_tx, srv_tx));
|
2014-07-11 16:29:15 -05:00
|
|
|
|
|
|
|
// wait for senders
|
2014-09-01 00:10:45 -05:00
|
|
|
if cli_rx.iter().take(N).count() != N {
|
|
|
|
a.close_accept().unwrap();
|
2014-10-09 14:17:22 -05:00
|
|
|
panic!("clients panicked");
|
2014-09-01 00:10:45 -05:00
|
|
|
}
|
2014-07-11 16:29:15 -05:00
|
|
|
|
|
|
|
// wait for one acceptor to die
|
2014-09-01 00:10:45 -05:00
|
|
|
let _ = srv_rx.recv();
|
2014-07-11 16:29:15 -05:00
|
|
|
|
|
|
|
// Notify other receivers should die
|
|
|
|
a.close_accept().unwrap();
|
|
|
|
|
|
|
|
// wait for receivers
|
2014-09-01 00:10:45 -05:00
|
|
|
assert_eq!(srv_rx.iter().take(N - 1).count(), N - 1);
|
2014-07-11 16:29:15 -05:00
|
|
|
|
|
|
|
// Everything should have been accepted.
|
2014-07-15 14:42:40 -05:00
|
|
|
assert_eq!(cnt.load(atomic::SeqCst), N * M);
|
2014-07-11 16:29:15 -05:00
|
|
|
}
|