1
mod helpers;
2

3
use helpers::{create_and_drop, CountDown};
4
use persy::PersyError;
5
use std::sync::Arc;
6
use std::thread;
7

8
#[test]
9 1
fn test_create_drop_segment() {
10 1
    create_and_drop("cds", |persy| {
11 1
        let mut tx = persy.begin().unwrap();
12 1
        tx.create_segment("test").unwrap();
13 1
        let finalizer = tx.prepare().unwrap();
14 1
        finalizer.commit().unwrap();
15

16 1
        assert!(persy.exists_segment("test").unwrap());
17 1
        let mut tx = persy.begin().unwrap();
18 1
        tx.drop_segment("test").unwrap();
19 1
        let finalizer = tx.prepare().unwrap();
20 1
        finalizer.commit().unwrap();
21 1
        assert!(!persy.exists_segment("test").unwrap());
22 1
    });
23 1
}
24

25
#[test]
26 1
fn test_create_drop_double_segments_and_insert_data() {
27 1
    create_and_drop("cdss", |persy| {
28 1
        let mut tx = persy.begin().unwrap();
29 1
        tx.create_segment("test").unwrap();
30 1
        tx.create_segment("test1").unwrap();
31 1
        let bytes = String::from("some").into_bytes();
32 1
        tx.insert("test", &bytes).unwrap();
33

34 1
        let bytes = String::from("some").into_bytes();
35 1
        tx.insert("test1", &bytes).unwrap();
36
        {
37 1
            let scanner = tx.scan("test").unwrap();
38 1
            assert_eq!(1, scanner.into_iter().count());
39
        }
40

41 1
        let scanner = tx.scan("test1").unwrap();
42 1
        assert_eq!(1, scanner.into_iter().count());
43 1
    });
44 1
}
45

46
#[test]
47 1
fn test_create_drop_segment_same_tx() {
48 1
    create_and_drop("cdss", |persy| {
49 1
        let mut tx = persy.begin().unwrap();
50 1
        tx.create_segment("test").unwrap();
51 1
        assert!(tx.exists_segment("test").unwrap());
52 1
        let err = tx.drop_segment("test");
53 1
        assert!(err.is_err());
54 1
    });
55 1
}
56

57
#[test]
58 1
fn test_create_drop_recreate_segment_same_tx() {
59 1
    create_and_drop("cdcs", |persy| {
60 1
        let mut tx = persy.begin().unwrap();
61 1
        tx.create_segment("test").unwrap();
62 1
        assert!(tx.exists_segment("test").unwrap());
63 1
        let finalizer = tx.prepare().unwrap();
64 1
        finalizer.commit().unwrap();
65

66 1
        let mut tx = persy.begin().unwrap();
67 1
        tx.drop_segment("test").unwrap();
68 1
        assert!(!tx.exists_segment("test").unwrap());
69 1
        tx.create_segment("test").unwrap();
70 1
        assert!(tx.exists_segment("test").unwrap());
71 1
        let finalizer = tx.prepare().unwrap();
72 1
        finalizer.commit().unwrap();
73 1
        assert!(persy.exists_segment("test").unwrap());
74 1
    });
75 1
}
76

77
#[test]
78 1
fn test_update_record_of_dropped_segment_other_tx() {
79 1
    create_and_drop("urds", |persy| {
80 1
        let mut tx = persy.begin().unwrap();
81 1
        tx.create_segment("test").unwrap();
82 1
        let bytes = String::from("some").into_bytes();
83 1
        let rec = tx.insert("test", &bytes).unwrap();
84 1
        let finalizer = tx.prepare().unwrap();
85 1
        finalizer.commit().unwrap();
86

87 1
        let mut tx = persy.begin().unwrap();
88 1
        tx.update("test", &rec, &bytes).unwrap();
89

90 1
        let mut tx1 = persy.begin().unwrap();
91 1
        tx1.drop_segment("test").unwrap();
92 1
        let finalizer = tx1.prepare().unwrap();
93 1
        finalizer.commit().unwrap();
94

95 1
        let finalizer = tx.prepare();
96 1
        assert!(finalizer.is_err());
97 1
    });
98 1
}
99

100
#[test]
101 1
fn test_update_record_of_dropped_recreated_segment_other_tx() {
102 1
    create_and_drop("urdcs", |persy| {
103 1
        let mut tx = persy.begin().unwrap();
104 1
        tx.create_segment("test").unwrap();
105 1
        let bytes = String::from("some").into_bytes();
106 1
        let rec = tx.insert("test", &bytes).unwrap();
107 1
        let finalizer = tx.prepare().unwrap();
108 1
        finalizer.commit().unwrap();
109

110 1
        let mut tx = persy.begin().unwrap();
111 1
        tx.update("test", &rec, &bytes).unwrap();
112

113 1
        let mut tx1 = persy.begin().unwrap();
114 1
        tx1.drop_segment("test").unwrap();
115 1
        tx1.create_segment("test").unwrap();
116 1
        let finalizer = tx1.prepare().unwrap();
117 1
        finalizer.commit().unwrap();
118

119 1
        let finalizer = tx.prepare();
120 1
        assert!(finalizer.is_err());
121 1
    });
122 1
}
123

