use more mocking in tests
This commit is contained in:
parent
a0541738ab
commit
2bf235e226
@ -20,8 +20,7 @@ use util::H256;
|
|||||||
/// Represents what has to be handled by actor listening to chain events
|
/// Represents what has to be handled by actor listening to chain events
|
||||||
#[derive(Ipc)]
|
#[derive(Ipc)]
|
||||||
pub trait ChainNotify : Send + Sync {
|
pub trait ChainNotify : Send + Sync {
|
||||||
/// fires when chain has new blocks, not including those encountered during
|
/// fires when chain has new blocks.
|
||||||
/// a major sync.
|
|
||||||
fn new_blocks(&self,
|
fn new_blocks(&self,
|
||||||
_imported: Vec<H256>,
|
_imported: Vec<H256>,
|
||||||
_invalid: Vec<H256>,
|
_invalid: Vec<H256>,
|
||||||
|
@ -37,11 +37,29 @@ impl HashToNumber for Client {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// helper trait for broadcasting a block to take a snapshot at.
|
||||||
|
trait Broadcast: Send + Sync {
|
||||||
|
fn take_at(&self, num: Option<u64>);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Broadcast for IoChannel<ClientIoMessage> {
|
||||||
|
fn take_at(&self, num: Option<u64>) {
|
||||||
|
let num = match num {
|
||||||
|
Some(n) => n,
|
||||||
|
None => return,
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Err(e) = self.send(ClientIoMessage::TakeSnapshot(num)) {
|
||||||
|
warn!("Snapshot watcher disconnected from IoService: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A `ChainNotify` implementation which will trigger a snapshot event
|
/// A `ChainNotify` implementation which will trigger a snapshot event
|
||||||
/// at certain block numbers.
|
/// at certain block numbers.
|
||||||
pub struct Watcher {
|
pub struct Watcher {
|
||||||
oracle: Arc<HashToNumber>,
|
oracle: Arc<HashToNumber>,
|
||||||
channel: IoChannel<ClientIoMessage>,
|
broadcast: Box<Broadcast>,
|
||||||
period: u64,
|
period: u64,
|
||||||
history: u64,
|
history: u64,
|
||||||
}
|
}
|
||||||
@ -53,7 +71,7 @@ impl Watcher {
|
|||||||
pub fn new(client: Arc<Client>, channel: IoChannel<ClientIoMessage>, period: u64, history: u64) -> Self {
|
pub fn new(client: Arc<Client>, channel: IoChannel<ClientIoMessage>, period: u64, history: u64) -> Self {
|
||||||
Watcher {
|
Watcher {
|
||||||
oracle: client,
|
oracle: client,
|
||||||
channel: channel,
|
broadcast: Box::new(channel),
|
||||||
period: period,
|
period: period,
|
||||||
history: history,
|
history: history,
|
||||||
}
|
}
|
||||||
@ -70,7 +88,6 @@ impl ChainNotify for Watcher {
|
|||||||
_: Vec<H256>,
|
_: Vec<H256>,
|
||||||
_duration: u64)
|
_duration: u64)
|
||||||
{
|
{
|
||||||
|
|
||||||
let highest = imported.into_iter()
|
let highest = imported.into_iter()
|
||||||
.filter_map(|h| self.oracle.to_number(h))
|
.filter_map(|h| self.oracle.to_number(h))
|
||||||
.filter(|&num| num >= self.period + self.history)
|
.filter(|&num| num >= self.period + self.history)
|
||||||
@ -78,23 +95,20 @@ impl ChainNotify for Watcher {
|
|||||||
.filter(|num| num % self.period == 0)
|
.filter(|num| num % self.period == 0)
|
||||||
.fold(0, ::std::cmp::max);
|
.fold(0, ::std::cmp::max);
|
||||||
|
|
||||||
if highest != 0 {
|
match highest {
|
||||||
if let Err(e) = self.channel.send(ClientIoMessage::TakeSnapshot(highest)) {
|
0 => self.broadcast.take_at(None),
|
||||||
warn!("Snapshot watcher disconnected from IoService: {}", e);
|
_ => self.broadcast.take_at(Some(highest)),
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::{HashToNumber, Watcher};
|
use super::{Broadcast, HashToNumber, Watcher};
|
||||||
|
|
||||||
use client::ChainNotify;
|
use client::ChainNotify;
|
||||||
use service::ClientIoMessage;
|
|
||||||
|
|
||||||
use util::{H256, U256, Mutex};
|
use util::{H256, U256};
|
||||||
use io::{IoContext, IoHandler, IoService};
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@ -107,30 +121,23 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Handler(Arc<Mutex<Vec<u64>>>);
|
struct TestBroadcast(Option<u64>);
|
||||||
|
impl Broadcast for TestBroadcast {
|
||||||
impl IoHandler<ClientIoMessage> for Handler {
|
fn take_at(&self, num: Option<u64>) {
|
||||||
fn message(&self, _context: &IoContext<ClientIoMessage>, message: &ClientIoMessage) {
|
if num != self.0 {
|
||||||
match *message {
|
panic!("Watcher broadcast wrong number. Expected {:?}, found {:?}", self.0, num);
|
||||||
ClientIoMessage::TakeSnapshot(num) => self.0.lock().push(num),
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// helper harness for tests.
|
// helper harness for tests which expect a notification.
|
||||||
fn harness(numbers: Vec<u64>, period: u64, history: u64) -> Vec<u64> {
|
fn harness(numbers: Vec<u64>, period: u64, history: u64, expected: Option<u64>) {
|
||||||
let events = Arc::new(Mutex::new(Vec::new()));
|
|
||||||
|
|
||||||
let service = IoService::start().unwrap();
|
|
||||||
service.register_handler(Arc::new(Handler(events.clone()))).unwrap();
|
|
||||||
|
|
||||||
let hashes: Vec<_> = numbers.clone().into_iter().map(|x| H256::from(U256::from(x))).collect();
|
let hashes: Vec<_> = numbers.clone().into_iter().map(|x| H256::from(U256::from(x))).collect();
|
||||||
let mut map = hashes.clone().into_iter().zip(numbers).collect();
|
let map = hashes.clone().into_iter().zip(numbers).collect();
|
||||||
|
|
||||||
let watcher = Watcher {
|
let watcher = Watcher {
|
||||||
oracle: Arc::new(TestOracle(map)),
|
oracle: Arc::new(TestOracle(map)),
|
||||||
channel: service.channel(),
|
broadcast: Box::new(TestBroadcast(expected)),
|
||||||
period: period,
|
period: period,
|
||||||
history: history,
|
history: history,
|
||||||
};
|
};
|
||||||
@ -143,35 +150,27 @@ mod tests {
|
|||||||
vec![],
|
vec![],
|
||||||
0,
|
0,
|
||||||
);
|
);
|
||||||
|
|
||||||
drop(service);
|
|
||||||
|
|
||||||
// binding necessary for compilation.
|
|
||||||
let v = events.lock().clone();
|
|
||||||
v
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// helper
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_not_fire() {
|
fn should_not_fire() {
|
||||||
let events = harness(vec![0], 5, 0);
|
harness(vec![0], 5, 0, None);
|
||||||
assert_eq!(events, vec![]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn fires_once_for_two() {
|
fn fires_once_for_two() {
|
||||||
let events = harness(vec![14, 15], 10, 5);
|
harness(vec![14, 15], 10, 5, Some(10));
|
||||||
assert_eq!(events, vec![10]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn finds_highest() {
|
fn finds_highest() {
|
||||||
let events = harness(vec![15, 25], 10, 5);
|
harness(vec![15, 25], 10, 5, Some(20));
|
||||||
assert_eq!(events, vec![20]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn doesnt_fire_before_history() {
|
fn doesnt_fire_before_history() {
|
||||||
let events = harness(vec![10, 11], 10, 5);
|
harness(vec![10, 11], 10, 5, None);
|
||||||
assert_eq!(events, vec![]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user