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