-
-
Notifications
You must be signed in to change notification settings - Fork 12
feat: impl OnceMap #107
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
Merged
feat: impl OnceMap #107
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
78a760b
feat: impl OnceMap
tisonkun 68f6ef8
reorganize tests
tisonkun feb1505
add more APIs
tisonkun 2e7813a
add docs
tisonkun bf8c00b
tidy clippy
tisonkun 736300a
add basic tests
tisonkun c6c5a17
tidy
tisonkun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| // Copyright 2024 tison <wander4096@gmail.com> | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| use std::sync::Arc; | ||
| use std::sync::atomic::AtomicUsize; | ||
| use std::sync::atomic::Ordering; | ||
| use std::time::Duration; | ||
|
|
||
| use tokio_test::assert_ready; | ||
|
|
||
| use super::*; | ||
| use crate::latch::Latch; | ||
| use crate::test_runtime; | ||
|
|
||
| #[tokio::test] | ||
| async fn test_call_once_runs_only_once() { | ||
| static ONCE: Once = Once::new(); | ||
| static COUNTER: AtomicUsize = AtomicUsize::new(0); | ||
|
|
||
| assert!(!ONCE.is_completed()); | ||
|
|
||
| ONCE.call_once(async || { | ||
| COUNTER.fetch_add(1, Ordering::SeqCst); | ||
| }) | ||
| .await; | ||
|
|
||
| assert!(ONCE.is_completed()); | ||
| assert_eq!(COUNTER.load(Ordering::SeqCst), 1); | ||
|
|
||
| // Second call should not run the closure | ||
| ONCE.call_once(async || { | ||
| COUNTER.fetch_add(1, Ordering::SeqCst); | ||
| }) | ||
| .await; | ||
|
|
||
| assert_eq!(COUNTER.load(Ordering::SeqCst), 1); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_once_multi_task() { | ||
| static ONCE: Once = Once::new(); | ||
| static COUNTER: AtomicUsize = AtomicUsize::new(0); | ||
|
|
||
| test_runtime().block_on(async { | ||
| const N: usize = 100; | ||
|
|
||
| let latch = Arc::new(Latch::new(N as u32)); | ||
| let mut handles = Vec::with_capacity(N); | ||
|
|
||
| for _ in 0..N { | ||
| let latch = latch.clone(); | ||
| handles.push(tokio::spawn(async move { | ||
| ONCE.call_once(async || { | ||
| COUNTER.fetch_add(1, Ordering::SeqCst); | ||
| }) | ||
| .await; | ||
| latch.count_down(); | ||
| })); | ||
| } | ||
|
|
||
| latch.wait().await; | ||
|
|
||
| for handle in handles { | ||
| handle.await.unwrap(); | ||
| } | ||
|
|
||
| // Only one task should have incremented the counter | ||
| assert_eq!(COUNTER.load(Ordering::SeqCst), 1); | ||
| assert!(ONCE.is_completed()); | ||
| }); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_once_cancelled() { | ||
| static ONCE: Once = Once::new(); | ||
| static COUNTER: AtomicUsize = AtomicUsize::new(0); | ||
|
|
||
| let handle1 = tokio::spawn(async { | ||
| let fut = ONCE.call_once(async || { | ||
| tokio::time::sleep(Duration::from_millis(1000)).await; | ||
| COUNTER.fetch_add(1, Ordering::SeqCst); | ||
| }); | ||
| let timeout = tokio::time::timeout(Duration::from_millis(1), fut).await; | ||
| assert!(timeout.is_err()); | ||
| }); | ||
|
|
||
| let handle2 = tokio::spawn(async { | ||
| tokio::time::sleep(Duration::from_millis(100)).await; | ||
| ONCE.call_once(async || { | ||
| COUNTER.fetch_add(10, Ordering::SeqCst); | ||
| }) | ||
| .await; | ||
| }); | ||
|
|
||
| handle1.await.unwrap(); | ||
| handle2.await.unwrap(); | ||
|
|
||
| // The second task should have run since the first was cancelled | ||
| assert_eq!(COUNTER.load(Ordering::SeqCst), 10); | ||
| assert!(ONCE.is_completed()); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_once_debug() { | ||
| let once = Once::new(); | ||
| let debug_str = format!("{:?}", once); | ||
| assert!(debug_str.contains("Once")); | ||
| assert!(debug_str.contains("done")); | ||
| assert!(debug_str.contains("false")); | ||
|
|
||
| once.call_once(async || {}).await; | ||
|
|
||
| let debug_str = format!("{:?}", once); | ||
| assert!(debug_str.contains("true")); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_once_default() { | ||
| let once = Once::default(); | ||
| assert!(!once.is_completed()); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_once_retry_after_panic() { | ||
| static ONCE: Once = Once::new(); | ||
| static COUNTER: AtomicUsize = AtomicUsize::new(0); | ||
|
|
||
| let handle = tokio::spawn(async { | ||
| ONCE.call_once(async || { | ||
| COUNTER.fetch_add(1, Ordering::SeqCst); | ||
| panic!("boom"); | ||
| }) | ||
| .await; | ||
| }); | ||
|
|
||
| let err = handle.await.expect_err("once init should panic"); | ||
| assert!(err.is_panic()); | ||
|
|
||
| ONCE.call_once(async || { | ||
| COUNTER.fetch_add(1, Ordering::SeqCst); | ||
| }) | ||
| .await; | ||
|
|
||
| assert_eq!(COUNTER.load(Ordering::SeqCst), 2); | ||
| assert!(ONCE.is_completed()); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_once_wait() { | ||
| // wait after call_once completed | ||
| { | ||
| let once = Once::new(); | ||
| once.call_once(async || {}).await; | ||
| assert_ready!(tokio_test::task::spawn(once.wait()).poll()); | ||
| } | ||
|
|
||
| // wait before call_once completed | ||
| { | ||
| static ONCE: Once = Once::new(); | ||
| let handle = tokio::spawn(async { | ||
| ONCE.wait().await; | ||
| }); | ||
|
|
||
| tokio::time::sleep(Duration::from_millis(100)).await; | ||
| ONCE.call_once(async || {}).await; | ||
| handle.await.unwrap(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.