1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
#![warn(rust_2018_idioms)]
#![cfg(feature = "full")]
use std::mem;
use std::sync::atomic::{AtomicU32, Ordering};
use std::time::Duration;
use tokio::runtime;
use tokio::sync::OnceCell;
use tokio::sync::SetError;
use tokio::time;
#[test]
fn drop_cell() {
static NUM_DROPS: AtomicU32 = AtomicU32::new(0);
struct Foo {}
let fooer = Foo {};
impl Drop for Foo {
fn drop(&mut self) {
NUM_DROPS.fetch_add(1, Ordering::Release);
}
}
{
let once_cell = OnceCell::new();
let prev = once_cell.set(fooer);
assert!(prev.is_ok())
}
assert!(NUM_DROPS.load(Ordering::Acquire) == 1);
}
#[test]
fn drop_cell_new_with() {
static NUM_DROPS: AtomicU32 = AtomicU32::new(0);
struct Foo {}
let fooer = Foo {};
impl Drop for Foo {
fn drop(&mut self) {
NUM_DROPS.fetch_add(1, Ordering::Release);
}
}
{
let once_cell = OnceCell::new_with(Some(fooer));
assert!(once_cell.initialized());
}
assert!(NUM_DROPS.load(Ordering::Acquire) == 1);
}
#[test]
fn drop_into_inner() {
static NUM_DROPS: AtomicU32 = AtomicU32::new(0);
struct Foo {}
let fooer = Foo {};
impl Drop for Foo {
fn drop(&mut self) {
NUM_DROPS.fetch_add(1, Ordering::Release);
}
}
let once_cell = OnceCell::new();
assert!(once_cell.set(fooer).is_ok());
let fooer = once_cell.into_inner();
let count = NUM_DROPS.load(Ordering::Acquire);
assert!(count == 0);
drop(fooer);
let count = NUM_DROPS.load(Ordering::Acquire);
assert!(count == 1);
}
#[test]
fn drop_into_inner_new_with() {
static NUM_DROPS: AtomicU32 = AtomicU32::new(0);
struct Foo {}
let fooer = Foo {};
impl Drop for Foo {
fn drop(&mut self) {
NUM_DROPS.fetch_add(1, Ordering::Release);
}
}
let once_cell = OnceCell::new_with(Some(fooer));
let fooer = once_cell.into_inner();
let count = NUM_DROPS.load(Ordering::Acquire);
assert!(count == 0);
mem::drop(fooer);
let count = NUM_DROPS.load(Ordering::Acquire);
assert!(count == 1);
}
#[test]
fn from() {
let cell = OnceCell::from(2);
assert_eq!(*cell.get().unwrap(), 2);
}
async fn func1() -> u32 {
5
}
async fn func2() -> u32 {
time::sleep(Duration::from_millis(1)).await;
10
}
async fn func_err() -> Result<u32, ()> {
Err(())
}
async fn func_ok() -> Result<u32, ()> {
Ok(10)
}
async fn func_panic() -> u32 {
time::sleep(Duration::from_millis(1)).await;
panic!();
}
async fn sleep_and_set() -> u32 {
// Simulate sleep by pausing time and waiting for another thread to
// resume clock when calling `set`, then finding the cell being initialized
// by this call
time::sleep(Duration::from_millis(2)).await;
5
}
async fn advance_time_and_set(cell: &'static OnceCell<u32>, v: u32) -> Result<(), SetError<u32>> {
time::advance(Duration::from_millis(1)).await;
cell.set(v)
}
#[test]
fn get_or_init() {
let rt = runtime::Builder::new_current_thread()
.enable_time()
.start_paused(true)
.build()
.unwrap();
static ONCE: OnceCell<u32> = OnceCell::const_new();
rt.block_on(async {
let handle1 = rt.spawn(async { ONCE.get_or_init(func1).await });
let handle2 = rt.spawn(async { ONCE.get_or_init(func2).await });
time::advance(Duration::from_millis(1)).await;
time::resume();
let result1 = handle1.await.unwrap();
let result2 = handle2.await.unwrap();
assert_eq!(*result1, 5);
assert_eq!(*result2, 5);
});
}
#[test]
fn get_or_init_panic() {
let rt = runtime::Builder::new_current_thread()
.enable_time()
.build()
.unwrap();
static ONCE: OnceCell<u32> = OnceCell::const_new();
rt.block_on(async {
time::pause();
let handle1 = rt.spawn(async { ONCE.get_or_init(func1).await });
let handle2 = rt.spawn(async { ONCE.get_or_init(func_panic).await });
time::advance(Duration::from_millis(1)).await;
let result1 = handle1.await.unwrap();
let result2 = handle2.await.unwrap();
assert_eq!(*result1, 5);
assert_eq!(*result2, 5);
});
}
#[test]
fn set_and_get() {
let rt = runtime::Builder::new_current_thread()
.enable_time()
.build()
.unwrap();
static ONCE: OnceCell<u32> = OnceCell::const_new();
rt.block_on(async {
let _ = rt.spawn(async { ONCE.set(5) }).await;
let value = ONCE.get().unwrap();
assert_eq!(*value, 5);
});
}
#[test]
fn get_uninit() {
static ONCE: OnceCell<u32> = OnceCell::const_new();
let uninit = ONCE.get();
assert!(uninit.is_none());
}
#[test]
fn set_twice() {
static ONCE: OnceCell<u32> = OnceCell::const_new();
let first = ONCE.set(5);
assert_eq!(first, Ok(()));
let second = ONCE.set(6);
assert!(second.err().unwrap().is_already_init_err());
}
#[test]
fn set_while_initializing() {
let rt = runtime::Builder::new_current_thread()
.enable_time()
.build()
.unwrap();
static ONCE: OnceCell<u32> = OnceCell::const_new();
rt.block_on(async {
time::pause();
let handle1 = rt.spawn(async { ONCE.get_or_init(sleep_and_set).await });
let handle2 = rt.spawn(async { advance_time_and_set(&ONCE, 10).await });
time::advance(Duration::from_millis(2)).await;
let result1 = handle1.await.unwrap();
let result2 = handle2.await.unwrap();
assert_eq!(*result1, 5);
assert!(result2.err().unwrap().is_initializing_err());
});
}
#[test]
fn get_or_try_init() {
let rt = runtime::Builder::new_current_thread()
.enable_time()
.start_paused(true)
.build()
.unwrap();
static ONCE: OnceCell<u32> = OnceCell::const_new();
rt.block_on(async {
let handle1 = rt.spawn(async { ONCE.get_or_try_init(func_err).await });
let handle2 = rt.spawn(async { ONCE.get_or_try_init(func_ok).await });
time::advance(Duration::from_millis(1)).await;
time::resume();
let result1 = handle1.await.unwrap();
assert!(result1.is_err());
let result2 = handle2.await.unwrap();
assert_eq!(*result2.unwrap(), 10);
});
}
|