瀏覽代碼

test(mpsc): tests for draining after tx closes (#12)

Signed-off-by: Eliza Weisman <[email protected]>
Eliza Weisman 3 年之前
父節點
當前提交
d590f6d937
共有 3 個文件被更改,包括 64 次插入0 次删除
  1. 24 0
      src/mpsc/tests/mpsc_async.rs
  2. 19 0
      src/mpsc/tests/mpsc_sync.rs
  3. 21 0
      tests/mpsc_sync.rs

+ 24 - 0
src/mpsc/tests/mpsc_async.rs

@@ -256,3 +256,27 @@ fn tx_close_wakes() {
         consumer.join().unwrap();
     });
 }
+
+#[test]
+fn tx_close_drains_queue() {
+    const LEN: usize = 4;
+    loom::model(|| {
+        let (tx, rx) = channel(ThingBuf::new(LEN));
+        let producer = thread::spawn(move || {
+            future::block_on(async move {
+                for i in 0..LEN {
+                    tx.send(i).await.unwrap();
+                }
+            })
+        });
+
+        future::block_on(async move {
+            for i in 0..LEN {
+                assert_eq!(rx.recv().await, Some(i))
+            }
+        });
+
+
+        producer.join().unwrap();
+    });
+}

+ 19 - 0
src/mpsc/tests/mpsc_sync.rs

@@ -258,3 +258,22 @@ fn tx_close_wakes() {
         consumer.join().unwrap();
     });
 }
+
+#[test]
+fn tx_close_drains_queue() {
+    const LEN: usize = 4;
+    loom::model(|| {
+        let (tx, rx) = sync::channel(ThingBuf::new(LEN));
+        let producer = thread::spawn(move || {
+            for i in 0..LEN {
+                tx.send(i).unwrap();
+            }
+        });
+
+        for i in 0..LEN {
+            assert_eq!(rx.recv(), Some(i))
+        }
+
+        producer.join().unwrap();
+    });
+}

+ 21 - 0
tests/mpsc_sync.rs

@@ -49,3 +49,24 @@ fn basically_works() {
         }
     }
 }
+
+#[test]
+fn tx_close_drains_queue() {
+    const LEN: usize = 4;
+    for i in 0..10000 {
+        println!("\n\n--- iteration {} ---\n\n", i);
+
+        let (tx, rx) = sync::channel(ThingBuf::new(LEN));
+        let producer = thread::spawn(move || {
+            for i in 0..LEN {
+                tx.send(i).unwrap();
+            }
+        });
+
+        for i in 0..LEN {
+            assert_eq!(rx.recv(), Some(i))
+        }
+
+        producer.join().unwrap();
+    }
+}