From 28e4847b59f70cd4188a90583558ade66bbfc813 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Wed, 16 Mar 2016 10:37:08 +0100 Subject: [PATCH] tests --- rpc/src/v1/impls/eth.rs | 2 +- rpc/src/v1/tests/eth.rs | 28 +++++++++++++++++++---- rpc/src/v1/tests/helpers/miner_service.rs | 20 +++++++++++++--- rpc/src/v1/tests/helpers/sync_provider.rs | 9 ++++---- 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 32e9b44b1..520b37045 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -340,7 +340,7 @@ impl Eth for EthClient let seed_hash = Ethash::get_seedhash(b.block().header().number()); to_value(&(pow_hash, seed_hash, target)) } - _ => Err(Error::invalid_params()) + _ => Err(Error::internal_error()) } }, _ => Err(Error::invalid_params()) diff --git a/rpc/src/v1/tests/eth.rs b/rpc/src/v1/tests/eth.rs index 6bc929709..280557e4b 100644 --- a/rpc/src/v1/tests/eth.rs +++ b/rpc/src/v1/tests/eth.rs @@ -43,12 +43,12 @@ fn sync_provider() -> Arc { } fn miner_service() -> Arc { - Arc::new(TestMinerService) + Arc::new(TestMinerService::default()) } struct EthTester { - client: Arc, - _sync: Arc, + pub client: Arc, + pub sync: Arc, _accounts_provider: Arc, _miner: Arc, hashrates: Arc>>, @@ -68,7 +68,7 @@ impl Default for EthTester { io.add_delegate(eth); EthTester { client: client, - _sync: sync, + sync: sync, _accounts_provider: ap, _miner: miner, io: io, @@ -346,5 +346,25 @@ fn rpc_eth_compile_serpent() { assert_eq!(EthTester::default().io.handle_request(request), Some(response.to_owned())); } +#[test] +fn returns_no_work_if_cant_mine() { + let eth_tester = EthTester::default(); + let request = r#"{"jsonrpc": "2.0", "method": "eth_getWork", "params": [], "id": 1}"#; + let response = r#"{"jsonrpc":"2.0","result":["","",""],"id":1}"#; + assert_eq!(eth_tester.io.handle_request(request), Some(response.to_owned())); +} + +#[test] +fn returns_error_if_can_mine_and_no_closed_block() { + use ethsync::{SyncState}; + + let eth_tester = EthTester::default(); + eth_tester.sync.status.write().unwrap().state = SyncState::Idle; + + let request = r#"{"jsonrpc": "2.0", "method": "eth_getWork", "params": [], "id": 1}"#; + let response = r#"{"jsonrpc":"2.0","error":{"code":-32603,"message":"Internal error","data":null},"id":1}"#; + + assert_eq!(eth_tester.io.handle_request(request), Some(response.to_owned())); +} diff --git a/rpc/src/v1/tests/helpers/miner_service.rs b/rpc/src/v1/tests/helpers/miner_service.rs index 0cddf2a1e..3a60bd136 100644 --- a/rpc/src/v1/tests/helpers/miner_service.rs +++ b/rpc/src/v1/tests/helpers/miner_service.rs @@ -22,7 +22,19 @@ use ethcore::block::ClosedBlock; use ethcore::transaction::SignedTransaction; use ethminer::{MinerService, MinerStatus}; -pub struct TestMinerService; +pub struct TestMinerService { + pub imported_transactions: RwLock>, + pub latest_closed_block: Mutex>, +} + +impl Default for TestMinerService { + fn default() -> TestMinerService { + TestMinerService { + imported_transactions: RwLock::new(Vec::new()), + latest_closed_block: Mutex::new(None), + } + } +} impl MinerService for TestMinerService { @@ -45,9 +57,11 @@ impl MinerService for TestMinerService { fn prepare_sealing(&self, _chain: &BlockChainClient) { unimplemented!(); } /// Grab the `ClosedBlock` that we want to be sealed. Comes as a mutex that you have to lock. - fn sealing_block(&self, _chain: &BlockChainClient) -> &Mutex> { unimplemented!(); } + fn sealing_block(&self, _chain: &BlockChainClient) -> &Mutex> { + &self.latest_closed_block + } /// Submit `seal` as a valid solution for the header of `pow_hash`. /// Will check the seal, but not actually insert the block into the chain. fn submit_seal(&self, _chain: &BlockChainClient, _pow_hash: H256, _seal: Vec) -> Result<(), Error> { unimplemented!(); } -} \ No newline at end of file +} diff --git a/rpc/src/v1/tests/helpers/sync_provider.rs b/rpc/src/v1/tests/helpers/sync_provider.rs index 631752dfc..bfbfad44b 100644 --- a/rpc/src/v1/tests/helpers/sync_provider.rs +++ b/rpc/src/v1/tests/helpers/sync_provider.rs @@ -15,6 +15,7 @@ // along with Parity. If not, see . use ethsync::{SyncProvider, SyncStatus, SyncState}; +use std::sync::{RwLock}; pub struct Config { pub protocol_version: u8, @@ -22,13 +23,13 @@ pub struct Config { } pub struct TestSyncProvider { - status: SyncStatus, + pub status: RwLock, } impl TestSyncProvider { pub fn new(config: Config) -> Self { TestSyncProvider { - status: SyncStatus { + status: RwLock::new(SyncStatus { state: SyncState::NotSynced, protocol_version: config.protocol_version, start_block_number: 0, @@ -39,14 +40,14 @@ impl TestSyncProvider { num_peers: config.num_peers, num_active_peers: 0, mem_used: 0, - }, + }) } } } impl SyncProvider for TestSyncProvider { fn status(&self) -> SyncStatus { - self.status.clone() + self.status.read().unwrap().clone() } }