From ab9b1d5eb4d831536d4a4df2eabf3b4e066b2bba Mon Sep 17 00:00:00 2001 From: niklasad1 Date: Mon, 2 Apr 2018 13:01:53 +0200 Subject: [PATCH 1/8] start --- Cargo.lock | 14 +++++++ Cargo.toml | 1 + hash-fetch/Cargo.toml | 1 + hash-fetch/src/client.rs | 10 ++--- hash-fetch/src/lib.rs | 4 ++ price-info/Cargo.toml | 1 + price-info/src/lib.rs | 7 +++- rpc/Cargo.toml | 1 + rpc/src/lib.rs | 3 ++ util/fake-fetch/Cargo.toml | 11 +++++ util/fake-fetch/res/registrar.json | 21 ++++++++++ util/fake-fetch/src/lib.rs | 65 ++++++++++++++++++++++++++++++ 12 files changed, 132 insertions(+), 7 deletions(-) create mode 100644 util/fake-fetch/Cargo.toml create mode 100644 util/fake-fetch/res/registrar.json create mode 100644 util/fake-fetch/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index dbc605dac5c..c15632d4ea0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1025,6 +1025,15 @@ dependencies = [ "vm 0.1.0", ] +[[package]] +name = "fake-fetch" +version = "0.0.1" +dependencies = [ + "fetch 0.1.0", + "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", + "hyper 0.11.24 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "fdlimit" version = "0.1.1" @@ -1943,6 +1952,7 @@ dependencies = [ "ethcore-transaction 0.1.0", "ethereum-types 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "ethkey 0.3.0", + "fake-fetch 0.0.1", "fdlimit 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "futures-cpupool 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2066,6 +2076,7 @@ dependencies = [ "ethabi-derive 5.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "ethcore-bytes 0.1.0", "ethereum-types 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fake-fetch 0.0.1", "fetch 0.1.0", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "futures-cpupool 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2151,6 +2162,8 @@ dependencies = [ "ethjson 0.1.0", "ethkey 0.3.0", "ethstore 0.2.0", + "ethsync 1.11.0", + "fake-fetch 0.0.1", "fetch 0.1.0", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "futures-cpupool 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2468,6 +2481,7 @@ dependencies = [ name = "price-info" version = "1.11.0" dependencies = [ + "fake-fetch 0.0.1", "fetch 0.1.0", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "futures-cpupool 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 09c00b3386b..291e157cc40 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -82,6 +82,7 @@ rustc_version = "0.2" pretty_assertions = "0.1" ipnetwork = "0.12.6" tempdir = "0.3" +fake-fetch = { path = "util/fake-fetch" } [target.'cfg(windows)'.dependencies] winapi = "0.2" diff --git a/hash-fetch/Cargo.toml b/hash-fetch/Cargo.toml index 12d49e648ca..6a735fe37e0 100644 --- a/hash-fetch/Cargo.toml +++ b/hash-fetch/Cargo.toml @@ -28,3 +28,4 @@ ethabi-contract = "5.0" [dev-dependencies] hyper = "0.11" parking_lot = "0.5" +fake-fetch = { path = "../util/fake-fetch" } diff --git a/hash-fetch/src/client.rs b/hash-fetch/src/client.rs index 201d65ba79e..ed36e226435 100644 --- a/hash-fetch/src/client.rs +++ b/hash-fetch/src/client.rs @@ -193,7 +193,7 @@ fn random_temp_path() -> PathBuf { #[cfg(test)] mod tests { - extern crate hyper; + use fake_fetch::FakeFetch; use rustc_hex::FromHex; use std::sync::{Arc, mpsc}; use parking_lot::Mutex; @@ -254,7 +254,7 @@ mod tests { fn should_return_error_if_hash_not_found() { // given let contract = Arc::new(FakeRegistrar::new()); - let fetch = FakeFetch { return_success: false }; + let fetch = FakeFetch::new(false); let client = Client::with_fetch(contract.clone(), CpuPool::new(1), fetch, Remote::new_sync()); // when @@ -272,7 +272,7 @@ mod tests { fn should_return_error_if_response_is_not_successful() { // given let registrar = Arc::new(registrar()); - let fetch = FakeFetch { return_success: false }; + let fetch = FakeFetch::new(false); let client = Client::with_fetch(registrar.clone(), CpuPool::new(1), fetch, Remote::new_sync()); // when @@ -290,7 +290,7 @@ mod tests { fn should_return_hash_mismatch() { // given let registrar = Arc::new(registrar()); - let fetch = FakeFetch { return_success: true }; + let fetch = FakeFetch::new(true); let mut client = Client::with_fetch(registrar.clone(), CpuPool::new(1), fetch, Remote::new_sync()); let path = random_temp_path(); let path2 = path.clone(); @@ -313,7 +313,7 @@ mod tests { fn should_return_path_if_hash_matches() { // given let registrar = Arc::new(registrar()); - let fetch = FakeFetch { return_success: true }; + let fetch = FakeFetch::new(true); let client = Client::with_fetch(registrar.clone(), CpuPool::new(1), fetch, Remote::new_sync()); // when diff --git a/hash-fetch/src/lib.rs b/hash-fetch/src/lib.rs index 6e74982bb84..18176be5005 100644 --- a/hash-fetch/src/lib.rs +++ b/hash-fetch/src/lib.rs @@ -42,6 +42,10 @@ extern crate ethabi_derive; extern crate ethabi_contract; #[cfg(test)] extern crate parking_lot; +#[cfg(test)] +extern crate hyper; +#[cfg(test)] +extern crate fake_fetch; mod client; diff --git a/price-info/Cargo.toml b/price-info/Cargo.toml index 5122ebb9dd7..8dd497be9a1 100644 --- a/price-info/Cargo.toml +++ b/price-info/Cargo.toml @@ -16,3 +16,4 @@ serde_json = "1.0" [dev-dependencies] hyper = "0.11" parking_lot = "0.5" +fake-fetch = { path = "../util/fake-fetch" } diff --git a/price-info/src/lib.rs b/price-info/src/lib.rs index 8b87f0c11de..1154159208f 100644 --- a/price-info/src/lib.rs +++ b/price-info/src/lib.rs @@ -25,6 +25,9 @@ extern crate serde_json; #[macro_use] extern crate log; +#[cfg(test)] +extern crate fake_fetch; + pub extern crate fetch; use std::cmp; @@ -190,11 +193,11 @@ mod test { } fn price_info_ok(response: &str) -> Client { - Client::new(FakeFetch(Some(response.to_owned()), Default::default()), CpuPool::new(1)) + Client::new(FakeFetch::new(false), CpuPool::new(1)) } fn price_info_not_found() -> Client { - Client::new(FakeFetch::new().unwrap(), CpuPool::new(1)) + Client::new(FakeFetch::new(false), CpuPool::new(1)) } #[test] diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml index c467f055375..a0639c4ccc2 100644 --- a/rpc/Cargo.toml +++ b/rpc/Cargo.toml @@ -70,3 +70,4 @@ pretty_assertions = "0.1" macros = { path = "../util/macros" } ethcore-network = { path = "../util/network" } kvdb-memorydb = { path = "../util/kvdb-memorydb" } +fake-fetch = { path = "../util/fake-fetch" } diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index 1a0989d4a2a..f3ba68a0650 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -91,6 +91,9 @@ extern crate macros; #[cfg(test)] extern crate kvdb_memorydb; +#[cfg(test)] +extern crate fake_fetch; + extern crate tempdir; pub extern crate jsonrpc_ws_server as ws; diff --git a/util/fake-fetch/Cargo.toml b/util/fake-fetch/Cargo.toml new file mode 100644 index 00000000000..217d7f461a5 --- /dev/null +++ b/util/fake-fetch/Cargo.toml @@ -0,0 +1,11 @@ +[package] +description = "Mock fetcher for testing" +name = "fake-fetch" +version = "0.0.1" +license = "GPL-3.0" +authors = ["Parity Technologies "] + +[dependencies] +fetch = { path = "../fetch" } +futures = "0.1" +hyper = "0.11" diff --git a/util/fake-fetch/res/registrar.json b/util/fake-fetch/res/registrar.json new file mode 100644 index 00000000000..38edcc7877f --- /dev/null +++ b/util/fake-fetch/res/registrar.json @@ -0,0 +1,21 @@ +[ + {"constant":false,"inputs":[{"name":"_new","type":"address"}],"name":"setOwner","outputs":[],"type":"function"}, + {"constant":false,"inputs":[{"name":"_name","type":"string"}],"name":"confirmReverse","outputs":[{"name":"success","type":"bool"}],"type":"function"}, + {"constant":false,"inputs":[{"name":"_name","type":"bytes32"}],"name":"reserve","outputs":[{"name":"success","type":"bool"}],"type":"function"}, + {"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_key","type":"string"},{"name":"_value","type":"bytes32"}],"name":"set","outputs":[{"name":"success","type":"bool"}],"type":"function"}, + {"constant":false,"inputs":[{"name":"_name","type":"bytes32"}],"name":"drop","outputs":[{"name":"success","type":"bool"}],"type":"function"}, + {"constant":true,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_key","type":"string"}],"name":"getAddress","outputs":[{"name":"","type":"address"}],"type":"function"}, + {"constant":false,"inputs":[{"name":"_amount","type":"uint256"}],"name":"setFee","outputs":[],"type":"function"}, + {"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_to","type":"address"}],"name":"transfer","outputs":[{"name":"success","type":"bool"}],"type":"function"}, + {"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"type":"function"}, + {"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"reserved","outputs":[{"name":"reserved","type":"bool"}],"type":"function"}, + {"constant":false,"inputs":[],"name":"drain","outputs":[],"type":"function"}, + {"constant":false,"inputs":[{"name":"_name","type":"string"},{"name":"_who","type":"address"}],"name":"proposeReverse","outputs":[{"name":"success","type":"bool"}],"type":"function"}, + {"constant":true,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_key","type":"string"}],"name":"getUint","outputs":[{"name":"","type":"uint256"}],"type":"function"}, + {"constant":true,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_key","type":"string"}],"name":"get","outputs":[{"name":"","type":"bytes32"}],"type":"function"}, + {"constant":true,"inputs":[],"name":"fee","outputs":[{"name":"","type":"uint256"}],"type":"function"}, + {"constant":true,"inputs":[{"name":"","type":"address"}],"name":"reverse","outputs":[{"name":"","type":"string"}],"type":"function"}, + {"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_key","type":"string"},{"name":"_value","type":"uint256"}],"name":"setUint","outputs":[{"name":"success","type":"bool"}],"type":"function"}, + {"constant":false,"inputs":[],"name":"removeReverse","outputs":[],"type":"function"}, + {"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_key","type":"string"},{"name":"_value","type":"address"}],"name":"setAddress","outputs":[{"name":"success","type":"bool"}],"type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"amount","type":"uint256"}],"name":"Drained","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"amount","type":"uint256"}],"name":"FeeChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"},{"indexed":true,"name":"owner","type":"address"}],"name":"Reserved","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"},{"indexed":true,"name":"oldOwner","type":"address"},{"indexed":true,"name":"newOwner","type":"address"}],"name":"Transferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"},{"indexed":true,"name":"owner","type":"address"}],"name":"Dropped","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"},{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"key","type":"string"}],"name":"DataChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"string"},{"indexed":true,"name":"reverse","type":"address"}],"name":"ReverseProposed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"string"},{"indexed":true,"name":"reverse","type":"address"}],"name":"ReverseConfirmed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"string"},{"indexed":true,"name":"reverse","type":"address"}],"name":"ReverseRemoved","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"old","type":"address"},{"indexed":true,"name":"current","type":"address"}],"name":"NewOwner","type":"event"} +] diff --git a/util/fake-fetch/src/lib.rs b/util/fake-fetch/src/lib.rs new file mode 100644 index 00000000000..f10c712cc2d --- /dev/null +++ b/util/fake-fetch/src/lib.rs @@ -0,0 +1,65 @@ +// Copyright 2015-2018 Parity Technologies (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +extern crate fetch; +extern crate hyper; +extern crate futures; + +use hyper::StatusCode; +use futures::{future, future::FutureResult}; +use fetch::{Fetch, Url, Method}; + + +#[derive(Clone, Default)] +pub struct FakeFetch { + success: bool, +} + +impl FakeFetch { + pub fn new(b: bool) -> Self { + FakeFetch { success: b } + } +} + +impl Fetch for FakeFetch { + type Result = FutureResult; + + fn fetch(&self, url: &str, _method: Method, abort: fetch::Abort) -> Self::Result { + let u = Url::parse(url).unwrap(); + future::ok(if self.success { + let r = hyper::Response::new().with_body(&b"Some content"[..]); + fetch::client::Response::new(u, r, abort) + } else { + fetch::client::Response::new(u, hyper::Response::new().with_status(StatusCode::NotFound), abort) + }) + } + + fn get(&self, url: &str, abort: fetch::Abort) -> Self::Result { + let url: Url = match url.parse() { + Ok(u) => u, + Err(e) => return future::err(e.into()) + }; + self.fetch(Request::get(url), abort) + } + + fn post(&self, url: &str, abort: fetch::Abort) -> Self::Result { + let url: Url = match url.parse() { + Ok(u) => u, + Err(e) => return future::err(e.into()) + }; + self.fetch(Request::post(url), abort) + } +} From b4269094957b4d2787efbdd825fed2b71092ab6d Mon Sep 17 00:00:00 2001 From: niklasad1 Date: Tue, 10 Apr 2018 12:03:32 +0200 Subject: [PATCH 2/8] hash-fetch --- hash-fetch/src/client.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/hash-fetch/src/client.rs b/hash-fetch/src/client.rs index ed36e226435..6ba78463b61 100644 --- a/hash-fetch/src/client.rs +++ b/hash-fetch/src/client.rs @@ -197,7 +197,6 @@ mod tests { use rustc_hex::FromHex; use std::sync::{Arc, mpsc}; use parking_lot::Mutex; - use futures::future; use futures_cpupool::CpuPool; use fetch::{self, Fetch, Url, Request}; use parity_reactor::Remote; @@ -304,7 +303,7 @@ mod tests { // then let result = rx.recv().unwrap(); - let hash = "0x06b0a4f426f6713234b2d4b2468640bc4e0bb72657a920ad24c5087153c593c8".into(); + let hash = "0x2be00befcf008bc0e7d9cdefc194db9c75352e8632f48498b5a6bfce9f02c88e".into(); assert_eq!(result.unwrap_err(), Error::HashMismatch { expected: 2.into(), got: hash }); assert!(!path.exists(), "Temporary file should be removed."); } @@ -318,7 +317,7 @@ mod tests { // when let (tx, rx) = mpsc::channel(); - client.fetch("0x06b0a4f426f6713234b2d4b2468640bc4e0bb72657a920ad24c5087153c593c8".into(), + client.fetch("0x2be00befcf008bc0e7d9cdefc194db9c75352e8632f48498b5a6bfce9f02c88e".into(), Default::default(), Box::new(move |result| { tx.send(result).unwrap(); })); From cab31c75a6a634e81346a5626a3c185d5e498803 Mon Sep 17 00:00:00 2001 From: niklasad1 Date: Tue, 10 Apr 2018 12:04:36 +0200 Subject: [PATCH 3/8] rpc --- rpc/src/v1/tests/helpers/mod.rs | 2 -- rpc/src/v1/tests/mocked/parity_set.rs | 8 +++++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/rpc/src/v1/tests/helpers/mod.rs b/rpc/src/v1/tests/helpers/mod.rs index aae48a2d280..8e1aeeb147d 100644 --- a/rpc/src/v1/tests/helpers/mod.rs +++ b/rpc/src/v1/tests/helpers/mod.rs @@ -17,14 +17,12 @@ //! Test rpc services. mod dapps; -mod fetch; mod miner_service; mod snapshot_service; mod sync_provider; mod update_service; pub use self::dapps::TestDappsService; -pub use self::fetch::TestFetch; pub use self::miner_service::TestMinerService; pub use self::snapshot_service::TestSnapshotService; pub use self::sync_provider::{Config, TestSyncProvider}; diff --git a/rpc/src/v1/tests/mocked/parity_set.rs b/rpc/src/v1/tests/mocked/parity_set.rs index 2ae8ab5b2be..63ba458c439 100644 --- a/rpc/src/v1/tests/mocked/parity_set.rs +++ b/rpc/src/v1/tests/mocked/parity_set.rs @@ -26,9 +26,11 @@ use futures_cpupool::CpuPool; use jsonrpc_core::IoHandler; use v1::{ParitySet, ParitySetClient}; -use v1::tests::helpers::{TestMinerService, TestFetch, TestUpdater, TestDappsService}; +use v1::tests::helpers::{TestMinerService, TestUpdater, TestDappsService}; use super::manage_network::TestManageNetwork; +use fake_fetch::FakeFetch; + fn miner_service() -> Arc { Arc::new(TestMinerService::default()) } @@ -45,7 +47,7 @@ fn updater_service() -> Arc { Arc::new(TestUpdater::default()) } -pub type TestParitySetClient = ParitySetClient; +pub type TestParitySetClient = ParitySetClient; fn parity_set_client( client: &Arc, @@ -55,7 +57,7 @@ fn parity_set_client( ) -> TestParitySetClient { let dapps_service = Arc::new(TestDappsService); let pool = CpuPool::new(1); - ParitySetClient::new(client, miner, updater, &(net.clone() as Arc), Some(dapps_service), TestFetch::default(), pool) + ParitySetClient::new(client, miner, updater, &(net.clone() as Arc), Some(dapps_service), FakeFetch::new(true), pool) } #[test] From 2b3f5c6e489c64fa0dd2a0a8512997a0e0ba4eda Mon Sep 17 00:00:00 2001 From: niklasad1 Date: Tue, 10 Apr 2018 15:10:52 +0200 Subject: [PATCH 4/8] revert price-info --- price-info/src/lib.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/price-info/src/lib.rs b/price-info/src/lib.rs index 1154159208f..8b87f0c11de 100644 --- a/price-info/src/lib.rs +++ b/price-info/src/lib.rs @@ -25,9 +25,6 @@ extern crate serde_json; #[macro_use] extern crate log; -#[cfg(test)] -extern crate fake_fetch; - pub extern crate fetch; use std::cmp; @@ -193,11 +190,11 @@ mod test { } fn price_info_ok(response: &str) -> Client { - Client::new(FakeFetch::new(false), CpuPool::new(1)) + Client::new(FakeFetch(Some(response.to_owned()), Default::default()), CpuPool::new(1)) } fn price_info_not_found() -> Client { - Client::new(FakeFetch::new(false), CpuPool::new(1)) + Client::new(FakeFetch::new().unwrap(), CpuPool::new(1)) } #[test] From a1f2783fca697a21bd5e7a62fc5e8ef7a9d6e650 Mon Sep 17 00:00:00 2001 From: niklasad1 Date: Tue, 10 Apr 2018 15:11:49 +0200 Subject: [PATCH 5/8] remove used file --- util/fake-fetch/res/registrar.json | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 util/fake-fetch/res/registrar.json diff --git a/util/fake-fetch/res/registrar.json b/util/fake-fetch/res/registrar.json deleted file mode 100644 index 38edcc7877f..00000000000 --- a/util/fake-fetch/res/registrar.json +++ /dev/null @@ -1,21 +0,0 @@ -[ - {"constant":false,"inputs":[{"name":"_new","type":"address"}],"name":"setOwner","outputs":[],"type":"function"}, - {"constant":false,"inputs":[{"name":"_name","type":"string"}],"name":"confirmReverse","outputs":[{"name":"success","type":"bool"}],"type":"function"}, - {"constant":false,"inputs":[{"name":"_name","type":"bytes32"}],"name":"reserve","outputs":[{"name":"success","type":"bool"}],"type":"function"}, - {"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_key","type":"string"},{"name":"_value","type":"bytes32"}],"name":"set","outputs":[{"name":"success","type":"bool"}],"type":"function"}, - {"constant":false,"inputs":[{"name":"_name","type":"bytes32"}],"name":"drop","outputs":[{"name":"success","type":"bool"}],"type":"function"}, - {"constant":true,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_key","type":"string"}],"name":"getAddress","outputs":[{"name":"","type":"address"}],"type":"function"}, - {"constant":false,"inputs":[{"name":"_amount","type":"uint256"}],"name":"setFee","outputs":[],"type":"function"}, - {"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_to","type":"address"}],"name":"transfer","outputs":[{"name":"success","type":"bool"}],"type":"function"}, - {"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"type":"function"}, - {"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"reserved","outputs":[{"name":"reserved","type":"bool"}],"type":"function"}, - {"constant":false,"inputs":[],"name":"drain","outputs":[],"type":"function"}, - {"constant":false,"inputs":[{"name":"_name","type":"string"},{"name":"_who","type":"address"}],"name":"proposeReverse","outputs":[{"name":"success","type":"bool"}],"type":"function"}, - {"constant":true,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_key","type":"string"}],"name":"getUint","outputs":[{"name":"","type":"uint256"}],"type":"function"}, - {"constant":true,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_key","type":"string"}],"name":"get","outputs":[{"name":"","type":"bytes32"}],"type":"function"}, - {"constant":true,"inputs":[],"name":"fee","outputs":[{"name":"","type":"uint256"}],"type":"function"}, - {"constant":true,"inputs":[{"name":"","type":"address"}],"name":"reverse","outputs":[{"name":"","type":"string"}],"type":"function"}, - {"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_key","type":"string"},{"name":"_value","type":"uint256"}],"name":"setUint","outputs":[{"name":"success","type":"bool"}],"type":"function"}, - {"constant":false,"inputs":[],"name":"removeReverse","outputs":[],"type":"function"}, - {"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_key","type":"string"},{"name":"_value","type":"address"}],"name":"setAddress","outputs":[{"name":"success","type":"bool"}],"type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"amount","type":"uint256"}],"name":"Drained","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"amount","type":"uint256"}],"name":"FeeChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"},{"indexed":true,"name":"owner","type":"address"}],"name":"Reserved","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"},{"indexed":true,"name":"oldOwner","type":"address"},{"indexed":true,"name":"newOwner","type":"address"}],"name":"Transferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"},{"indexed":true,"name":"owner","type":"address"}],"name":"Dropped","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"},{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"key","type":"string"}],"name":"DataChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"string"},{"indexed":true,"name":"reverse","type":"address"}],"name":"ReverseProposed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"string"},{"indexed":true,"name":"reverse","type":"address"}],"name":"ReverseConfirmed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"string"},{"indexed":true,"name":"reverse","type":"address"}],"name":"ReverseRemoved","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"old","type":"address"},{"indexed":true,"name":"current","type":"address"}],"name":"NewOwner","type":"event"} -] From 3b9f56dd4bc7a8a1189a88b02a178601e568bae9 Mon Sep 17 00:00:00 2001 From: niklasad1 Date: Tue, 10 Apr 2018 16:17:49 +0200 Subject: [PATCH 6/8] adapt to changes in fetch --- Cargo.lock | 1 - hash-fetch/src/client.rs | 38 -------------------------------------- util/fake-fetch/src/lib.rs | 7 +++---- 3 files changed, 3 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c15632d4ea0..42fb1c7ad6b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2162,7 +2162,6 @@ dependencies = [ "ethjson 0.1.0", "ethkey 0.3.0", "ethstore 0.2.0", - "ethsync 1.11.0", "fake-fetch 0.0.1", "fetch 0.1.0", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/hash-fetch/src/client.rs b/hash-fetch/src/client.rs index 6ba78463b61..fd09561c9a4 100644 --- a/hash-fetch/src/client.rs +++ b/hash-fetch/src/client.rs @@ -198,47 +198,9 @@ mod tests { use std::sync::{Arc, mpsc}; use parking_lot::Mutex; use futures_cpupool::CpuPool; - use fetch::{self, Fetch, Url, Request}; use parity_reactor::Remote; use urlhint::tests::{FakeRegistrar, URLHINT}; use super::{Error, Client, HashFetch, random_temp_path}; - use self::hyper::StatusCode; - - - #[derive(Clone)] - struct FakeFetch { - return_success: bool - } - - impl Fetch for FakeFetch { - type Result = future::Ok; - - fn fetch(&self, request: Request, abort: fetch::Abort) -> Self::Result { - assert_eq!(request.url().as_str(), "https://parity.io/assets/images/ethcore-black-horizontal.png"); - let u = request.url().clone(); - future::ok(if self.return_success { - fetch::client::Response::new(u, hyper::Response::new().with_body(&b"result"[..]), abort) - } else { - fetch::client::Response::new(u, hyper::Response::new().with_status(StatusCode::NotFound), abort) - }) - } - - fn get(&self, url: &str, abort: fetch::Abort) -> Self::Result { - let url: Url = match url.parse() { - Ok(u) => u, - Err(e) => return future::err(e.into()) - }; - self.fetch(Request::get(url), abort) - } - - fn post(&self, url: &str, abort: fetch::Abort) -> Self::Result { - let url: Url = match url.parse() { - Ok(u) => u, - Err(e) => return future::err(e.into()) - }; - self.fetch(Request::post(url), abort) - } - } fn registrar() -> FakeRegistrar { let mut registrar = FakeRegistrar::new(); diff --git a/util/fake-fetch/src/lib.rs b/util/fake-fetch/src/lib.rs index f10c712cc2d..3ffcc7e3a7d 100644 --- a/util/fake-fetch/src/lib.rs +++ b/util/fake-fetch/src/lib.rs @@ -20,8 +20,7 @@ extern crate futures; use hyper::StatusCode; use futures::{future, future::FutureResult}; -use fetch::{Fetch, Url, Method}; - +use fetch::{Fetch, Url, Request}; #[derive(Clone, Default)] pub struct FakeFetch { @@ -37,8 +36,8 @@ impl FakeFetch { impl Fetch for FakeFetch { type Result = FutureResult; - fn fetch(&self, url: &str, _method: Method, abort: fetch::Abort) -> Self::Result { - let u = Url::parse(url).unwrap(); + fn fetch(&self, request: Request, abort: fetch::Abort) -> Self::Result { + let u = request.url().clone(); future::ok(if self.success { let r = hyper::Response::new().with_body(&b"Some content"[..]); fetch::client::Response::new(u, r, abort) From fb0923e6675d99b6fc2d97cbaf170b655b183fbf Mon Sep 17 00:00:00 2001 From: niklasad1 Date: Wed, 11 Apr 2018 09:30:43 +0200 Subject: [PATCH 7/8] make fake-fetch generic over --- util/fake-fetch/src/lib.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/util/fake-fetch/src/lib.rs b/util/fake-fetch/src/lib.rs index 3ffcc7e3a7d..9dbe05b9cab 100644 --- a/util/fake-fetch/src/lib.rs +++ b/util/fake-fetch/src/lib.rs @@ -23,22 +23,22 @@ use futures::{future, future::FutureResult}; use fetch::{Fetch, Url, Request}; #[derive(Clone, Default)] -pub struct FakeFetch { - success: bool, +pub struct FakeFetch where T: Clone + Send + Sync { + val: Option, } -impl FakeFetch { - pub fn new(b: bool) -> Self { - FakeFetch { success: b } +impl FakeFetch where T: Clone + Send + Sync { + pub fn new(t: Option) -> Self { + FakeFetch { val : t } } } -impl Fetch for FakeFetch { +impl Fetch for FakeFetch where T: Clone + Send+ Sync { type Result = FutureResult; fn fetch(&self, request: Request, abort: fetch::Abort) -> Self::Result { let u = request.url().clone(); - future::ok(if self.success { + future::ok(if self.val.is_some() { let r = hyper::Response::new().with_body(&b"Some content"[..]); fetch::client::Response::new(u, r, abort) } else { From 1ef260a76fa8cf5f593afe9cc371bd311b5cacc6 Mon Sep 17 00:00:00 2001 From: niklasad1 Date: Wed, 11 Apr 2018 09:49:59 +0200 Subject: [PATCH 8/8] fix tests to comply with the new `fake-fetch` --- hash-fetch/src/client.rs | 8 ++-- price-info/src/lib.rs | 65 ++++----------------------- rpc/src/v1/tests/helpers/fetch.rs | 59 ------------------------ rpc/src/v1/tests/mocked/parity_set.rs | 4 +- 4 files changed, 15 insertions(+), 121 deletions(-) delete mode 100644 rpc/src/v1/tests/helpers/fetch.rs diff --git a/hash-fetch/src/client.rs b/hash-fetch/src/client.rs index fd09561c9a4..1c7d417758f 100644 --- a/hash-fetch/src/client.rs +++ b/hash-fetch/src/client.rs @@ -215,7 +215,7 @@ mod tests { fn should_return_error_if_hash_not_found() { // given let contract = Arc::new(FakeRegistrar::new()); - let fetch = FakeFetch::new(false); + let fetch = FakeFetch::new(None::); let client = Client::with_fetch(contract.clone(), CpuPool::new(1), fetch, Remote::new_sync()); // when @@ -233,7 +233,7 @@ mod tests { fn should_return_error_if_response_is_not_successful() { // given let registrar = Arc::new(registrar()); - let fetch = FakeFetch::new(false); + let fetch = FakeFetch::new(None::); let client = Client::with_fetch(registrar.clone(), CpuPool::new(1), fetch, Remote::new_sync()); // when @@ -251,7 +251,7 @@ mod tests { fn should_return_hash_mismatch() { // given let registrar = Arc::new(registrar()); - let fetch = FakeFetch::new(true); + let fetch = FakeFetch::new(Some(1)); let mut client = Client::with_fetch(registrar.clone(), CpuPool::new(1), fetch, Remote::new_sync()); let path = random_temp_path(); let path2 = path.clone(); @@ -274,7 +274,7 @@ mod tests { fn should_return_path_if_hash_matches() { // given let registrar = Arc::new(registrar()); - let fetch = FakeFetch::new(true); + let fetch = FakeFetch::new(Some(1)); let client = Client::with_fetch(registrar.clone(), CpuPool::new(1), fetch, Remote::new_sync()); // when diff --git a/price-info/src/lib.rs b/price-info/src/lib.rs index 8b87f0c11de..9685fc7eee9 100644 --- a/price-info/src/lib.rs +++ b/price-info/src/lib.rs @@ -25,6 +25,9 @@ extern crate serde_json; #[macro_use] extern crate log; +#[cfg(test)] +extern crate fake_fetch; + pub extern crate fetch; use std::cmp; @@ -133,68 +136,18 @@ impl Client { #[cfg(test)] mod test { - extern crate hyper; - extern crate parking_lot; - - use self::parking_lot::Mutex; use std::sync::Arc; - use std::sync::atomic::{AtomicBool, Ordering}; - use fetch; - use fetch::{Fetch, Url, Request}; use futures_cpupool::CpuPool; - use futures::future::{self, FutureResult}; use Client; - use self::hyper::StatusCode; - - #[derive(Clone)] - struct FakeFetch(Option, Arc>); - - impl FakeFetch { - fn new() -> Result { - Ok(FakeFetch(None, Default::default())) - } - } - - impl Fetch for FakeFetch { - type Result = FutureResult; - - fn fetch(&self, request: Request, abort: fetch::Abort) -> Self::Result { - assert_eq!(request.url().as_str(), "https://api.etherscan.io/api?module=stats&action=ethprice"); - let u = request.url().clone(); - let mut val = self.1.lock(); - *val = *val + 1; - if let Some(ref response) = self.0 { - let r = hyper::Response::new().with_body(response.clone()); - future::ok(fetch::client::Response::new(u, r, abort)) - } else { - let r = hyper::Response::new().with_status(StatusCode::NotFound); - future::ok(fetch::client::Response::new(u, r, abort)) - } - } - - fn get(&self, url: &str, abort: fetch::Abort) -> Self::Result { - let url: Url = match url.parse() { - Ok(u) => u, - Err(e) => return future::err(e.into()) - }; - self.fetch(Request::get(url), abort) - } - - fn post(&self, url: &str, abort: fetch::Abort) -> Self::Result { - let url: Url = match url.parse() { - Ok(u) => u, - Err(e) => return future::err(e.into()) - }; - self.fetch(Request::post(url), abort) - } - } + use std::sync::atomic::{AtomicBool, Ordering}; + use fake_fetch::FakeFetch; - fn price_info_ok(response: &str) -> Client { - Client::new(FakeFetch(Some(response.to_owned()), Default::default()), CpuPool::new(1)) + fn price_info_ok(response: &str) -> Client> { + Client::new(FakeFetch::new(Some(response.to_owned())), CpuPool::new(1)) } - fn price_info_not_found() -> Client { - Client::new(FakeFetch::new().unwrap(), CpuPool::new(1)) + fn price_info_not_found() -> Client> { + Client::new(FakeFetch::new(None::), CpuPool::new(1)) } #[test] diff --git a/rpc/src/v1/tests/helpers/fetch.rs b/rpc/src/v1/tests/helpers/fetch.rs deleted file mode 100644 index 7de3949c4bc..00000000000 --- a/rpc/src/v1/tests/helpers/fetch.rs +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright 2015-2017 Parity Technologies (UK) Ltd. -// This file is part of Parity. - -// Parity is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// Parity is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with Parity. If not, see . - -//! Test implementation of fetch client. - -use std::thread; -use std::boxed::Box; -use jsonrpc_core::futures::{self, Future}; -use fetch::{self, Fetch, Url, Request}; -use futures::future; -use hyper; - -/// Test implementation of fetcher. Will always return the same file. -#[derive(Default, Clone)] -pub struct TestFetch; - -impl Fetch for TestFetch { - type Result = Box + Send + 'static>; - - fn fetch(&self, request: Request, abort: fetch::Abort) -> Self::Result { - let u = request.url().clone(); - let (tx, rx) = futures::oneshot(); - thread::spawn(move || { - let r = hyper::Response::new().with_body(&b"Some content"[..]); - tx.send(fetch::Response::new(u, r, abort)).unwrap(); - }); - - Box::new(rx.map_err(|_| fetch::Error::Aborted)) - } - - fn get(&self, url: &str, abort: fetch::Abort) -> Self::Result { - let url: Url = match url.parse() { - Ok(u) => u, - Err(e) => return Box::new(future::err(e.into())) - }; - self.fetch(Request::get(url), abort) - } - - fn post(&self, url: &str, abort: fetch::Abort) -> Self::Result { - let url: Url = match url.parse() { - Ok(u) => u, - Err(e) => return Box::new(future::err(e.into())) - }; - self.fetch(Request::post(url), abort) - } -} diff --git a/rpc/src/v1/tests/mocked/parity_set.rs b/rpc/src/v1/tests/mocked/parity_set.rs index 63ba458c439..af08236fdf6 100644 --- a/rpc/src/v1/tests/mocked/parity_set.rs +++ b/rpc/src/v1/tests/mocked/parity_set.rs @@ -47,7 +47,7 @@ fn updater_service() -> Arc { Arc::new(TestUpdater::default()) } -pub type TestParitySetClient = ParitySetClient; +pub type TestParitySetClient = ParitySetClient>; fn parity_set_client( client: &Arc, @@ -57,7 +57,7 @@ fn parity_set_client( ) -> TestParitySetClient { let dapps_service = Arc::new(TestDappsService); let pool = CpuPool::new(1); - ParitySetClient::new(client, miner, updater, &(net.clone() as Arc), Some(dapps_service), FakeFetch::new(true), pool) + ParitySetClient::new(client, miner, updater, &(net.clone() as Arc), Some(dapps_service), FakeFetch::new(Some(1)), pool) } #[test]