124
#[test]
125 1
fn test_delete_record_of_dropped_segment_other_tx() {
126 1
    create_and_drop("drds", |persy| {
127 1
        let mut tx = persy.begin().unwrap();
128 1
        tx.create_segment("test").unwrap();
129 1
        let bytes = String::from("some").into_bytes();
130 1
        let rec = tx.insert("test", &bytes).unwrap();
131 1
        let finalizer = tx.prepare().unwrap();
132 1
        finalizer.commit().unwrap();
133

134 1
        let mut tx = persy.begin().unwrap();
135 1
        tx.delete("test", &rec).unwrap();
136

137 1
        let mut tx1 = persy.begin().unwrap();
138 1
        tx1.drop_segment("test").unwrap();
139 1
        let finalizer = tx1.prepare().unwrap();
140 1
        finalizer.commit().unwrap();
141

142 1
        let finalizer = tx.prepare();
143 1
        assert!(finalizer.is_err());
144 1
    });
145 1
}
146

147
#[test]
148 1
fn test_delete_record_of_dropped_created_segment_other_tx() {
149 1
    create_and_drop("drdcs", |persy| {
150 1
        let mut tx = persy.begin().unwrap();
151 1
        tx.create_segment("test").unwrap();
152 1
        let bytes = String::from("some").into_bytes();
153 1
        let rec = tx.insert("test", &bytes).unwrap();
154 1
        let finalizer = tx.prepare().unwrap();
155 1
        finalizer.commit().unwrap();
156

157 1
        let mut tx = persy.begin().unwrap();
158 1
        tx.delete("test", &rec).unwrap();
159

160 1
        let mut tx1 = persy.begin().unwrap();
161 1
        tx1.drop_segment("test").unwrap();
162 1
        tx1.create_segment("test").unwrap();
163 1
        let finalizer = tx1.prepare().unwrap();
164 1
        finalizer.commit().unwrap();
165

166 1
        let finalizer = tx.prepare();
167 1
        assert!(finalizer.is_err());
168 1
    });
169 1
}
170

171
#[test]
172 1
fn test_insert_record_of_dropped_recreated_segment_other_tx() {
173 1
    create_and_drop("irdcs", |persy| {
174 1
        let mut tx = persy.begin().unwrap();
175 1
        tx.create_segment("test").unwrap();
176 1
        let bytes = String::from("some").into_bytes();
177 1
        let finalizer = tx.prepare().unwrap();
178 1
        finalizer.commit().unwrap();
179

180 1
        let mut tx = persy.begin().unwrap();
181 1
        tx.insert("test", &bytes).unwrap();
182

183 1
        let mut tx1 = persy.begin().unwrap();
184 1
        tx1.drop_segment("test").unwrap();
185 1
        tx1.create_segment("test").unwrap();
186 1
        let finalizer = tx1.prepare().unwrap();
187 1
        finalizer.commit().unwrap();
188

189 1
        let finalizer = tx.prepare();
190 1
        assert!(finalizer.is_err());
191 1
    });
192 1
}
193

194
#[test]
195 1
fn test_insert_record_of_dropped_segment_other_tx() {
196 1
    create_and_drop("irds", |persy| {
197 1
        let mut tx = persy.begin().unwrap();
198 1
        tx.create_segment("test").unwrap();
199 1
        let bytes = String::from("some").into_bytes();
200 1
        let finalizer = tx.prepare().unwrap();
201 1
        finalizer.commit().unwrap();
202

203 1
        let mut tx = persy.begin().unwrap();
204 1
        tx.insert("test", &bytes).unwrap();
205

206 1
        let mut tx1 = persy.begin().unwrap();
207 1
        tx1.drop_segment("test").unwrap();
208 1
        let finalizer = tx1.prepare().unwrap();
209 1
        finalizer.commit().unwrap();
210

211 1
        let finalizer = tx.prepare();
212 1
        assert!(finalizer.is_err());
213 1
    });
214 1
}
215

216
#[test]
217 1
fn test_record_of_drop_segment_same_tx() {
218 1
    create_and_drop("rds", |persy| {
219 1
        let mut tx = persy.begin().unwrap();
220 1
        tx.create_segment("test").unwrap();
221 1
        let finalizer = tx.prepare().unwrap();
222 1
        finalizer.commit().unwrap();
223

224 1
        let mut tx = persy.begin().unwrap();
225 1
        let bytes = String::from("some").into_bytes();
226 1
        let id = tx.insert("test", &bytes).expect("insert record works");
227 1
        tx.drop_segment("test").unwrap();
228 1
        let finalizer = tx.prepare().unwrap();
229 1
        finalizer.commit().unwrap();
230 1
        let mut tx = persy.begin().unwrap();
231 1
        tx.create_segment("test").unwrap();
232 1
        assert!(match tx.update("test", &id, &String::from("none").into_bytes()) {
233 1
            Err(PersyError::RecordNotFound(ref id2)) if *id2 == id => true,
234 0
            _ => false,
235
        });
236 1
        assert_eq!(tx.read("test", &id).ok(), Some(None));
237

238 1
        let finalizer = tx.prepare().unwrap();
239 1
        finalizer.commit().unwrap();
240 1
    });
241 1
}
242

