| 1 | use std::{panic::catch_unwind, thread, time::Duration}; |
| 2 | |
| 3 | use futures_timer::Delay; |
| 4 | use threadfin::ThreadPool; |
| 5 | |
| 6 | fn single_thread() -> ThreadPool { |
| 7 | ThreadPool::builder().size(0..1).build() |
| 8 | } |
| 9 | |
| 10 | #[test] |
| 11 | #[should_panic(expected = "thread pool name must not contain null bytes")] |
| 12 | fn name_with_null_bytes_panics() { |
| 13 | ThreadPool::builder().name("uh\0oh").build(); |
| 14 | } |
| 15 | |
| 16 | #[test] |
| 17 | #[allow(clippy::reversed_empty_ranges)] |
| 18 | #[should_panic(expected = "thread pool minimum size cannot be larger than maximum size")] |
| 19 | fn invalid_size_panics() { |
| 20 | ThreadPool::builder().size(2..1); |
| 21 | } |
| 22 | |
| 23 | #[test] |
| 24 | #[should_panic(expected = "thread pool maximum size must be non-zero")] |
| 25 | fn invalid_size_zero_panics() { |
| 26 | ThreadPool::builder().size(0); |
| 27 | } |
| 28 | |
| 29 | #[test] |
| 30 | fn execute() { |
| 31 | let pool = single_thread(); |
| 32 | |
| 33 | let result = pool.execute(|| 2 + 2).join(); |
| 34 | |
| 35 | assert_eq!(result, 4); |
| 36 | } |
| 37 | |
| 38 | #[test] |
| 39 | fn execute_future() { |
| 40 | let pool = single_thread(); |
| 41 | |
| 42 | let result = pool.execute_future(async { 2 + 2 }).join(); |
| 43 | |
| 44 | assert_eq!(result, 4); |
| 45 | } |
| 46 | |
| 47 | #[test] |
| 48 | fn task_join_timeout() { |
| 49 | let pool = single_thread(); |
| 50 | |
| 51 | let result = pool |
| 52 | .execute(|| thread::sleep(Duration::from_secs(5))) |
| 53 | .join_timeout(Duration::from_millis(10)); |
| 54 | |
| 55 | assert!(result.is_err()); |
| 56 | } |
| 57 | |
| 58 | #[test] |
| 59 | fn futures_that_yield_are_run_concurrently() { |
| 60 | let pool = single_thread(); |
| 61 | |
| 62 | assert_eq!(pool.running_tasks(), 0); |
| 63 | |
| 64 | let first = pool |
| 65 | .try_execute_future(Delay::new(Duration::from_millis(400))) |
| 66 | .unwrap(); |
| 67 | |
| 68 | // Even though there's only one worker thread, it should become idle quickly |
| 69 | // and start polling for more work, because a delay future yields |
| 70 | // immediately and doesn't wake for a while. |
| 71 | thread::sleep(Duration::from_millis(100)); |
| 72 | |
| 73 | assert_eq!(pool.running_tasks(), 1); |
| 74 | |
| 75 | let second = pool |
| 76 | .try_execute_future(Delay::new(Duration::from_millis(200))) |
| 77 | .unwrap(); |
| 78 | |
| 79 | thread::sleep(Duration::from_millis(100)); |
| 80 | |
| 81 | // Now both tasks are running, but there's still only 1 worker thread! |
| 82 | assert_eq!(pool.running_tasks(), 2); |
| 83 | assert_eq!(pool.threads(), 1); |
| 84 | |
| 85 | first.join(); |
| 86 | second.join(); |
| 87 | |
| 88 | // Both tasks completed. |
| 89 | assert_eq!(pool.completed_tasks(), 2); |
| 90 | } |
| 91 | |
| 92 | #[test] |
| 93 | fn try_execute_under_core_count() { |
| 94 | let pool = ThreadPool::builder().size(1).build(); |
| 95 | |
| 96 | // Give some time for thread to start... |
| 97 | thread::sleep(Duration::from_millis(100)); |
| 98 | assert_eq!(pool.threads(), 1); |
| 99 | |
| 100 | assert!(pool.try_execute(|| 2 + 2).is_ok()); |
| 101 | } |
| 102 | |
| 103 | #[test] |
| 104 | fn try_execute_over_core_count() { |
| 105 | let pool = ThreadPool::builder().size(0..1).build(); |
| 106 | |
| 107 | assert!(pool.try_execute(|| 2 + 2).is_ok()); |
| 108 | } |
| 109 | |
| 110 | #[test] |
| 111 | fn try_execute_over_limit() { |
| 112 | let pool = ThreadPool::builder().size(0..1).queue_limit(0).build(); |
| 113 | |
| 114 | assert!(pool.try_execute(|| 2 + 2).is_ok()); |
| 115 | assert!(pool.try_execute(|| 2 + 2).is_err()); |
| 116 | |
| 117 | fn task() -> usize { |
| 118 | 2 + 2 |
| 119 | } |
| 120 | |
| 121 | // The returned function in the error is identical to the function given. |
| 122 | let error = pool.try_execute(task).unwrap_err(); |
| 123 | assert_eq!(error.into_inner() as usize, task as usize); |
| 124 | } |
| 125 | |
| 126 | #[test] |
| 127 | fn name() { |
| 128 | let pool = ThreadPool::builder().name("foo").build(); |
| 129 | |
| 130 | let name = pool |
| 131 | .execute(|| thread::current().name().unwrap().to_owned()) |
| 132 | .join(); |
| 133 | |
| 134 | assert_eq!(name, "foo"); |
| 135 | } |
| 136 | |
| 137 | #[test] |
| 138 | #[should_panic(expected = "oh no!")] |
| 139 | fn panic_propagates_to_task() { |
| 140 | let pool = single_thread(); |
| 141 | |
| 142 | pool.execute(|| panic!("oh no!")).join(); |
| 143 | } |
| 144 | |
| 145 | #[test] |
| 146 | fn panic_count() { |
| 147 | let pool = single_thread(); |
| 148 | assert_eq!(pool.panicked_tasks(), 0); |
| 149 | |
| 150 | let task = pool.execute(|| panic!("oh no!")); |
| 151 | let _ = catch_unwind(move || { |
| 152 | task.join(); |
| 153 | }); |
| 154 | |
| 155 | assert_eq!(pool.panicked_tasks(), 1); |
| 156 | } |
| 157 | |
| 158 | #[test] |
| 159 | fn thread_count() { |
| 160 | let pool = ThreadPool::builder().size(0..1).build(); |
| 161 | |
| 162 | assert_eq!(pool.threads(), 0); |
| 163 | |
| 164 | pool.execute(|| 2 + 2).join(); |
| 165 | assert_eq!(pool.threads(), 1); |
| 166 | |
| 167 | let pool_with_starting_threads = ThreadPool::builder().size(1).build(); |
| 168 | |
| 169 | // Give some time for thread to start... |
| 170 | thread::sleep(Duration::from_millis(50)); |
| 171 | assert_eq!(pool_with_starting_threads.threads(), 1); |
| 172 | } |
| 173 | |
| 174 | #[test] |
| 175 | fn idle_shutdown() { |
| 176 | let pool = ThreadPool::builder() |
| 177 | .size(0..1) |
| 178 | .keep_alive(Duration::from_millis(100)) |
| 179 | .build(); |
| 180 | assert_eq!(pool.threads(), 0, "pool starts out empty"); |
| 181 | |
| 182 | pool.execute(|| 2 + 2).join(); |
| 183 | assert_eq!(pool.threads(), 1, "one thread was added"); |
| 184 | |
| 185 | thread::sleep(Duration::from_millis(200)); |
| 186 | assert_eq!( |
| 187 | pool.threads(), |
| 188 | 0, |
| 189 | "thread became idle and terminated after timeout" |
| 190 | ); |
| 191 | } |
| 192 | |
| 193 | #[test] |
| 194 | fn join() { |
| 195 | // Just a dumb test to make sure join doesn't do anything strange. |
| 196 | ThreadPool::default().join(); |
| 197 | } |
| 198 | |
| 199 | #[test] |
| 200 | fn join_timeout_expiring() { |
| 201 | let pool = ThreadPool::builder().size(1).build(); |
| 202 | assert_eq!(pool.threads(), 1); |
| 203 | |
| 204 | // Schedule a slow task on the only thread. We have to keep the task |
| 205 | // around, because dropping it could cancel the task. |
| 206 | let _task = pool.execute(|| thread::sleep(Duration::from_millis(500))); |
| 207 | |
| 208 | // Joining should time out since there's one task still running longer |
| 209 | // than our join timeout. |
| 210 | assert!(!pool.join_timeout(Duration::from_millis(10))); |
| 211 | } |
| 212 | |
| 213 | #[test] |
| 214 | fn configure_common_after_init_returns_error() { |
| 215 | threadfin::common(); // init |
| 216 | |
| 217 | assert!(threadfin::configure_common(|b| b).is_err()); |
| 218 | } |