-
Notifications
You must be signed in to change notification settings - Fork 577
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add loom test for the counter (#6888)
Basically same as #6822. But we reuse the Counter to avoid code duplicate. Copy the content here. Use https://github.com/tokio-rs/loom for concurrency test. Maybe can make the correctness more confident. But I'm not so sure how to write the best test. it requires some code change for previous structure, so may need discuss: remove workspacke hack in task_stats_alloc crate. Otherwise there will be package conflict if you run RUSTFLAGS="--cfg loom" cargo test --test loom. Have not investigate deeply but simple remove just works. Refactor &'static AtomicUsize to be NonNull<AtomicUsize>. This is because the AtomicUsize in loom type do not support .as_mut_ptr (described in What to do when loom::AtomicUsize do not implement as_mut_ptr() tokio-rs/loom#298), so we use NonNull as intermediate workaround, that will add some unsafe code in .add() and .sub(). But seems OK. To test the drop, I have to add a flag var AtomicUsize in .sub() to ensure that the value is dropped. Please provide some suggestions if you have better ideas on how to write test. Approved-By: BugenZhao Approved-By: liurenjie1024 Co-Authored-By: BowenXiao1999 <[email protected]> Co-Authored-By: Bowen <[email protected]>
- Loading branch information
1 parent
d2dfad3
commit 2235e84
Showing
4 changed files
with
157 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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,50 @@ | ||
// Copyright 2022 Singularity Data | ||
// | ||
// 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. | ||
|
||
#![cfg(loom)] | ||
|
||
/// Note this test is not running in CI, due to the re-compile time cost. Add it when it is | ||
/// necessary. Run `RUSTFLAGS="--cfg loom" cargo test --test loom` to test. | ||
use loom::sync::Arc; | ||
use loom::thread; | ||
use task_stats_alloc::TaskLocalBytesAllocated; | ||
|
||
#[test] | ||
fn test_to_avoid_double_drop() { | ||
loom::model(|| { | ||
let bytes_num = 3; | ||
let num = Arc::new(TaskLocalBytesAllocated::new()); | ||
|
||
let threads: Vec<_> = (0..bytes_num) | ||
.map(|_| { | ||
let num = num.clone(); | ||
thread::spawn(move || { | ||
num.add(1); | ||
num.sub(1) | ||
}) | ||
}) | ||
.collect(); | ||
|
||
// How many times the bytes have been dropped. | ||
let mut drop_num = 0; | ||
for t in threads { | ||
if t.join().unwrap() { | ||
drop_num += 1; | ||
} | ||
} | ||
|
||
// Ensure the counter is dropped. | ||
assert_eq!(drop_num, 1); | ||
}); | ||
} |