Skip to content
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

Change loom tests to use cfg(loom) internally #314

Merged
merged 1 commit into from Nov 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions azure-pipelines.yml
Expand Up @@ -51,6 +51,13 @@ jobs:
name: tsan
rust_version: nightly

# Loom
- template: ci/azure-loom.yml
parameters:
name: loom
rust_version: stable


- template: ci/azure-deploy-docs.yml
parameters:
dependsOn:
Expand Down
15 changes: 15 additions & 0 deletions ci/azure-loom.yml
@@ -0,0 +1,15 @@
jobs:
- job: ${{parameters.name}}
displayName: Loom tests
pool:
vmImage: ubuntu-16.04

steps:
- template: azure-install-rust.yml
parameters:
rust_version: ${{parameters.rust_version}}

- script: RUSTFLAGS="--cfg loom" cargo test --lib
displayName: RUSTFLAGS="--cfg loom" cargo test --lib


33 changes: 33 additions & 0 deletions src/bytes.rs
Expand Up @@ -903,3 +903,36 @@ unsafe fn release_shared(ptr: *mut Shared) {
// Drop the data
Box::from_raw(ptr);
}

// fuzz tests
#[cfg(all(test, loom))]
mod fuzz {
use std::sync::Arc;
use loom::thread;

use super::Bytes;
#[test]
fn bytes_cloning_vec() {
loom::model(|| {
let a = Bytes::from(b"abcdefgh".to_vec());
let addr = a.as_ptr() as usize;

// test the Bytes::clone is Sync by putting it in an Arc
let a1 = Arc::new(a);
let a2 = a1.clone();

let t1 = thread::spawn(move || {
let b: Bytes = (*a1).clone();
assert_eq!(b.as_ptr() as usize, addr);
});

let t2 = thread::spawn(move || {
let b: Bytes = (*a2).clone();
assert_eq!(b.as_ptr() as usize, addr);
});

t1.join().unwrap();
t2.join().unwrap();
});
}
}
35 changes: 35 additions & 0 deletions src/bytes_mut.rs
Expand Up @@ -1427,3 +1427,38 @@ unsafe fn shared_v_drop(data: &mut AtomicPtr<()>, _ptr: *const u8, _len: usize)
let shared = (*data.get_mut()) as *mut Shared;
release_shared(shared as *mut Shared);
}

// fuzz tests
#[cfg(all(test, loom))]
mod fuzz {
use std::sync::Arc;
use loom::thread;

use crate::Bytes;
use super::BytesMut;

#[test]
fn bytes_mut_cloning_frozen() {
loom::model(|| {
let a = BytesMut::from(&b"abcdefgh"[..]).split().freeze();
let addr = a.as_ptr() as usize;

// test the Bytes::clone is Sync by putting it in an Arc
let a1 = Arc::new(a);
let a2 = a1.clone();

let t1 = thread::spawn(move || {
let b: Bytes = (*a1).clone();
assert_eq!(b.as_ptr() as usize, addr);
});

let t2 = thread::spawn(move || {
let b: Bytes = (*a2).clone();
assert_eq!(b.as_ptr() as usize, addr);
});

t1.join().unwrap();
t2.join().unwrap();
});
}
}
4 changes: 4 additions & 0 deletions src/loom.rs
@@ -1,5 +1,9 @@
#[cfg(not(all(test, loom)))]
pub(crate) mod sync {
pub(crate) mod atomic {
pub(crate) use core::sync::atomic::{fence, AtomicPtr, AtomicUsize, Ordering};
}
}

#[cfg(all(test, loom))]
pub(crate) use ::loom::sync;
73 changes: 0 additions & 73 deletions tests/fuzz_bytes.rs

This file was deleted.