<commit_before>extern crate iron;
extern crate persistent;

#[cfg(test)]
mod test {
    use std::sync::{Arc, Mutex};
    use persistent::Persistent;
    use iron::{Chain, StackChain, Request, Response, Alloy};
    use std::mem::uninitialized;

    fn unsafe_dispatch<C: Chain>(chain: &mut C) {
        unsafe {
            let _ = chain.dispatch(uninitialized(), uninitialized(), None);
        }
    }

    #[test]
    fn inserts_data() {
        fn data_exists(_: &mut Request, _: &mut Response, alloy: &mut Alloy) {
            assert_eq!(*alloy.find::<Persistent<int, int>>().unwrap().data.lock(), 154);
        }

        let data = Arc::new(Mutex::new(154));
        let persistent: Persistent<int, int> = Persistent { data: data.clone() };
        let mut testchain: StackChain = Chain::new();
        testchain.link(persistent);
        testchain.link(data_exists);

        unsafe_dispatch(&mut testchain);
    }

    #[test]
    fn changes_when_modified() {
        fn modify_data(_: &mut Request, _: &mut Response, alloy: &mut Alloy) {
            *alloy.find::<Persistent<int, int>>().unwrap().data.lock() += 1;
        }

        fn data_modified(_: &mut Request, _: &mut Response, alloy: &mut Alloy) {
            assert_eq!(*alloy.find::<Persistent<int, int>>().unwrap().data.lock(), 155);
        }

        let data = Arc::new(Mutex::new(154));
        let persistent: Persistent<int, int> = Persistent { data: data.clone() };
        let mut testchain: StackChain = Chain::new();
        testchain.link(persistent);
        testchain.link(modify_data);
        testchain.link(data_modified);

        unsafe_dispatch(&mut testchain);
    }

    #[test]
    fn persists_between_calls() {
        fn modify_data(_: &mut Request, _: &mut Response, alloy: &mut Alloy) {
            *alloy.find::<Persistent<int, int>>().unwrap().data.lock() += 1;
        }

        fn data_modified(_: &mut Request, _: &mut Response, alloy: &mut Alloy) {
            assert_eq!(*alloy.find::<Persistent<int, int>>().unwrap().data.lock(), 156);
        }

        let data = Arc::new(Mutex::new(154));
        let persistent: Persistent<int, int> = Persistent { data: data.clone() };
        let mut testchain: StackChain = Chain::new();
        testchain.link(persistent);
        testchain.link(modify_data);
        unsafe_dispatch(&mut testchain);

        testchain.link(data_modified);
        unsafe_dispatch(&mut testchain);
    }
}
<commit_msg>(test) Updated tests to use RWLock.<commit_after>extern crate iron;
extern crate persistent;

#[cfg(test)]
mod test {
    use std::sync::{Arc, RWLock};
    use persistent::Persistent;
    use iron::{Chain, StackChain, Request, Response, Alloy};
    use std::mem::uninitialized;

    fn unsafe_dispatch<C: Chain>(chain: &mut C) {
        unsafe {
            let _ = chain.dispatch(uninitialized(), uninitialized(), None);
        }
    }

    #[test]
    fn inserts_data() {
        fn data_exists(_: &mut Request, _: &mut Response, alloy: &mut Alloy) {
            assert_eq!(*alloy.find::<Persistent<int, int>>().unwrap().data.read(), 154);
        }

        let data = Arc::new(RWLock::new(154));
        let persistent: Persistent<int, int> = Persistent { data: data.clone() };
        let mut testchain: StackChain = Chain::new();
        testchain.link(persistent);
        testchain.link(data_exists);

        unsafe_dispatch(&mut testchain);
    }

    #[test]
    fn changes_when_modified() {
        fn modify_data(_: &mut Request, _: &mut Response, alloy: &mut Alloy) {
            *alloy.find::<Persistent<int, int>>().unwrap().data.write() += 1;
        }

        fn data_modified(_: &mut Request, _: &mut Response, alloy: &mut Alloy) {
            assert_eq!(*alloy.find::<Persistent<int, int>>().unwrap().data.read(), 155);
        }

        let data = Arc::new(RWLock::new(154));
        let persistent: Persistent<int, int> = Persistent { data: data.clone() };
        let mut testchain: StackChain = Chain::new();
        testchain.link(persistent);
        testchain.link(modify_data);
        testchain.link(data_modified);

        unsafe_dispatch(&mut testchain);
    }

    #[test]
    fn persists_between_calls() {
        fn modify_data(_: &mut Request, _: &mut Response, alloy: &mut Alloy) {
            *alloy.find::<Persistent<int, int>>().unwrap().data.write() += 1;
        }

        fn data_modified(_: &mut Request, _: &mut Response, alloy: &mut Alloy) {
            assert_eq!(*alloy.find::<Persistent<int, int>>().unwrap().data.read(), 156);
        }

        let data = Arc::new(RWLock::new(154));
        let persistent: Persistent<int, int> = Persistent { data: data.clone() };
        let mut testchain: StackChain = Chain::new();
        testchain.link(persistent);
        testchain.link(modify_data);
        unsafe_dispatch(&mut testchain);

        testchain.link(data_modified);
        unsafe_dispatch(&mut testchain);
    }
}

{'fraction_non_alphanumeric': 0.1572646103896104, 'fraction_numerical': 0.008116883116883116, 'mean_word_length': 2.7770114942528736, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7165746', 'n_tokens_mistral': 1563, 'n_tokens_neox': 1529, 'n_words': 329}