1
mod helpers;
2
use helpers::create_and_drop;
3
use persy::Persy;
4
use std::thread;
5

6
#[test]
7 1
fn test_multithread_many_key_insert() {
8 1
    create_and_drop("multithread_many_key_insert", |persy| {
9 1
        let mut tx = persy.begin().expect("persy begin");
10 1
        tx.create_index::<i32, i32>("tdbi", persy::ValueMode::REPLACE)
11
            .expect("persy create_index");
12 1
        let prepared = tx.prepare().expect("persy prepare_commit");
13 1
        prepared.commit().expect("persy commit");
14

15 1
        let my_insert = |persy: &Persy, key: i32, value: i32| {
16 1
            let mut tx = persy.begin().expect("persy begin");
17 1
            tx.put::<i32, i32>("tdbi", key, value).expect("persy put");
18 1
            let prepared = tx.prepare().expect("persy prepare_commit");
19 1
            prepared.commit().expect("persy commit");
20 1
        };
21

22 1
        let spawn_worker = |r| {
23 1
            let own_persy = persy.clone();
24 1
            thread::spawn(move || {
25 1
                for i in r {
26 1
                    my_insert(&own_persy, i, i);
27
                }
28 1
            })
29 1
        };
30

31 1
        let t_1 = spawn_worker(0..1000);
32 1
        let t_2 = spawn_worker(0..1000);
33 1
        let t_3 = spawn_worker(1000..2000);
34 1
        let t_4 = spawn_worker(1000..2000);
35

36 1
        assert!(t_1.join().is_ok());
37 1
        assert!(t_2.join().is_ok());
38 1
        assert!(t_3.join().is_ok());
39 1
        assert!(t_4.join().is_ok());
40 1
        for x in 0..2000 {
41 1
            assert!(persy.get::<i32, i32>("tdbi", &x).unwrap().is_some());
42
        }
43 1
        let range_iter = persy.range::<i32, i32, _>("tdbi", ..).unwrap();
44 1
        assert_eq!(range_iter.count(), 2000);
45 1
        let range_iter = persy.range::<i32, i32, _>("tdbi", ..).unwrap();
46 1
        assert_eq!(range_iter.rev().count(), 2000);
47 1
    });
48 1
}

Read our documentation on viewing source code .

Loading