Debian Codegraph @

third_party/rust/ffi-support/tests/test.rs

raw
1 /* Copyright 2018-2019 Mozilla Foundation
2 *
3 * Licensed under the Apache License (Version 2.0), or the MIT license,
4 * (the "Licenses") at your option. You may not use this file except in
5 * compliance with one of the Licenses. You may obtain copies of the
6 * Licenses at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 * http://opensource.org/licenses/MIT
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the Licenses is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the Licenses for the specific language governing permissions and
15 * limitations under the Licenses.
16 */
17
18 //! This test is a stress test meant to trigger some bugs seen prior to the use
19 //! of handlemaps. See /docs/design/test-faster.md for why it's split -- TLDR:
20 //! it uses rayon and is hard to rewrite with normal threads.
21
22 use ffi_support::{ConcurrentHandleMap, ExternError};
23 use std::sync::atomic::{AtomicUsize, Ordering};
24 use std::sync::Arc;
25
26 fn with_error<F: FnOnce(&mut ExternError) -> T, T>(callback: F) -> T {
27 let mut e = ExternError::success();
28 let result = callback(&mut e);
29 if let Some(m) = unsafe { e.get_and_consume_message() } {
30 panic!("unexpected error: {}", m);
31 }
32 result
33 }
34
35 struct DropChecking {
36 counter: Arc<AtomicUsize>,
37 id: usize,
38 }
39 impl Drop for DropChecking {
40 fn drop(&mut self) {
41 let val = self.counter.fetch_add(1, Ordering::SeqCst);
42 log::debug!("Dropped {} :: {}", self.id, val);
43 }
44 }
45 #[test]
46 fn test_concurrent_drop() {
47 use rand::prelude::*;
48 use rayon::prelude::*;
49 let _ = env_logger::try_init();
50 let drop_counter = Arc::new(AtomicUsize::new(0));
51 let id = Arc::new(AtomicUsize::new(1));
52 let map = ConcurrentHandleMap::new();
53 let count = 1000;
54 let mut handles = (0..count)
55 .into_par_iter()
56 .map(|_| {
57 let id = id.fetch_add(1, Ordering::SeqCst);
58 let handle = with_error(|e| {
59 map.insert_with_output(e, || {
60 log::debug!("Created {}", id);
61 DropChecking {
62 counter: drop_counter.clone(),
63 id,
64 }
65 })
66 });
67 (id, handle)
68 })
69 .collect::<Vec<_>>();
70
71 handles.shuffle(&mut thread_rng());
72
73 assert_eq!(drop_counter.load(Ordering::SeqCst), 0);
74 handles.par_iter().for_each(|(id, h)| {
75 with_error(|e| {
76 map.call_with_output(e, *h, |val| {
77 assert_eq!(val.id, *id);
78 })
79 });
80 });
81
82 assert_eq!(drop_counter.load(Ordering::SeqCst), 0);
83
84 handles.par_iter().for_each(|(id, h)| {
85 with_error(|e| {
86 map.call_with_output(e, *h, |val| {
87 assert_eq!(val.id, *id);
88 })
89 });
90 });
91
92 handles.par_iter().for_each(|(id, h)| {
93 let item = map
94 .remove_u64(*h)
95 .expect("remove to succeed")
96 .expect("item to exist");
97 assert_eq!(item.id, *id);
98 let h = map.insert(item).into_u64();
99 map.delete_u64(h).expect("delete to succeed");
100 });
101 assert_eq!(drop_counter.load(Ordering::SeqCst), count);
102 }