|
| 1 | +#![cfg(test)] |
| 2 | + |
| 3 | +use crate::ThreadPoolBuilder; |
| 4 | +use std::sync::atomic::{AtomicUsize, Ordering}; |
| 5 | +use std::sync::Arc; |
| 6 | +use std::{thread, time}; |
| 7 | + |
| 8 | +#[test] |
| 9 | +fn broadcast_global() { |
| 10 | + let v = crate::broadcast(|ctx| ctx.index()); |
| 11 | + assert!(v.into_iter().eq(0..crate::current_num_threads())); |
| 12 | +} |
| 13 | + |
| 14 | +#[test] |
| 15 | +fn spawn_broadcast_global() { |
| 16 | + let (tx, rx) = crossbeam_channel::unbounded(); |
| 17 | + crate::spawn_broadcast(move |ctx| tx.send(ctx.index()).unwrap()); |
| 18 | + |
| 19 | + let mut v: Vec<_> = rx.into_iter().collect(); |
| 20 | + v.sort_unstable(); |
| 21 | + assert!(v.into_iter().eq(0..crate::current_num_threads())); |
| 22 | +} |
| 23 | + |
| 24 | +#[test] |
| 25 | +fn broadcast_pool() { |
| 26 | + let pool = ThreadPoolBuilder::new().num_threads(7).build().unwrap(); |
| 27 | + let v = pool.broadcast(|ctx| ctx.index()); |
| 28 | + assert!(v.into_iter().eq(0..7)); |
| 29 | +} |
| 30 | + |
| 31 | +#[test] |
| 32 | +fn spawn_broadcast_pool() { |
| 33 | + let (tx, rx) = crossbeam_channel::unbounded(); |
| 34 | + let pool = ThreadPoolBuilder::new().num_threads(7).build().unwrap(); |
| 35 | + pool.spawn_broadcast(move |ctx| tx.send(ctx.index()).unwrap()); |
| 36 | + |
| 37 | + let mut v: Vec<_> = rx.into_iter().collect(); |
| 38 | + v.sort_unstable(); |
| 39 | + assert!(v.into_iter().eq(0..7)); |
| 40 | +} |
| 41 | + |
| 42 | +#[test] |
| 43 | +fn broadcast_self() { |
| 44 | + let pool = ThreadPoolBuilder::new().num_threads(7).build().unwrap(); |
| 45 | + let v = pool.install(|| crate::broadcast(|ctx| ctx.index())); |
| 46 | + assert!(v.into_iter().eq(0..7)); |
| 47 | +} |
| 48 | + |
| 49 | +#[test] |
| 50 | +fn spawn_broadcast_self() { |
| 51 | + let (tx, rx) = crossbeam_channel::unbounded(); |
| 52 | + let pool = ThreadPoolBuilder::new().num_threads(7).build().unwrap(); |
| 53 | + pool.spawn(|| crate::spawn_broadcast(move |ctx| tx.send(ctx.index()).unwrap())); |
| 54 | + |
| 55 | + let mut v: Vec<_> = rx.into_iter().collect(); |
| 56 | + v.sort_unstable(); |
| 57 | + assert!(v.into_iter().eq(0..7)); |
| 58 | +} |
| 59 | + |
| 60 | +#[test] |
| 61 | +fn broadcast_mutual() { |
| 62 | + let count = AtomicUsize::new(0); |
| 63 | + let pool1 = ThreadPoolBuilder::new().num_threads(3).build().unwrap(); |
| 64 | + let pool2 = ThreadPoolBuilder::new().num_threads(7).build().unwrap(); |
| 65 | + pool1.install(|| { |
| 66 | + pool2.broadcast(|_| { |
| 67 | + pool1.broadcast(|_| { |
| 68 | + count.fetch_add(1, Ordering::Relaxed); |
| 69 | + }) |
| 70 | + }) |
| 71 | + }); |
| 72 | + assert_eq!(count.into_inner(), 3 * 7); |
| 73 | +} |
| 74 | + |
| 75 | +#[test] |
| 76 | +fn spawn_broadcast_mutual() { |
| 77 | + let (tx, rx) = crossbeam_channel::unbounded(); |
| 78 | + let pool1 = Arc::new(ThreadPoolBuilder::new().num_threads(3).build().unwrap()); |
| 79 | + let pool2 = ThreadPoolBuilder::new().num_threads(7).build().unwrap(); |
| 80 | + pool1.spawn({ |
| 81 | + let pool1 = Arc::clone(&pool1); |
| 82 | + move || { |
| 83 | + pool2.spawn_broadcast(move |_| { |
| 84 | + let tx = tx.clone(); |
| 85 | + pool1.spawn_broadcast(move |_| tx.send(()).unwrap()) |
| 86 | + }) |
| 87 | + } |
| 88 | + }); |
| 89 | + assert_eq!(rx.into_iter().count(), 3 * 7); |
| 90 | +} |
| 91 | + |
| 92 | +#[test] |
| 93 | +fn broadcast_mutual_sleepy() { |
| 94 | + let count = AtomicUsize::new(0); |
| 95 | + let pool1 = ThreadPoolBuilder::new().num_threads(3).build().unwrap(); |
| 96 | + let pool2 = ThreadPoolBuilder::new().num_threads(7).build().unwrap(); |
| 97 | + pool1.install(|| { |
| 98 | + thread::sleep(time::Duration::from_secs(1)); |
| 99 | + pool2.broadcast(|_| { |
| 100 | + thread::sleep(time::Duration::from_secs(1)); |
| 101 | + pool1.broadcast(|_| { |
| 102 | + thread::sleep(time::Duration::from_millis(100)); |
| 103 | + count.fetch_add(1, Ordering::Relaxed); |
| 104 | + }) |
| 105 | + }) |
| 106 | + }); |
| 107 | + assert_eq!(count.into_inner(), 3 * 7); |
| 108 | +} |
| 109 | + |
| 110 | +#[test] |
| 111 | +fn spawn_broadcast_mutual_sleepy() { |
| 112 | + let (tx, rx) = crossbeam_channel::unbounded(); |
| 113 | + let pool1 = Arc::new(ThreadPoolBuilder::new().num_threads(3).build().unwrap()); |
| 114 | + let pool2 = ThreadPoolBuilder::new().num_threads(7).build().unwrap(); |
| 115 | + pool1.spawn({ |
| 116 | + let pool1 = Arc::clone(&pool1); |
| 117 | + move || { |
| 118 | + thread::sleep(time::Duration::from_secs(1)); |
| 119 | + pool2.spawn_broadcast(move |_| { |
| 120 | + let tx = tx.clone(); |
| 121 | + thread::sleep(time::Duration::from_secs(1)); |
| 122 | + pool1.spawn_broadcast(move |_| { |
| 123 | + thread::sleep(time::Duration::from_millis(100)); |
| 124 | + tx.send(()).unwrap(); |
| 125 | + }) |
| 126 | + }) |
| 127 | + } |
| 128 | + }); |
| 129 | + assert_eq!(rx.into_iter().count(), 3 * 7); |
| 130 | +} |
| 131 | + |
| 132 | +#[test] |
| 133 | +fn broadcast_panic_one() { |
| 134 | + let count = AtomicUsize::new(0); |
| 135 | + let pool = ThreadPoolBuilder::new().num_threads(7).build().unwrap(); |
| 136 | + let result = crate::unwind::halt_unwinding(|| { |
| 137 | + pool.broadcast(|ctx| { |
| 138 | + count.fetch_add(1, Ordering::Relaxed); |
| 139 | + if ctx.index() == 3 { |
| 140 | + panic!("Hello, world!"); |
| 141 | + } |
| 142 | + }) |
| 143 | + }); |
| 144 | + assert_eq!(count.into_inner(), 7); |
| 145 | + assert!(result.is_err(), "broadcast panic should propagate!"); |
| 146 | +} |
| 147 | + |
| 148 | +#[test] |
| 149 | +fn spawn_broadcast_panic_one() { |
| 150 | + let (tx, rx) = crossbeam_channel::unbounded(); |
| 151 | + let (panic_tx, panic_rx) = crossbeam_channel::unbounded(); |
| 152 | + let pool = ThreadPoolBuilder::new() |
| 153 | + .num_threads(7) |
| 154 | + .panic_handler(move |e| panic_tx.send(e).unwrap()) |
| 155 | + .build() |
| 156 | + .unwrap(); |
| 157 | + pool.spawn_broadcast(move |ctx| { |
| 158 | + tx.send(()).unwrap(); |
| 159 | + if ctx.index() == 3 { |
| 160 | + panic!("Hello, world!"); |
| 161 | + } |
| 162 | + }); |
| 163 | + drop(pool); // including panic_tx |
| 164 | + assert_eq!(rx.into_iter().count(), 7); |
| 165 | + assert_eq!(panic_rx.into_iter().count(), 1); |
| 166 | +} |
| 167 | + |
| 168 | +#[test] |
| 169 | +fn broadcast_panic_many() { |
| 170 | + let count = AtomicUsize::new(0); |
| 171 | + let pool = ThreadPoolBuilder::new().num_threads(7).build().unwrap(); |
| 172 | + let result = crate::unwind::halt_unwinding(|| { |
| 173 | + pool.broadcast(|ctx| { |
| 174 | + count.fetch_add(1, Ordering::Relaxed); |
| 175 | + if ctx.index() % 2 == 0 { |
| 176 | + panic!("Hello, world!"); |
| 177 | + } |
| 178 | + }) |
| 179 | + }); |
| 180 | + assert_eq!(count.into_inner(), 7); |
| 181 | + assert!(result.is_err(), "broadcast panic should propagate!"); |
| 182 | +} |
| 183 | + |
| 184 | +#[test] |
| 185 | +fn spawn_broadcast_panic_many() { |
| 186 | + let (tx, rx) = crossbeam_channel::unbounded(); |
| 187 | + let (panic_tx, panic_rx) = crossbeam_channel::unbounded(); |
| 188 | + let pool = ThreadPoolBuilder::new() |
| 189 | + .num_threads(7) |
| 190 | + .panic_handler(move |e| panic_tx.send(e).unwrap()) |
| 191 | + .build() |
| 192 | + .unwrap(); |
| 193 | + pool.spawn_broadcast(move |ctx| { |
| 194 | + tx.send(()).unwrap(); |
| 195 | + if ctx.index() % 2 == 0 { |
| 196 | + panic!("Hello, world!"); |
| 197 | + } |
| 198 | + }); |
| 199 | + drop(pool); // including panic_tx |
| 200 | + assert_eq!(rx.into_iter().count(), 7); |
| 201 | + assert_eq!(panic_rx.into_iter().count(), 4); |
| 202 | +} |
0 commit comments