| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/::catch_unwindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/catch_unwind()., threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/, timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#}; |
| 2 | |
| 3 | use futures_timerrust-analyzer cargo futures-timer 3.0.4 crate/::Delayrust-analyzer cargo futures-timer 3.0.4 native/delay/Delay#; |
| 4 | use threadfinrust-analyzer cargo threadfin 0.1.2 crate/::ThreadPoolrust-analyzer cargo threadfin 0.1.2 pool/ThreadPool#; |
| 5 | |
| 6 | fn single_threadrust-analyzer cargo threadfin 0.1.2 single_thread().() -> ThreadPoolrust-analyzer cargo threadfin 0.1.2 pool/ThreadPool# { |
| 7 | ThreadPoolrust-analyzer cargo threadfin 0.1.2 pool/ThreadPool#::builderrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]builder().().sizerust-analyzer cargo threadfin 0.1.2 pool/impl#[Builder]size().(0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#1).buildrust-analyzer cargo threadfin 0.1.2 pool/impl#[Builder]build().() |
| 8 | } |
| 9 | |
| 10 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 11 | #[should_panic(expected = "thread pool name must not contain null bytes")] |
| 12 | fn name_with_null_bytes_panicsrust-analyzer cargo threadfin 0.1.2 name_with_null_bytes_panics().() { |
| 13 | ThreadPoolrust-analyzer cargo threadfin 0.1.2 pool/ThreadPool#::builderrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]builder().().namerust-analyzer cargo threadfin 0.1.2 pool/impl#[Builder]name().("uh\0oh").buildrust-analyzer cargo threadfin 0.1.2 pool/impl#[Builder]build().(); |
| 14 | } |
| 15 | |
| 16 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/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_panicsrust-analyzer cargo threadfin 0.1.2 invalid_size_panics().() { |
| 20 | ThreadPoolrust-analyzer cargo threadfin 0.1.2 pool/ThreadPool#::builderrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]builder().().sizerust-analyzer cargo threadfin 0.1.2 pool/impl#[Builder]size().(2..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#1); |
| 21 | } |
| 22 | |
| 23 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 24 | #[should_panic(expected = "thread pool maximum size must be non-zero")] |
| 25 | fn invalid_size_zero_panicsrust-analyzer cargo threadfin 0.1.2 invalid_size_zero_panics().() { |
| 26 | ThreadPoolrust-analyzer cargo threadfin 0.1.2 pool/ThreadPool#::builderrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]builder().().sizerust-analyzer cargo threadfin 0.1.2 pool/impl#[Builder]size().(0); |
| 27 | } |
| 28 | |
| 29 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 30 | fn executerust-analyzer cargo threadfin 0.1.2 execute().() { |
| 31 | let pool = single_threadrust-analyzer cargo threadfin 0.1.2 single_thread().(); |
| 32 | |
| 33 | let result = pool.executerust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]execute().(|| 2 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Add<Self>`]add().2).joinrust-analyzer cargo threadfin 0.1.2 task/impl#[`Task<T>`]join().(); |
| 34 | |
| 35 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(result, 4); |
| 36 | } |
| 37 | |
| 38 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 39 | fn execute_futurerust-analyzer cargo threadfin 0.1.2 execute_future().() { |
| 40 | let pool = single_threadrust-analyzer cargo threadfin 0.1.2 single_thread().(); |
| 41 | |
| 42 | let result = pool.execute_futurerust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]execute_future().(async { 2 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Add<Self>`]add().2 }).joinrust-analyzer cargo threadfin 0.1.2 task/impl#[`Task<T>`]join().(); |
| 43 | |
| 44 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(result, 4); |
| 45 | } |
| 46 | |
| 47 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 48 | fn task_join_timeoutrust-analyzer cargo threadfin 0.1.2 task_join_timeout().() { |
| 49 | let pool = single_threadrust-analyzer cargo threadfin 0.1.2 single_thread().(); |
| 50 | |
| 51 | let result = pool |
| 52 | .executerust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]execute().(|| threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::sleeprust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/sleep().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_secsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_secs().(5))) |
| 53 | .join_timeoutrust-analyzer cargo threadfin 0.1.2 task/impl#[`Task<T>`]join_timeout().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(10)); |
| 54 | |
| 55 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(result.is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()); |
| 56 | } |
| 57 | |
| 58 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 59 | fn futures_that_yield_are_run_concurrentlyrust-analyzer cargo threadfin 0.1.2 futures_that_yield_are_run_concurrently().() { |
| 60 | let pool = single_threadrust-analyzer cargo threadfin 0.1.2 single_thread().(); |
| 61 | |
| 62 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(pool.running_tasksrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]running_tasks().(), 0); |
| 63 | |
| 64 | let first = pool |
| 65 | .try_execute_futurerust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]try_execute_future().(Delayrust-analyzer cargo futures-timer 3.0.4 native/delay/Delay#::newrust-analyzer cargo futures-timer 3.0.4 native/delay/impl#[Delay]new().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(400))) |
| 66 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]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 | threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::sleeprust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/sleep().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(100)); |
| 72 | |
| 73 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(pool.running_tasksrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]running_tasks().(), 1); |
| 74 | |
| 75 | let second = pool |
| 76 | .try_execute_futurerust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]try_execute_future().(Delayrust-analyzer cargo futures-timer 3.0.4 native/delay/Delay#::newrust-analyzer cargo futures-timer 3.0.4 native/delay/impl#[Delay]new().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(200))) |
| 77 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 78 | |
| 79 | threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::sleeprust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/sleep().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(100)); |
| 80 | |
| 81 | // Now both tasks are running, but there's still only 1 worker thread! |
| 82 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(pool.running_tasksrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]running_tasks().(), 2); |
| 83 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(pool.threadsrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]threads().(), 1); |
| 84 | |
| 85 | first.joinrust-analyzer cargo threadfin 0.1.2 task/impl#[`Task<T>`]join().(); |
| 86 | second.joinrust-analyzer cargo threadfin 0.1.2 task/impl#[`Task<T>`]join().(); |
| 87 | |
| 88 | // Both tasks completed. |
| 89 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(pool.completed_tasksrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]completed_tasks().(), 2); |
| 90 | } |
| 91 | |
| 92 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 93 | fn try_execute_under_core_countrust-analyzer cargo threadfin 0.1.2 try_execute_under_core_count().() { |
| 94 | let pool = ThreadPoolrust-analyzer cargo threadfin 0.1.2 pool/ThreadPool#::builderrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]builder().().sizerust-analyzer cargo threadfin 0.1.2 pool/impl#[Builder]size().(1).buildrust-analyzer cargo threadfin 0.1.2 pool/impl#[Builder]build().(); |
| 95 | |
| 96 | // Give some time for thread to start... |
| 97 | threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::sleeprust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/sleep().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(100)); |
| 98 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(pool.threadsrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]threads().(), 1); |
| 99 | |
| 100 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(pool.try_executerust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]try_execute().(|| 2 +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Add<Self>`]add(). 2).is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().()); |
| 101 | } |
| 102 | |
| 103 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 104 | fn try_execute_over_core_countrust-analyzer cargo threadfin 0.1.2 try_execute_over_core_count().() { |
| 105 | let pool = ThreadPoolrust-analyzer cargo threadfin 0.1.2 pool/ThreadPool#::builderrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]builder().().sizerust-analyzer cargo threadfin 0.1.2 pool/impl#[Builder]size().(0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#1).buildrust-analyzer cargo threadfin 0.1.2 pool/impl#[Builder]build().(); |
| 106 | |
| 107 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(pool.try_executerust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]try_execute().(|| 2 +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Add<Self>`]add(). 2).is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().()); |
| 108 | } |
| 109 | |
| 110 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 111 | fn try_execute_over_limitrust-analyzer cargo threadfin 0.1.2 try_execute_over_limit().() { |
| 112 | let pool = ThreadPoolrust-analyzer cargo threadfin 0.1.2 pool/ThreadPool#::builderrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]builder().().sizerust-analyzer cargo threadfin 0.1.2 pool/impl#[Builder]size().(0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#1).queue_limitrust-analyzer cargo threadfin 0.1.2 pool/impl#[Builder]queue_limit().(0).buildrust-analyzer cargo threadfin 0.1.2 pool/impl#[Builder]build().(); |
| 113 | |
| 114 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(pool.try_executerust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]try_execute().(|| 2 +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Add<Self>`]add(). 2).is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().()); |
| 115 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(pool.try_executerust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]try_execute().(|| 2 +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Add<Self>`]add(). 2).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()); |
| 116 | |
| 117 | fn taskrust-analyzer cargo threadfin 0.1.2 task().() -> usize { |
| 118 | 2 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().2 |
| 119 | } |
| 120 | |
| 121 | // The returned function in the error is identical to the function given. |
| 122 | let error = pool.try_executerust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]try_execute().(taskrust-analyzer cargo threadfin 0.1.2 task().).unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().(); |
| 123 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(error.into_innerrust-analyzer cargo threadfin 0.1.2 error/impl#[`PoolFullError<T>`]into_inner().() as usize, taskrust-analyzer cargo threadfin 0.1.2 task(). as usize); |
| 124 | } |
| 125 | |
| 126 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 127 | fn namerust-analyzer cargo threadfin 0.1.2 name().() { |
| 128 | let pool = ThreadPoolrust-analyzer cargo threadfin 0.1.2 pool/ThreadPool#::builderrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]builder().().namerust-analyzer cargo threadfin 0.1.2 pool/impl#[Builder]name().("foo").buildrust-analyzer cargo threadfin 0.1.2 pool/impl#[Builder]build().(); |
| 129 | |
| 130 | let name = pool |
| 131 | .executerust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]execute().(|| threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::currentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/current/current().().namerust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/thread/impl#[Thread]name().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()) |
| 132 | .joinrust-analyzer cargo threadfin 0.1.2 task/impl#[`Task<T>`]join().(); |
| 133 | |
| 134 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(name, "foo"); |
| 135 | } |
| 136 | |
| 137 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 138 | #[should_panic(expected = "oh no!")] |
| 139 | fn panic_propagates_to_taskrust-analyzer cargo threadfin 0.1.2 panic_propagates_to_task().() { |
| 140 | let pool = single_threadrust-analyzer cargo threadfin 0.1.2 single_thread().(); |
| 141 | |
| 142 | pool.executerust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]execute().(|| panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("oh no!")).joinrust-analyzer cargo threadfin 0.1.2 task/impl#[`Task<T>`]join().(); |
| 143 | } |
| 144 | |
| 145 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 146 | fn panic_countrust-analyzer cargo threadfin 0.1.2 panic_count().() { |
| 147 | let pool = single_threadrust-analyzer cargo threadfin 0.1.2 single_thread().(); |
| 148 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(pool.panicked_tasksrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]panicked_tasks().(), 0); |
| 149 | |
| 150 | let task = pool.executerust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]execute().(|| panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("oh no!")); |
| 151 | let _ = catch_unwindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/catch_unwind().(move || { |
| 152 | task.joinrust-analyzer cargo threadfin 0.1.2 task/impl#[`Task<T>`]join().(); |
| 153 | }); |
| 154 | |
| 155 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(pool.panicked_tasksrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]panicked_tasks().(), 1); |
| 156 | } |
| 157 | |
| 158 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 159 | fn thread_countrust-analyzer cargo threadfin 0.1.2 thread_count().() { |
| 160 | let pool = ThreadPoolrust-analyzer cargo threadfin 0.1.2 pool/ThreadPool#::builderrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]builder().().sizerust-analyzer cargo threadfin 0.1.2 pool/impl#[Builder]size().(0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#1).buildrust-analyzer cargo threadfin 0.1.2 pool/impl#[Builder]build().(); |
| 161 | |
| 162 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(pool.threadsrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]threads().(), 0); |
| 163 | |
| 164 | pool.executerust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]execute().(|| 2 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Add<Self>`]add().2).joinrust-analyzer cargo threadfin 0.1.2 task/impl#[`Task<T>`]join().(); |
| 165 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(pool.threadsrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]threads().(), 1); |
| 166 | |
| 167 | let pool_with_starting_threads = ThreadPoolrust-analyzer cargo threadfin 0.1.2 pool/ThreadPool#::builderrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]builder().().sizerust-analyzer cargo threadfin 0.1.2 pool/impl#[Builder]size().(1).buildrust-analyzer cargo threadfin 0.1.2 pool/impl#[Builder]build().(); |
| 168 | |
| 169 | // Give some time for thread to start... |
| 170 | threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::sleeprust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/sleep().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(50)); |
| 171 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(pool_with_starting_threads.threadsrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]threads().(), 1); |
| 172 | } |
| 173 | |
| 174 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 175 | fn idle_shutdownrust-analyzer cargo threadfin 0.1.2 idle_shutdown().() { |
| 176 | let pool = ThreadPoolrust-analyzer cargo threadfin 0.1.2 pool/ThreadPool#::builderrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]builder().() |
| 177 | .sizerust-analyzer cargo threadfin 0.1.2 pool/impl#[Builder]size().(0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#1) |
| 178 | .keep_aliverust-analyzer cargo threadfin 0.1.2 pool/impl#[Builder]keep_alive().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(100)) |
| 179 | .buildrust-analyzer cargo threadfin 0.1.2 pool/impl#[Builder]build().(); |
| 180 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(pool.threadsrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]threads().(), 0, "pool starts out empty"); |
| 181 | |
| 182 | pool.executerust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]execute().(|| 2 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Add<Self>`]add().2).joinrust-analyzer cargo threadfin 0.1.2 task/impl#[`Task<T>`]join().(); |
| 183 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(pool.threadsrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]threads().(), 1, "one thread was added"); |
| 184 | |
| 185 | threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::sleeprust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/sleep().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(200)); |
| 186 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 187 | pool.threadsrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]threads().(), |
| 188 | 0, |
| 189 | "thread became idle and terminated after timeout" |
| 190 | ); |
| 191 | } |
| 192 | |
| 193 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 194 | fn joinrust-analyzer cargo threadfin 0.1.2 join().() { |
| 195 | // Just a dumb test to make sure join doesn't do anything strange. |
| 196 | ThreadPoolrust-analyzer cargo threadfin 0.1.2 pool/ThreadPool#::defaultrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool][Default]default().().joinrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]join().(); |
| 197 | } |
| 198 | |
| 199 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 200 | fn join_timeout_expiringrust-analyzer cargo threadfin 0.1.2 join_timeout_expiring().() { |
| 201 | let pool = ThreadPoolrust-analyzer cargo threadfin 0.1.2 pool/ThreadPool#::builderrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]builder().().sizerust-analyzer cargo threadfin 0.1.2 pool/impl#[Builder]size().(1).buildrust-analyzer cargo threadfin 0.1.2 pool/impl#[Builder]build().(); |
| 202 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(pool.threadsrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]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.executerust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]execute().(|| threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::sleeprust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/sleep().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(500))); |
| 207 | |
| 208 | // Joining should time out since there's one task still running longer |
| 209 | // than our join timeout. |
| 210 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().pool.join_timeoutrust-analyzer cargo threadfin 0.1.2 pool/impl#[ThreadPool]join_timeout().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(10))); |
| 211 | } |
| 212 | |
| 213 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 214 | fn configure_common_after_init_returns_errorrust-analyzer cargo threadfin 0.1.2 configure_common_after_init_returns_error().() { |
| 215 | threadfinrust-analyzer cargo threadfin 0.1.2 crate/::commonrust-analyzer cargo threadfin 0.1.2 common/common().(); // init |
| 216 | |
| 217 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(threadfinrust-analyzer cargo threadfin 0.1.2 crate/::configure_commonrust-analyzer cargo threadfin 0.1.2 common/configure_common().(|b| b).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()); |
| 218 | } |