use crate::utils::{open_database, tmpdir};
use leveldb::database::kv::KV;
use leveldb::database::slice::Slice;
use leveldb::options::{Options, WriteOptions};

#[test]
fn access_from_threads() {
    use std::sync::Arc;
    use std::thread;
    use std::thread::JoinHandle;

    let mut opts = Options::new();
    opts.create_if_missing = true;
    let tmp = tmpdir("sharing");
    let database = open_database(tmp.path(), true);
    let shared = Arc::new(database);

    let _ = (0..10)
        .map(|i| {
            let local_db = shared.clone();

            thread::spawn(move || {
                let write_opts = WriteOptions::new();
                let num = &[i as u8][..];
                match local_db.put(write_opts, Slice::from(num), Slice::from(num)) {
                    Ok(_) => {}
                    Err(e) => {
                        panic!("failed to write to database: {:?}", e)
                    }
                }
            })
        })
        .map(JoinHandle::join)
        .collect::<Vec<_>>();
}
