-
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
base: main
Are you sure you want to change the base?
Changes from all commits
e3c2658
70c7870
5c00501
5a2ba55
bad41d8
c9a8842
370dd0e
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,4 +1,4 @@ | ||
| use std::sync::Arc; | ||
| use std::{panic, sync::Arc, thread}; | ||
|
|
||
| use ctor::ctor; | ||
| use scope_local::{ActiveScope, Scope, scope_local}; | ||
|
|
@@ -54,4 +54,103 @@ fn shared() { | |
| } | ||
|
|
||
| assert_eq!(Arc::strong_count(&SHARED), 1); | ||
|
|
||
| let panic = panic::catch_unwind(|| { | ||
| let mut scope = Scope::new(); | ||
| *SHARED.scope_mut(&mut scope) = SHARED.clone(); | ||
| panic!("panic"); | ||
| }); | ||
| assert!(panic.is_err()); | ||
|
|
||
| assert_eq!(Arc::strong_count(&SHARED), 1); | ||
| } | ||
|
|
||
| scope_local! { | ||
| static T_SHARED: Arc<String> = Arc::new("qwq".to_string()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn threads_shared() { | ||
| assert_eq!(Arc::strong_count(&SHARED), 1); | ||
|
|
||
| let handles: Vec<_> = (0..10) | ||
| .map(|_| { | ||
| thread::spawn(move || { | ||
| let mut scope = Scope::new(); | ||
| *T_SHARED.scope_mut(&mut scope) = T_SHARED.clone(); | ||
| assert!(Arc::strong_count(&T_SHARED) >= 2); | ||
| assert_eq!(*T_SHARED, Arc::new("qwq".to_string())); | ||
| }) | ||
| }) | ||
| .collect(); | ||
|
|
||
| for h in handles { | ||
| h.join().unwrap(); | ||
| } | ||
|
|
||
| assert_eq!(Arc::strong_count(&T_SHARED), 1); | ||
|
|
||
| { | ||
| let mut scope = Scope::new(); | ||
| *T_SHARED.scope_mut(&mut scope) = T_SHARED.clone(); | ||
| let scope = Arc::new(scope); | ||
|
|
||
| let handles: Vec<_> = (0..10) | ||
| .map(|_| { | ||
| let scope = scope.clone(); | ||
| thread::spawn(move || { | ||
| unsafe { ActiveScope::set(&scope) }; | ||
| assert_eq!(Arc::strong_count(&T_SHARED), 2); | ||
| assert_eq!(*T_SHARED, Arc::new("qwq".to_string())); | ||
| ActiveScope::set_global(); | ||
| }) | ||
| }) | ||
| .collect(); | ||
|
|
||
| for h in handles { | ||
| h.join().unwrap(); | ||
| } | ||
| } | ||
|
|
||
| assert_eq!(Arc::strong_count(&T_SHARED), 1); | ||
| } | ||
|
|
||
| #[test] | ||
| fn isolation() { | ||
|
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. Consider adding a test that multiple threads share one scope? |
||
| 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); | ||
|
|
||
| ActiveScope::set_global(); | ||
|
Comment on lines
+128
to
+129
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
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, 0); | ||
| } | ||
|
|
||
| #[test] | ||
| fn nested() { | ||
| let mut outer = Scope::new(); | ||
| unsafe { ActiveScope::set(&outer) }; | ||
| *DATA.scope_mut(&mut outer) = 1; | ||
|
|
||
| let mut inner = Scope::new(); | ||
| unsafe { ActiveScope::set(&inner) }; | ||
| *DATA.scope_mut(&mut inner) = 2; | ||
| assert_eq!(*DATA, 2); | ||
|
|
||
| unsafe { ActiveScope::set(&outer) }; | ||
| assert_eq!(*DATA, 1); | ||
|
|
||
| ActiveScope::set_global(); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it also fix issues like arceos-org/percpu#5 and arceos-org/percpu#12? If so, you may also contribute it to
percpu.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change would make the load address no longer 0x0, which causes the assertions in the tests to fail. The corresponding tests would also need to be adjusted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. You may adjust the test codes accordingly.