Skip to content

Commit

Permalink
Add a miri test for OwnedCell
Browse files Browse the repository at this point in the history
  • Loading branch information
anacrolix committed Jul 11, 2024
1 parent 248cf0b commit 6780e52
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/owned_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,46 @@ impl<O, D> DerefMut for OwnedCell<O, D> {
&mut self.dep
}
}

#[cfg(test)]
mod tests {
use std::convert::Infallible;

use super::{test, Mutex, OwnedCell};

struct Conn {
count: usize,
}

impl Conn {
fn transaction(&mut self) -> Result<MutTransaction, Infallible> {
Ok(MutTransaction { conn: self })
}
fn new() -> Self {
Self { count: 0 }
}
}

struct MutTransaction<'a> {
conn: &'a mut Conn,
}

impl<'a> MutTransaction<'a> {
fn inc(&mut self) {
self.conn.count += 1
}
}

/// Test the case where we mutate the owner from the dependent, and then try to access the
/// owner. Intended for use with miri.
#[test]
fn test_miri_readonly_transaction() -> anyhow::Result<()> {
let conn = Mutex::new(Conn::new());
let mut cell = OwnedCell::try_make_mut(conn.lock().unwrap(), |conn| conn.transaction())?;
// We need to access before the mutate or miri doesn't notice.
assert_eq!(0, cell.owner().count);
cell.inc();
assert_eq!(1, cell.owner().count);
Ok(())
}
}
1 change: 1 addition & 0 deletions src/sys/flock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ mod tests {
use crate::test;

#[test]
#[cfg(not(miri))]
fn open_locked_file() -> anyhow::Result<()> {
let file1_named = NamedTempFile::new()?;
let file1_ref = file1_named.as_file();
Expand Down
1 change: 1 addition & 0 deletions src/sys/punchfile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod tests {

#[test]
#[allow(clippy::identity_op)]
#[cfg(not(miri))]
fn hole_punching() -> anyhow::Result<()> {
let mut temp_file = NamedTempFile::new()?;
let file = temp_file.as_file_mut();
Expand Down
1 change: 1 addition & 0 deletions src/sys/seekhole/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ mod tests {
use crate::testing::write_random_tempfile;

#[self::test]
#[cfg(not(miri))]
fn just_a_hole() -> anyhow::Result<()> {
let os_temp_dir = temp_dir();
let mut min_hole_size = path_min_hole_size(&os_temp_dir)?;
Expand Down
3 changes: 3 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ fn test_inc_array() {
/// the query that looked for the starting offset for hole punching would punch out the whole file
/// thinking it was empty.
#[test]
#[cfg(not(miri))]
fn test_replace_keys() -> Result<()> {
let tempdir = test_tempdir("test_replace_keys")?;
let handle = Handle::new(tempdir.path.clone())?;
Expand Down Expand Up @@ -109,6 +110,7 @@ fn test_replace_keys() -> Result<()> {

/// Prove that file cloning doesn't occur too late if the value is replaced.
#[test]
#[cfg(not(miri))]
fn punch_value_before_snapshot_cloned() -> anyhow::Result<()> {
let tempdir = test_tempdir("punch_value_before_snapshot_cloned")?;
let handle = Handle::new(tempdir.path.clone())?;
Expand Down Expand Up @@ -143,6 +145,7 @@ fn punch_value_before_snapshot_cloned() -> anyhow::Result<()> {
}

#[test]
#[cfg(not(miri))]
fn test_torrent_storage_benchmark() -> anyhow::Result<()> {
use testing::torrent_storage::*;
BENCHMARK_OPTS.build()?.run()
Expand Down

0 comments on commit 6780e52

Please sign in to comment.