From 8120a996a360fd739a10f7f7bb6c0fb5a08e378f Mon Sep 17 00:00:00 2001 From: Dmitry Date: Tue, 28 Mar 2023 15:43:08 +0300 Subject: [PATCH] add test_share_reader --- lib/src/db.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/lib/src/db.rs b/lib/src/db.rs index 73b121b..cdb98c8 100644 --- a/lib/src/db.rs +++ b/lib/src/db.rs @@ -504,4 +504,40 @@ mod test { assert_eq!(pair.0, pair.1); }); } + + /// sharing Reader instance between threads + #[test] + fn test_share_reader() { + use std::thread; + + let dir = tempdir().expect("tempdir"); + let tmpfile = dir.path().join("test.tmp"); + let opts = WriterOpts { + compress_lvl: 1, + data_buf_size: 10 * 1024 * 1024, + out_buf_size: 10 * 1024 * 1024, + current_buf_size: 4096, + }; + let mut writer: Writer = Writer::new(&tmpfile, opts).expect("new writer"); + + let items_iter = gen_data(10); + let items: Vec = items_iter.collect(); + + writer.load(&mut items.clone().into_iter()).expect("load"); + writer.finish().expect("finish write"); + + let reader: Reader = Reader::new(&tmpfile, 2048).expect("new reader"); + assert_eq!(items.len(), reader.len()); + + let reader = Arc::new(reader); + for _ in 0..=3 { + let cur_items = items.clone(); + let cur_reader = Arc::clone(&reader); + thread::spawn(move || { + cur_items.into_iter().zip(cur_reader.iter()).for_each(|pair| { + assert_eq!(pair.0, pair.1); + }); + }); + } + } }