Skip to content

Commit

Permalink
Add a test for CpuPool shutting down
Browse files Browse the repository at this point in the history
Fix an off-by-one in the count to send shutdown messages.

Closes rust-lang#65
  • Loading branch information
alexcrichton committed Aug 16, 2016
1 parent bce2ad2 commit 992b7f7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion futures-cpupool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl Clone for CpuPool {

impl Drop for CpuPool {
fn drop(&mut self) {
if self.inner.cnt.fetch_sub(1, Ordering::Relaxed) != 0 {
if self.inner.cnt.fetch_sub(1, Ordering::Relaxed) > 1 {
return
}
for _ in 0..self.inner.size {
Expand Down
32 changes: 32 additions & 0 deletions futures-cpupool/tests/smoke.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
extern crate futures;
extern crate futures_cpupool;

use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
use std::sync::mpsc::channel;
use std::thread;
use std::time::Duration;

use futures::Future;
use futures_cpupool::CpuPool;
Expand Down Expand Up @@ -40,3 +43,32 @@ fn select() {
assert!(item1 != item2);
assert!((item1 == 1 && item2 == 2) || (item1 == 2 && item2 == 1));
}

#[test]
fn threads_go_away() {
static CNT: AtomicUsize = ATOMIC_USIZE_INIT;

struct A;

impl Drop for A {
fn drop(&mut self) {
CNT.fetch_add(1, Ordering::SeqCst);
}
}

thread_local!(static FOO: A = A);

let pool = CpuPool::new(2);
get(pool.execute(|| {
FOO.with(|_| ())
})).unwrap();
drop(pool);

for _ in 0..100 {
if CNT.load(Ordering::SeqCst) == 1 {
return
}
thread::sleep(Duration::from_millis(10));
}
panic!("thread didn't exit");
}

0 comments on commit 992b7f7

Please sign in to comment.