Repository: Thomasdezeeuw/a10
Pull request #154 (2025-01-25 15:21:27)

Test cleanup


Patch:
diff --git a/src/bitmap.rs b/src/bitmap.rs
index b4e4d17..98a6de8 100644
--- a/src/bitmap.rs
+++ b/src/bitmap.rs
@@ -77,111 +77,116 @@ impl fmt::Debug for AtomicBitMap {
     }
 }
 
-#[test]
-fn setting_and_unsetting_one() {
-    setting_and_unsetting(64)
-}
-
-#[test]
-fn setting_and_unsetting_two() {
-    setting_and_unsetting(128)
-}
-
-#[test]
-fn setting_and_unsetting_three() {
-    setting_and_unsetting(192)
-}
-
-#[test]
-fn setting_and_unsetting_four() {
-    setting_and_unsetting(256)
-}
-
-#[test]
-fn setting_and_unsetting_eight() {
-    setting_and_unsetting(512)
-}
+#[cfg(test)]
+mod tests {
+    use super::*;
 
-#[test]
-fn setting_and_unsetting_sixteen() {
-    setting_and_unsetting(1024)
-}
+    #[test]
+    fn setting_and_unsetting_one() {
+        setting_and_unsetting(64)
+    }
 
-#[cfg(test)]
-fn setting_and_unsetting(entries: usize) {
-    let map = AtomicBitMap::new(entries);
-    assert_eq!(map.capacity(), entries);
+    #[test]
+    fn setting_and_unsetting_two() {
+        setting_and_unsetting(128)
+    }
 
-    // Ask for all indices.
-    for n in 0..entries {
-        assert!(matches!(map.next_available(), Some(i) if i == n));
+    #[test]
+    fn setting_and_unsetting_three() {
+        setting_and_unsetting(192)
     }
-    // All bits should be set.
-    for data in &map.data {
-        assert!(data.load(Ordering::Relaxed) == usize::MAX);
+
+    #[test]
+    fn setting_and_unsetting_four() {
+        setting_and_unsetting(256)
     }
-    // No more indices left.
-    assert!(matches!(map.next_available(), None));
-
-    // Test unsetting an index not in order.
-    map.make_available(63);
-    map.make_available(0);
-    assert!(matches!(map.next_available(), Some(i) if i == 0));
-    assert!(matches!(map.next_available(), Some(i) if i == 63));
-
-    // Unset all indices again.
-    for n in (0..entries).into_iter().rev() {
-        map.make_available(n);
+
+    #[test]
+    fn setting_and_unsetting_eight() {
+        setting_and_unsetting(512)
     }
-    // Bitmap should be zeroed.
-    for data in &map.data {
-        assert!(data.load(Ordering::Relaxed) == 0);
+
+    #[test]
+    fn setting_and_unsetting_sixteen() {
+        setting_and_unsetting(1024)
     }
-    // Next avaiable index should be 0 again.
-    assert!(matches!(map.next_available(), Some(i) if i == 0));
-}
 
-#[test]
-fn setting_and_unsetting_concurrent() {
-    use std::sync::{Arc, Barrier};
-    use std::thread;
-
-    const N: usize = 4;
-    const M: usize = 1024;
-
-    let bitmap = Arc::new(AtomicBitMap::new(N * M));
-    let barrier = Arc::new(Barrier::new(N + 1));
-    let handles = (0..N)
-        .map(|i| {
-            let bitmap = bitmap.clone();
-            let barrier = barrier.clone();
-            thread::spawn(move || {
-                let mut indices = Vec::with_capacity(M);
-                barrier.wait();
-
-                if i % 2 == 0 {
-                    for _ in 0..M {
-                        let idx = bitmap.next_available().expect("failed to get index");
-                        indices.push(idx);
-                    }
+    #[cfg(test)]
+    fn setting_and_unsetting(entries: usize) {
+        let map = AtomicBitMap::new(entries);
+        assert_eq!(map.capacity(), entries);
 
-                    for idx in indices {
-                        bitmap.make_available(idx);
-                    }
-                } else {
-                    for _ in 0..M {
-                        let idx = bitmap.next_available().expect("failed to get index");
-                        bitmap.make_available(idx);
+        // Ask for all indices.
+        for n in 0..entries {
+            assert!(matches!(map.next_available(), Some(i) if i == n));
+        }
+        // All bits should be set.
+        for data in &map.data {
+            assert!(data.load(Ordering::Relaxed) == usize::MAX);
+        }
+        // No more indices left.
+        assert!(matches!(map.next_available(), None));
+
+        // Test unsetting an index not in order.
+        map.make_available(63);
+        map.make_available(0);
+        assert!(matches!(map.next_available(), Some(i) if i == 0));
+        assert!(matches!(map.next_available(), Some(i) if i == 63));
+
+        // Unset all indices again.
+        for n in (0..entries).into_iter().rev() {
+            map.make_available(n);
+        }
+        // Bitmap should be zeroed.
+        for data in &map.data {
+            assert!(data.load(Ordering::Relaxed) == 0);
+        }
+        // Next avaiable index should be 0 again.
+        assert!(matches!(map.next_available(), Some(i) if i == 0));
+    }
+
+    #[test]
+    fn setting_and_unsetting_concurrent() {
+        use std::sync::{Arc, Barrier};
+        use std::thread;
+
+        const N: usize = 4;
+        const M: usize = 1024;
+
+        let bitmap = Arc::new(AtomicBitMap::new(N * M));
+        let barrier = Arc::new(Barrier::new(N + 1));
+        let handles = (0..N)
+            .map(|i| {
+                let bitmap = bitmap.clone();
+                let barrier = barrier.clone();
+                thread::spawn(move || {
+                    let mut indices = Vec::with_capacity(M);
+                    barrier.wait();
+
+                    if i % 2 == 0 {
+                        for _ in 0..M {
+                            let idx = bitmap.next_available().expect("failed to get index");
+                            indices.push(idx);
+                        }
+
+                        for idx in indices {
+                            bitmap.make_available(idx);
+                        }
+                    } else {
+                        for _ in 0..M {
+                            let idx = bitmap.next_available().expect("failed to get index");
+                            bitmap.make_available(idx);
+                        }
                     }
-                }
+                })
             })
-        })
-        .collect::<Vec<_>>();
-
-    barrier.wait();
-    handles
-        .into_iter()
-        .map(|handle| handle.join())
-        .collect::<thread::Result<()>>()
-        .unwrap();
+            .collect::<Vec<_>>();
+
+        barrier.wait();
+        handles
+            .into_iter()
+            .map(|handle| handle.join())
+            .collect::<thread::Result<()>>()
+            .unwrap();
+    }
 }
diff --git a/src/io/read_buf.rs b/src/io/read_buf.rs
index c470e24..6226c6f 100644
--- a/src/io/read_buf.rs
+++ b/src/io/read_buf.rs
@@ -414,9 +414,3 @@ impl Drop for ReadBuf {
         self.release();
     }
 }
-
-#[test]
-fn size_assertion() {
-    assert_eq!(std::mem::size_of::<ReadBufPool>(), 8);
-    assert_eq!(std::mem::size_of::<ReadBuf>(), 24);
-}


Test patch:
diff --git a/tests/async_fd/io.rs b/tests/async_fd/io.rs
index 8bd7126..c608c7d 100644
--- a/tests/async_fd/io.rs
+++ b/tests/async_fd/io.rs
@@ -44,6 +44,20 @@ fn try_clone() {
     assert_eq!(&buf.data, DATA);
 }
 
+#[test]
+fn read_buf_pool_is_send_and_sync() {
+    is_send::<ReadBufPool>();
+    is_sync::<ReadBufPool>();
+    is_send::<ReadBuf>();
+    is_sync::<ReadBuf>();
+}
+
+#[test]
+fn read_buf_pool_size_assertion() {
+    assert_eq!(std::mem::size_of::<ReadBufPool>(), 8);
+    assert_eq!(std::mem::size_of::<ReadBuf>(), 24);
+}
+
 #[test]
 fn read_read_buf_pool() {
     require_kernel!(5, 19);
@@ -52,11 +66,6 @@ fn read_read_buf_pool() {
     let mut ring = Ring::new(2).expect("failed to create test ring");
     let sq = ring.submission_queue().clone();
 
-    is_send::<ReadBufPool>();
-    is_sync::<ReadBufPool>();
-    is_send::<ReadBuf>();
-    is_sync::<ReadBuf>();
-
     let test_file = &LOREM_IPSUM_50;
     let buf_pool = ReadBufPool::new(sq.clone(), 2, BUF_SIZE as u32).unwrap();