Skip to content

Commit 6780e52

Browse files
committed
Add a miri test for OwnedCell
1 parent 248cf0b commit 6780e52

File tree

5 files changed

+49
-0
lines changed

5 files changed

+49
-0
lines changed

src/owned_cell.rs

+43
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,46 @@ impl<O, D> DerefMut for OwnedCell<O, D> {
9292
&mut self.dep
9393
}
9494
}
95+
96+
#[cfg(test)]
97+
mod tests {
98+
use std::convert::Infallible;
99+
100+
use super::{test, Mutex, OwnedCell};
101+
102+
struct Conn {
103+
count: usize,
104+
}
105+
106+
impl Conn {
107+
fn transaction(&mut self) -> Result<MutTransaction, Infallible> {
108+
Ok(MutTransaction { conn: self })
109+
}
110+
fn new() -> Self {
111+
Self { count: 0 }
112+
}
113+
}
114+
115+
struct MutTransaction<'a> {
116+
conn: &'a mut Conn,
117+
}
118+
119+
impl<'a> MutTransaction<'a> {
120+
fn inc(&mut self) {
121+
self.conn.count += 1
122+
}
123+
}
124+
125+
/// Test the case where we mutate the owner from the dependent, and then try to access the
126+
/// owner. Intended for use with miri.
127+
#[test]
128+
fn test_miri_readonly_transaction() -> anyhow::Result<()> {
129+
let conn = Mutex::new(Conn::new());
130+
let mut cell = OwnedCell::try_make_mut(conn.lock().unwrap(), |conn| conn.transaction())?;
131+
// We need to access before the mutate or miri doesn't notice.
132+
assert_eq!(0, cell.owner().count);
133+
cell.inc();
134+
assert_eq!(1, cell.owner().count);
135+
Ok(())
136+
}
137+
}

src/sys/flock/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ mod tests {
3737
use crate::test;
3838

3939
#[test]
40+
#[cfg(not(miri))]
4041
fn open_locked_file() -> anyhow::Result<()> {
4142
let file1_named = NamedTempFile::new()?;
4243
let file1_ref = file1_named.as_file();

src/sys/punchfile/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ mod tests {
2929

3030
#[test]
3131
#[allow(clippy::identity_op)]
32+
#[cfg(not(miri))]
3233
fn hole_punching() -> anyhow::Result<()> {
3334
let mut temp_file = NamedTempFile::new()?;
3435
let file = temp_file.as_file_mut();

src/sys/seekhole/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ mod tests {
134134
use crate::testing::write_random_tempfile;
135135

136136
#[self::test]
137+
#[cfg(not(miri))]
137138
fn just_a_hole() -> anyhow::Result<()> {
138139
let os_temp_dir = temp_dir();
139140
let mut min_hole_size = path_min_hole_size(&os_temp_dir)?;

src/tests.rs

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ fn test_inc_array() {
5555
/// the query that looked for the starting offset for hole punching would punch out the whole file
5656
/// thinking it was empty.
5757
#[test]
58+
#[cfg(not(miri))]
5859
fn test_replace_keys() -> Result<()> {
5960
let tempdir = test_tempdir("test_replace_keys")?;
6061
let handle = Handle::new(tempdir.path.clone())?;
@@ -109,6 +110,7 @@ fn test_replace_keys() -> Result<()> {
109110

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

145147
#[test]
148+
#[cfg(not(miri))]
146149
fn test_torrent_storage_benchmark() -> anyhow::Result<()> {
147150
use testing::torrent_storage::*;
148151
BENCHMARK_OPTS.build()?.run()

0 commit comments

Comments
 (0)