Add Core to test harness in bookmarks-test

Summary:
The `Bookmarks` trait assumes that the underlying implementation is asynchronous (as evidenced by the fact that all methods return `Future`s). Most non-toy implementations of an asynchronous bookmark store will need to schedule arbitrary tasks on the Core executing the returned Futures. (In some cases, the constructor itself may need to schedule tasks on the Core.) As such, this test harness should expose a Core to each test in a first class way (rather than trying to shoehorn it in via the `state`, which doesn't actually quite work here).

Existing implementations can just ignore the Core.

Reviewed By: jsgf

Differential Revision: D6215855

fbshipit-source-id: ea3f28bdaec667111b384db29d0c7c49954398e5
This commit is contained in:
Arun Kulshreshtha 2017-11-08 12:22:49 -08:00 committed by Facebook Github Bot
parent 81ce6d1cd2
commit 83293f1441

View File

@ -10,21 +10,26 @@
extern crate futures;
extern crate tempdir;
extern crate tokio_core;
extern crate bookmarks;
extern crate filebookmarks;
extern crate membookmarks;
extern crate storage_types;
use futures::{Future, Stream};
use std::cell::RefCell;
use std::rc::Rc;
use futures::Stream;
use tempdir::TempDir;
use tokio_core::reactor::Core;
use bookmarks::BookmarksMut;
use filebookmarks::FileBookmarks;
use membookmarks::MemBookmarks;
use storage_types::Version;
fn basic<B>(bookmarks: B)
fn basic<B>(bookmarks: B, core: &mut Core)
where
B: BookmarksMut<Value = String>,
{
@ -33,37 +38,44 @@ where
let two = "2".to_string();
let three = "3".to_string();
assert_eq!(bookmarks.get(&foo).wait().unwrap(), None);
assert_eq!(core.run(bookmarks.get(&foo)).unwrap(), None);
let absent = Version::absent();
let foo_v1 = bookmarks.set(&foo, &one, &absent).wait().unwrap().unwrap();
let foo_v1 = core.run(bookmarks.set(&foo, &one, &absent))
.unwrap()
.unwrap();
assert_eq!(
bookmarks.get(&foo).wait().unwrap(),
core.run(bookmarks.get(&foo)).unwrap(),
Some((one.clone(), foo_v1))
);
let foo_v2 = bookmarks.set(&foo, &two, &foo_v1).wait().unwrap().unwrap();
let foo_v2 = core.run(bookmarks.set(&foo, &two, &foo_v1))
.unwrap()
.unwrap();
// Should fail due to version mismatch.
assert_eq!(bookmarks.set(&foo, &three, &foo_v1).wait().unwrap(), None);
assert_eq!(
core.run(bookmarks.set(&foo, &three, &foo_v1)).unwrap(),
None
);
assert_eq!(
bookmarks.delete(&foo, &foo_v2).wait().unwrap().unwrap(),
core.run(bookmarks.delete(&foo, &foo_v2)).unwrap().unwrap(),
absent
);
assert_eq!(bookmarks.get(&foo).wait().unwrap(), None);
assert_eq!(core.run(bookmarks.get(&foo)).unwrap(), None);
// Even though bookmark doesn't exist, this should fail with a version mismatch.
assert_eq!(bookmarks.delete(&foo, &foo_v2).wait().unwrap(), None);
assert_eq!(core.run(bookmarks.delete(&foo, &foo_v2)).unwrap(), None);
// Deleting it with the absent version should work.
assert_eq!(
bookmarks.delete(&foo, &absent).wait().unwrap().unwrap(),
core.run(bookmarks.delete(&foo, &absent)).unwrap().unwrap(),
absent
);
}
fn list<B>(bookmarks: B)
fn list<B>(bookmarks: B, core: &mut Core)
where
B: BookmarksMut<Value = String>,
{
@ -71,30 +83,24 @@ where
let two = b"2";
let three = b"3";
let _ = bookmarks
.create(&one, &"foo".to_string())
.wait()
let _ = core.run(bookmarks.create(&one, &"foo".to_string()))
.unwrap()
.unwrap();
let _ = bookmarks
.create(&two, &"bar".to_string())
.wait()
let _ = core.run(bookmarks.create(&two, &"bar".to_string()))
.unwrap()
.unwrap();
let _ = bookmarks
.create(&three, &"baz".to_string())
.wait()
let _ = core.run(bookmarks.create(&three, &"baz".to_string()))
.unwrap()
.unwrap();
let mut result = bookmarks.keys().collect().wait().unwrap();
let mut result = core.run(bookmarks.keys().collect()).unwrap();
result.sort();
let expected = vec![one, two, three];
assert_eq!(result, expected);
}
fn persistence<F, B>(mut new_bookmarks: F)
fn persistence<F, B>(mut new_bookmarks: F, core: Rc<RefCell<Core>>)
where
F: FnMut() -> B,
B: BookmarksMut<Value = String>,
@ -104,11 +110,17 @@ where
let version = {
let bookmarks = new_bookmarks();
bookmarks.create(&foo, &bar).wait().unwrap().unwrap()
core.borrow_mut()
.run(bookmarks.create(&foo, &bar))
.unwrap()
.unwrap()
};
let bookmarks = new_bookmarks();
assert_eq!(bookmarks.get(&foo).wait().unwrap(), Some((bar, version)));
assert_eq!(
core.borrow_mut().run(bookmarks.get(&foo)).unwrap(),
Some((bar, version))
);
}
macro_rules! bookmarks_test_impl {
@ -122,22 +134,33 @@ macro_rules! bookmarks_test_impl {
#[test]
fn test_basic() {
let mut core = Core::new().unwrap();
let state = $state;
basic($new_cb(&state));
let bookmarks = $new_cb(&state, &mut core);
basic(bookmarks, &mut core);
}
#[test]
fn test_list() {
let mut core = Core::new().unwrap();
let state = $state;
list($new_cb(&state));
let bookmarks = $new_cb(&state, &mut core);
list(bookmarks, &mut core);
}
#[test]
fn test_persistence() {
// Not all bookmark implementations support persistence.
if $persistent {
let core = Rc::new(RefCell::new(Core::new().unwrap()));
let state = $state;
persistence(|| $new_cb(&state));
let new_bookmarks = {
let core = Rc::clone(&core);
move || {
$new_cb(&state, &mut *core.borrow_mut())
}
};
persistence(new_bookmarks, core);
}
}
}
@ -147,7 +170,7 @@ macro_rules! bookmarks_test_impl {
bookmarks_test_impl! {
membookmarks_test => {
state: (),
new: |_| MemBookmarks::new(),
new: |_, _| MemBookmarks::new(),
persistent: false,
}
}
@ -155,7 +178,7 @@ bookmarks_test_impl! {
bookmarks_test_impl! {
filebookmarks_test => {
state: TempDir::new("filebookmarks_test").unwrap(),
new: |dir: &TempDir| FileBookmarks::open(dir.as_ref()).unwrap(),
new: |dir: &TempDir, _| FileBookmarks::open(dir.as_ref()).unwrap(),
persistent: true,
}
}