test for request_set and remove idle flag

This commit is contained in:
Robert Habermeier 2017-01-04 18:43:11 +01:00
parent 04282be721
commit f63faea308
2 changed files with 28 additions and 13 deletions

View File

@ -140,7 +140,6 @@ struct Peer {
sent_head: H256, // last chain head we've given them. sent_head: H256, // last chain head we've given them.
last_update: SteadyTime, last_update: SteadyTime,
pending_requests: RequestSet, pending_requests: RequestSet,
idle: bool, // make into a current percentage of max buffer being requested?
} }
impl Peer { impl Peer {
@ -261,16 +260,10 @@ impl LightProtocol {
fn max_requests(&self, peer: PeerId, kind: request::Kind) -> usize { fn max_requests(&self, peer: PeerId, kind: request::Kind) -> usize {
self.peers.read().get(&peer).and_then(|peer| { self.peers.read().get(&peer).and_then(|peer| {
let mut peer = peer.lock(); let mut peer = peer.lock();
let idle = peer.idle;
match peer.remote_flow { match peer.remote_flow {
Some((ref mut buf, ref flow)) => { Some((ref mut buf, ref flow)) => {
flow.recharge(buf); flow.recharge(buf);
Some(flow.max_amount(&*buf, kind))
if !idle {
Some(0)
} else {
Some(flow.max_amount(&*buf, kind))
}
} }
None => None, None => None,
} }
@ -288,8 +281,6 @@ impl LightProtocol {
let peer = peers.get(peer_id).ok_or_else(|| Error::UnknownPeer)?; let peer = peers.get(peer_id).ok_or_else(|| Error::UnknownPeer)?;
let mut peer = peer.lock(); let mut peer = peer.lock();
if !peer.idle { return Err(Error::Overburdened) }
match peer.remote_flow { match peer.remote_flow {
Some((ref mut buf, ref flow)) => { Some((ref mut buf, ref flow)) => {
flow.recharge(buf); flow.recharge(buf);
@ -315,7 +306,6 @@ impl LightProtocol {
io.send(*peer_id, packet_id, packet_data); io.send(*peer_id, packet_id, packet_data);
peer.idle = false;
peer.pending_requests.insert(ReqId(req_id), request, SteadyTime::now()); peer.pending_requests.insert(ReqId(req_id), request, SteadyTime::now());
Ok(ReqId(req_id)) Ok(ReqId(req_id))
@ -404,7 +394,6 @@ impl LightProtocol {
match peers.get(peer) { match peers.get(peer) {
Some(peer_info) => { Some(peer_info) => {
let mut peer_info = peer_info.lock(); let mut peer_info = peer_info.lock();
peer_info.idle = true;
match peer_info.pending_requests.remove(&req_id, SteadyTime::now()) { match peer_info.pending_requests.remove(&req_id, SteadyTime::now()) {
None => return Err(Error::UnsolicitedResponse), None => return Err(Error::UnsolicitedResponse),
@ -596,7 +585,6 @@ impl LightProtocol {
sent_head: pending.sent_head, sent_head: pending.sent_head,
last_update: pending.last_update, last_update: pending.last_update,
pending_requests: RequestSet::default(), pending_requests: RequestSet::default(),
idle: true,
})); }));
for handler in &self.handlers { for handler in &self.handlers {

View File

@ -111,3 +111,30 @@ impl RequestSet {
self.ids.keys().cloned().collect() self.ids.keys().cloned().collect()
} }
} }
#[cfg(test)]
mod tests {
use net::{timeout, ReqId};
use request::{Request, Receipts};
use time::{SteadyTime, Duration};
use super::RequestSet;
#[test]
fn multi_timeout() {
let test_begin = SteadyTime::now();
let mut req_set = RequestSet::default();
let the_req = Request::Receipts(Receipts { block_hashes: Vec::new() });
req_set.insert(ReqId(0), the_req.clone(), test_begin);
req_set.insert(ReqId(1), the_req, test_begin + Duration::seconds(1));
assert_eq!(req_set.base, Some(test_begin));
let test_end = test_begin + Duration::milliseconds(timeout::RECEIPTS);
assert!(req_set.check_timeout(test_end));
req_set.remove(&ReqId(0), test_begin + Duration::seconds(1)).unwrap();
assert!(!req_set.check_timeout(test_end));
assert!(req_set.check_timeout(test_end + Duration::seconds(1)));
}
}