Skip to content

Commit

Permalink
Update Simulacrum
Browse files Browse the repository at this point in the history
This allows simplifying the workaround for issue 55.
pcsm/simulacrum#55
  • Loading branch information
asomers committed Oct 29, 2018
1 parent 77f8f89 commit 5a75a08
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ lto = true
members = ["isa-l"]

[patch.crates-io]
simulacrum_mock = { git = "https://github.com/pcsm/simulacrum/", rev = "872b48d" }
simulacrum_mock = { git = "https://github.com/pcsm/simulacrum/", rev = "13416e9" }
24 changes: 9 additions & 15 deletions src/common/ddml_mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ use simulacrum::*;
use std::{
any,
borrow::Borrow,
collections::hash_map::HashMap
};

pub struct DDMLMock {
e: Expectations,
m: HashMap<any::TypeId, &'static str>
}

impl DDMLMock {
Expand All @@ -42,7 +40,6 @@ impl DDMLMock {
pub fn new() -> Self {
Self {
e: Expectations::new(),
m: HashMap::new()
}
}

Expand Down Expand Up @@ -88,24 +85,17 @@ impl DDMLMock {
}

// Due to a bug in Simulacrum, mocking DDML::put is way overcomplicated.
// The test method must first call `expect_put_type` with the type to be
// mocked, and a name for it. Then `expect_put` and `put` can be used like
// normal.
//
// Simulacrum can't mock a single generic method with different type
// parameters more than once in the same test
// parameters more than once in the same test. The workaround is to
// dynamically construct the method name.
// https://github.com/pcsm/simulacrum/issues/55"]
pub fn expect_put<T: Cacheable>(&mut self) -> Method<(T, Compression, TxgT),
Box<Future<Item=DRP, Error=Error> + Send>>
{
let method_name = self.m.get(&any::TypeId::of::<T>()).unwrap_or(&"put");
self.e.expect::<(T, Compression, TxgT),
Box<Future<Item=DRP, Error=Error> + Send>>
(method_name)
}

pub fn expect_put_type<T: Cacheable>(&mut self, name: &'static str) {
self.m.insert(any::TypeId::of::<T>(), name);
(&DDMLMock::generic_method_name::<T>())
}

pub fn expect_sync_all(&mut self)
Expand All @@ -115,6 +105,10 @@ impl DDMLMock {
Box<Future<Item=(), Error=Error> + Send>>("sync_all")
}

fn generic_method_name<T: 'static + ?Sized>() -> String {
format!("put::<{:?}>", any::TypeId::of::<T>())
}

pub fn get_direct<T: Cacheable>(&self, drp: &DRP)
-> Box<Future<Item=Box<T>, Error=Error> + Send> {
self.e.was_called_returning::<*const DRP,
Expand Down Expand Up @@ -249,10 +243,10 @@ impl DML for DDMLMock {
txg:TxgT)
-> Box<Future<Item=DRP, Error=Error> + Send>
{
let method_name = self.m.get(&any::TypeId::of::<T>()).unwrap_or(&"put");
let method_name = DDMLMock::generic_method_name::<T>();
self.e.was_called_returning::<(T, Compression, TxgT),
(Box<Future<Item=DRP, Error=Error> + Send>)>
(method_name, (cacheable, compression, txg))
(&method_name, (cacheable, compression, txg))
}

fn sync_all(&self, txg: TxgT) -> Box<Future<Item=(), Error=Error> + Send>
Expand Down
18 changes: 10 additions & 8 deletions src/common/dml_mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::common::cache::{Cacheable, CacheRef};
use crate::common::dml::*;
use futures::Future;
use simulacrum::*;
use std::any;

/// A very simple mock object that implements `DML` and nothing else.
pub struct DMLMock {
Expand Down Expand Up @@ -38,20 +39,21 @@ impl DMLMock {
}

// Due to a bug in Simulacrum, mocking DDML::put is way overcomplicated.
// The test method must first call `expect_put_type` with the type to be
// mocked, and a name for it. Then `expect_put` and `put` can be used like
// normal.
//
// Simulacrum can't mock a single generic method with different type
// parameters more than once in the same test
// parameters more than once in the same test. The workaround is to
// dynamically construct the method name.
// https://github.com/pcsm/simulacrum/issues/55"]
pub fn expect_put<T: Cacheable>(&mut self) -> Method<(T, Compression, TxgT),
Box<Future<Item=u32, Error=Error> + Send>>
{
let method_name = &"put";
self.e.expect::<(T, Compression, TxgT),
Box<Future<Item=u32, Error=Error> + Send>>
(method_name)
(&DMLMock::generic_method_name::<T>())
}

fn generic_method_name<T: 'static + ?Sized>() -> String {
format!("put::<{:?}>", any::TypeId::of::<T>())
}

pub fn new() -> Self {
Expand Down Expand Up @@ -101,10 +103,10 @@ impl DML for DMLMock {
txg:TxgT)
-> Box<Future<Item=u32, Error=Error> + Send>
{
let method_name = &"put";
let method_name = DMLMock::generic_method_name::<T>();
self.e.was_called_returning::<(T, Compression, TxgT),
(Box<Future<Item=u32, Error=Error> + Send>)>
(method_name, (cacheable, compression, txg))
(&method_name, (cacheable, compression, txg))
}

fn sync_all(&self, txg: TxgT) -> Box<Future<Item=(), Error=Error> + Send>
Expand Down
4 changes: 0 additions & 4 deletions src/common/idml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1119,10 +1119,6 @@ mod t {
let mut ddml = DDML::new();
let drp = DRP::new(PBA::new(0, 0), Compression::None, 40000, 40000,
0xdead_beef);
ddml.expect_put_type::<Arc<tree::Node<DRP, RID, RidtEntry>>>
("put_ridt");
ddml.expect_put_type::<Arc<tree::Node<DRP, PBA, RID>>>
("put_alloct");
ddml.expect_put::<Arc<tree::Node<DRP, RID, RidtEntry>>>()
.called_any()
.with(params!(any(), any(), TxgT::from(42)))
Expand Down

0 comments on commit 5a75a08

Please sign in to comment.