243
#[test]
244 1
pub fn test_councurrent_double_create() {
245 1
    create_and_drop("concurrent_segment_double_create.", |persy| {
246 1
        let both_create = Arc::new(CountDown::new(2));
247 1
        let end = Arc::new(CountDown::new(2));
248 1
        for _ in [0; 2].iter() {
249 1
            let both_create_moved = both_create.clone();
250 1
            let end_moved = end.clone();
251 1
            let persy = persy.clone();
252 1
            thread::spawn(move || {
253 1
                let mut tx = persy.begin().expect("error on transaction begin");
254 1
                tx.create_segment("def").expect("error on segment creation");
255 1
                both_create_moved.count_down().expect("lock not panic");
256 1
                both_create_moved.wait().expect("thread wait the other");
257 1
                let fin = tx.prepare().expect("error on commit prepare");
258 1
                fin.commit().expect("error on commit");
259 1
                end_moved.count_down().expect("lock not panic");
260 1
            });
261 1
        }
262

263 1
        end.wait().expect("threas finisced");
264 1
        assert!(persy.exists_segment("def").unwrap());
265 1
    });
266 1
}
267

268
#[test]
269 1
pub fn test_councurrent_double_drop() {
270 1
    create_and_drop("concurrent_segment_double_drop.", |persy| {
271 1
        let mut tx = persy.begin().expect("error on transaction begin");
272 1
        tx.create_segment("def").expect("error on segment creation");
273 1
        let fin = tx.prepare().expect("error on commit prepare");
274 1
        fin.commit().expect("error on commit");
275 1
        let both_create = Arc::new(CountDown::new(2));
276 1
        let end = Arc::new(CountDown::new(2));
277 1
        for _ in [0; 2].iter() {
278 1
            let both_create_moved = both_create.clone();
279 1
            let end_moved = end.clone();
280 1
            let persy = persy.clone();
281 1
            thread::spawn(move || {
282 1
                let mut tx = persy.begin().expect("error on transaction begin");
283 1
                tx.drop_segment("def").expect("error on segment creation");
284 1
                both_create_moved.count_down().expect("lock not panic");
285 1
                both_create_moved.wait().expect("thread wait the other");
286 1
                let fin = tx.prepare().expect("error on commit prepare");
287 1
                fin.commit().expect("error on commit");
288 1
                end_moved.count_down().expect("lock not panic");
289 1
            });
290 1
        }
291

292 1
        end.wait().expect("threas finisced");
293 1
        assert!(!persy.exists_segment("def").unwrap());
294 1
    });
295 1
}
296

297
#[test]
298 1
pub fn test_list_segments() {
299 1
    create_and_drop("test_list_segments", |persy| {
300 1
        let mut tx = persy.begin().expect("error on transaction begin");
301 1
        tx.create_segment("def").expect("error on segment creation");
302 1
        tx.create_segment("two").expect("error on segment creation");
303 1
        let fin = tx.prepare().expect("error on commit prepare");
304 1
        fin.commit().expect("error on commit");
305 1
        let segments = persy.list_segments().expect("list segments works as expected");
306 1
        assert_eq!(segments.len(), 2);
307 1
        let names = segments.into_iter().map(|(name, _id)| name).collect::<Vec<String>>();
308 1
        assert!(names.contains(&"def".to_string()));
309 1
        assert!(names.contains(&"two".to_string()));
310 1
    });
311 1
}
312

313
#[test]
314 1
pub fn test_list_segments_tx() {
315 1
    create_and_drop("test_list_segments", |persy| {
316 1
        let mut tx = persy.begin().expect("error on transaction begin");
317 1
        tx.create_segment("def").expect("error on segment creation");
318 1
        tx.create_segment("two").expect("error on segment creation");
319 1
        let fin = tx.prepare().expect("error on commit prepare");
320 1
        fin.commit().expect("error on commit");
321

322 1
        let mut tx = persy.begin().expect("error on transaction begin");
323 1
        tx.drop_segment("two").expect("error on segment drop");
324 1
        tx.create_segment("three").expect("error on segment creation");
325 1
        let segments = tx.list_segments().expect("list segments works as expected");
326 1
        assert_eq!(segments.len(), 2);
327 1
        let names = segments.into_iter().map(|x| x.0).collect::<Vec<String>>();
328 1
        assert!(names.contains(&"def".to_string()));
329 1
        assert!(names.contains(&"three".to_string()));
330 1
    });
331 1
}

Read our documentation on viewing source code .

Loading