From 1bc77f843b73458de219957629bf44d781e0199f Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Fri, 4 Apr 2014 15:06:45 +0200 Subject: [PATCH] More mock clock conversions, to avoid failures when running test cases independently. Probably we should be injecting a mock Clock class so it goes away at the end of each test, but this would complicate the API somewhat. --- .../test/java/com/google/bitcoin/core/ECKeyTest.java | 2 +- .../google/bitcoin/core/FullBlockTestGenerator.java | 2 +- .../test/java/com/google/bitcoin/core/PeerTest.java | 5 ++--- .../test/java/com/google/bitcoin/core/WalletTest.java | 10 +++++++--- .../protocols/channels/ChannelConnectionTest.java | 2 +- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/core/src/test/java/com/google/bitcoin/core/ECKeyTest.java b/core/src/test/java/com/google/bitcoin/core/ECKeyTest.java index b7d373a4..d948b66a 100644 --- a/core/src/test/java/com/google/bitcoin/core/ECKeyTest.java +++ b/core/src/test/java/com/google/bitcoin/core/ECKeyTest.java @@ -250,7 +250,7 @@ public class ECKeyTest { @Test public void testUnencryptedCreate() throws Exception { - Utils.rollMockClock(0); + Utils.setMockClock(); ECKey key = new ECKey(); long time = key.getCreationTimeSeconds(); assertNotEquals(0, time); diff --git a/core/src/test/java/com/google/bitcoin/core/FullBlockTestGenerator.java b/core/src/test/java/com/google/bitcoin/core/FullBlockTestGenerator.java index 7d3cabe3..e29eb91d 100644 --- a/core/src/test/java/com/google/bitcoin/core/FullBlockTestGenerator.java +++ b/core/src/test/java/com/google/bitcoin/core/FullBlockTestGenerator.java @@ -96,7 +96,7 @@ public class FullBlockTestGenerator { this.params = params; coinbaseOutKey = new ECKey(); coinbaseOutKeyPubKey = coinbaseOutKey.getPubKey(); - Utils.rollMockClock(0); // Set a mock clock for timestamp tests + Utils.setMockClock(); } public RuleList getBlocksToTest(boolean addSigExpensiveBlocks, boolean runLargeReorgs, File blockStorageFile) throws ScriptException, ProtocolException, IOException { diff --git a/core/src/test/java/com/google/bitcoin/core/PeerTest.java b/core/src/test/java/com/google/bitcoin/core/PeerTest.java index 31f56ee6..37412b8a 100644 --- a/core/src/test/java/com/google/bitcoin/core/PeerTest.java +++ b/core/src/test/java/com/google/bitcoin/core/PeerTest.java @@ -36,7 +36,6 @@ import java.math.BigInteger; import java.net.InetSocketAddress; import java.net.SocketException; import java.nio.channels.CancelledKeyException; -import java.nio.channels.ClosedChannelException; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; @@ -436,7 +435,7 @@ public class PeerTest extends TestWithNetworkConnections { @Test public void fastCatchup() throws Exception { connect(); - + Utils.setMockClock(); // Check that blocks before the fast catchup point are retrieved using getheaders, and after using getblocks. // This test is INCOMPLETE because it does not check we handle >2000 blocks correctly. Block b1 = createFakeBlock(blockStore).block; @@ -484,7 +483,7 @@ public class PeerTest extends TestWithNetworkConnections { @Test public void pingPong() throws Exception { connect(); - Utils.rollMockClock(0); + Utils.setMockClock(); // No ping pong happened yet. assertEquals(Long.MAX_VALUE, peer.getLastPingTime()); assertEquals(Long.MAX_VALUE, peer.getPingTime()); diff --git a/core/src/test/java/com/google/bitcoin/core/WalletTest.java b/core/src/test/java/com/google/bitcoin/core/WalletTest.java index 018d9752..0fc2f358 100644 --- a/core/src/test/java/com/google/bitcoin/core/WalletTest.java +++ b/core/src/test/java/com/google/bitcoin/core/WalletTest.java @@ -926,7 +926,7 @@ public class WalletTest extends TestWithWallet { @Test public void transactionsList() throws Exception { // Check the wallet can give us an ordered list of all received transactions. - Utils.rollMockClock(0); + Utils.setMockClock(); Transaction tx1 = sendMoneyToWallet(Utils.toNanoCoins(1, 0), AbstractBlockChain.NewBlockType.BEST_CHAIN); Utils.rollMockClock(60 * 10); Transaction tx2 = sendMoneyToWallet(Utils.toNanoCoins(0, 5), AbstractBlockChain.NewBlockType.BEST_CHAIN); @@ -964,7 +964,8 @@ public class WalletTest extends TestWithWallet { @Test public void keyCreationTime() throws Exception { wallet = new Wallet(params); - long now = Utils.rollMockClock(0).getTime() / 1000; // Fix the mock clock. + Utils.setMockClock(); + long now = Utils.currentTimeMillis() / 1000; // No keys returns current time. assertEquals(now, wallet.getEarliestKeyCreationTime()); Utils.rollMockClock(60); @@ -978,7 +979,8 @@ public class WalletTest extends TestWithWallet { @Test public void scriptCreationTime() throws Exception { wallet = new Wallet(params); - long now = Utils.rollMockClock(0).getTime() / 1000; // Fix the mock clock. + Utils.setMockClock(); + long now = Utils.currentTimeMillis() / 1000; // No keys returns current time. assertEquals(now, wallet.getEarliestKeyCreationTime()); Utils.rollMockClock(60); @@ -2164,6 +2166,7 @@ public class WalletTest extends TestWithWallet { @Test public void keyRotation() throws Exception { + Utils.setMockClock(); // Watch out for wallet-initiated broadcasts. MockTransactionBroadcaster broadcaster = new MockTransactionBroadcaster(wallet); wallet.setTransactionBroadcaster(broadcaster); @@ -2236,6 +2239,7 @@ public class WalletTest extends TestWithWallet { ECKey key = new ECKey(); wallet.addKey(key); Address address = key.toAddress(params); + Utils.setMockClock(); Utils.rollMockClock(86400); for (int i = 0; i < 800; i++) { sendMoneyToWallet(wallet, Utils.CENT, address, AbstractBlockChain.NewBlockType.BEST_CHAIN); diff --git a/core/src/test/java/com/google/bitcoin/protocols/channels/ChannelConnectionTest.java b/core/src/test/java/com/google/bitcoin/protocols/channels/ChannelConnectionTest.java index e97fde0e..662580d4 100644 --- a/core/src/test/java/com/google/bitcoin/protocols/channels/ChannelConnectionTest.java +++ b/core/src/test/java/com/google/bitcoin/protocols/channels/ChannelConnectionTest.java @@ -268,7 +268,7 @@ public class ChannelConnectionTest extends TestWithWallet { @Test public void testChannelResume() throws Exception { // Tests various aspects of channel resuming. - Utils.rollMockClock(0); + Utils.setMockClock(); final Sha256Hash someServerId = Sha256Hash.create(new byte[]{});