-
Notifications
You must be signed in to change notification settings - Fork 2
test: add more tests #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3173e80
28a78f6
7a822ee
72beafa
d6fe88c
84c322d
d6c9283
6f5e8ad
39d16b3
998b497
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,17 @@ | ||
| use std::sync::Arc; | ||
| use std::{ | ||
| panic, | ||
| sync::{ | ||
| Arc, | ||
| atomic::{AtomicUsize, Ordering}, | ||
| }, | ||
| thread, | ||
| }; | ||
|
|
||
| use ctor::ctor; | ||
| use scope_local::{ActiveScope, Scope, scope_local}; | ||
|
|
||
| #[ctor] | ||
| fn init() { | ||
| fn init_percpu() { | ||
| percpu::init(); | ||
|
|
||
| unsafe { percpu::write_percpu_reg(percpu::percpu_area_base(0)) }; | ||
|
|
@@ -14,18 +21,22 @@ fn init() { | |
| println!("per-CPU area size = {}", percpu::percpu_area_size()); | ||
| } | ||
|
|
||
| scope_local! { | ||
| static DATA: usize = 0; | ||
| } | ||
|
|
||
| #[test] | ||
| fn global() { | ||
| assert_eq!(*DATA, 0); | ||
| fn scope_init() { | ||
| scope_local! { | ||
| static DATA: usize = 42; | ||
| } | ||
| assert_eq!(*DATA, 42); | ||
| } | ||
|
|
||
| #[test] | ||
| fn scope() { | ||
| scope_local! { | ||
| static DATA: usize = 0; | ||
| } | ||
|
|
||
| let mut scope = Scope::new(); | ||
| assert_eq!(*DATA, 0); | ||
| assert_eq!(*DATA.scope(&scope), 0); | ||
|
|
||
| *DATA.scope_mut(&mut scope) = 42; | ||
|
|
@@ -35,14 +46,16 @@ fn scope() { | |
| assert_eq!(*DATA, 42); | ||
|
|
||
| ActiveScope::set_global(); | ||
| } | ||
|
|
||
| scope_local! { | ||
| static SHARED: Arc<String> = Arc::new("qwq".to_string()); | ||
| assert_eq!(*DATA, 0); | ||
| assert_eq!(*DATA.scope(&scope), 42); | ||
| } | ||
|
|
||
| #[test] | ||
| fn shared() { | ||
| fn scope_drop() { | ||
| scope_local! { | ||
| static SHARED: Arc<()> = Arc::new(()); | ||
| } | ||
|
|
||
| assert_eq!(Arc::strong_count(&SHARED), 1); | ||
|
|
||
| { | ||
|
|
@@ -55,3 +68,110 @@ fn shared() { | |
|
|
||
| assert_eq!(Arc::strong_count(&SHARED), 1); | ||
| } | ||
|
|
||
| #[test] | ||
| fn scope_panic_unwind_drop() { | ||
| scope_local! { | ||
| static SHARED: Arc<()> = Arc::new(()); | ||
| } | ||
|
|
||
| let panic = panic::catch_unwind(|| { | ||
| let mut scope = Scope::new(); | ||
| *SHARED.scope_mut(&mut scope) = SHARED.clone(); | ||
| assert_eq!(Arc::strong_count(&SHARED), 2); | ||
| panic!("panic"); | ||
| }); | ||
| assert!(panic.is_err()); | ||
|
|
||
| assert_eq!(Arc::strong_count(&SHARED), 1); | ||
| } | ||
|
|
||
| #[test] | ||
| fn thread_share_item() { | ||
| scope_local! { | ||
| static SHARED: Arc<()> = Arc::new(()); | ||
| } | ||
|
|
||
| let handles: Vec<_> = (0..10) | ||
| .map(|_| { | ||
| thread::spawn(move || { | ||
| let global = &*SHARED; | ||
|
|
||
| let mut scope = Scope::new(); | ||
| *SHARED.scope_mut(&mut scope) = global.clone(); | ||
|
|
||
| unsafe { ActiveScope::set(&scope) }; | ||
|
|
||
| assert!(Arc::strong_count(&SHARED) >= 2); | ||
| assert!(Arc::ptr_eq(&SHARED, global)); | ||
|
|
||
| ActiveScope::set_global(); | ||
| }) | ||
| }) | ||
| .collect(); | ||
|
|
||
| for h in handles { | ||
| h.join().unwrap(); | ||
| } | ||
|
|
||
| assert_eq!(Arc::strong_count(&SHARED), 1); | ||
| } | ||
|
|
||
| #[test] | ||
| fn thread_share_scope() { | ||
| scope_local! { | ||
| static SHARED: Arc<()> = Arc::new(()); | ||
| } | ||
|
|
||
| let scope = Arc::new(Scope::new()); | ||
|
|
||
| let handles: Vec<_> = (0..10) | ||
| .map(|_| { | ||
| let scope = scope.clone(); | ||
| thread::spawn(move || { | ||
| unsafe { ActiveScope::set(&scope) }; | ||
| assert_eq!(Arc::strong_count(&SHARED), 1); | ||
| assert!(Arc::ptr_eq(&SHARED, &SHARED.scope(&scope))); | ||
| ActiveScope::set_global(); | ||
| }) | ||
| }) | ||
| .collect(); | ||
|
|
||
| for h in handles { | ||
| h.join().unwrap(); | ||
| } | ||
|
|
||
| assert_eq!(Arc::strong_count(&SHARED), 1); | ||
| assert_eq!(Arc::strong_count(&SHARED.scope(&scope)), 1); | ||
| } | ||
|
|
||
| #[test] | ||
| fn thread_isolation() { | ||
| scope_local! { | ||
| static DATA: usize = 42; | ||
| static DATA2: AtomicUsize = AtomicUsize::new(42); | ||
| } | ||
|
|
||
| let handles: Vec<_> = (0..10) | ||
| .map(|i| { | ||
| thread::spawn(move || { | ||
| let mut scope = Scope::new(); | ||
| *DATA.scope_mut(&mut scope) = i; | ||
|
|
||
| unsafe { ActiveScope::set(&scope) }; | ||
| assert_eq!(*DATA, i); | ||
|
|
||
| DATA2.store(i, Ordering::Relaxed); | ||
|
|
||
| ActiveScope::set_global(); | ||
|
Comment on lines
+163
to
+166
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this line? I remember
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing this line will cause a SIGSEGV
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That's strange, and may indicate a bug in our design. |
||
| }) | ||
| }) | ||
| .collect(); | ||
|
|
||
| for h in handles { | ||
| h.join().unwrap(); | ||
| } | ||
|
|
||
| assert_eq!(*DATA, 42); | ||
| assert_eq!(DATA2.load(Ordering::Relaxed), 42); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.