3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-12 10:15:52 +00:00

Fix race of mock clock with current time if tests are all run sequentially. This commit requires you to use one of the setMockClock() variants before being able to roll it.

This commit is contained in:
Andreas Schildbach 2014-03-29 10:26:57 +01:00
parent 1b0954a84a
commit 2cbdf324ab
6 changed files with 20 additions and 8 deletions

View File

@ -1,5 +1,6 @@
/** /**
* Copyright 2011 Google Inc. * Copyright 2011 Google Inc.
* Copyright 2014 Andreas Schildbach
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -460,13 +461,20 @@ public class Utils {
*/ */
public static Date rollMockClockMillis(long millis) { public static Date rollMockClockMillis(long millis) {
if (mockTime == null) if (mockTime == null)
mockTime = new Date(); throw new IllegalStateException("You need to use setMockClock() first.");
mockTime = new Date(mockTime.getTime() + millis); mockTime = new Date(mockTime.getTime() + millis);
return mockTime; return mockTime;
} }
/** /**
* Sets the mock clock to the given time (in seconds) * Sets the mock clock to the current time.
*/
public static void setMockClock() {
mockTime = new Date();
}
/**
* Sets the mock clock to the given time (in seconds).
*/ */
public static void setMockClock(long mockClock) { public static void setMockClock(long mockClock) {
mockTime = new Date(mockClock * 1000); mockTime = new Date(mockClock * 1000);

View File

@ -52,6 +52,7 @@ public class ChainSplitTest {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
BriefLogFormatter.init(); BriefLogFormatter.init();
Utils.setMockClock(); // Use mock clock
Wallet.SendRequest.DEFAULT_FEE_PER_KB = BigInteger.ZERO; Wallet.SendRequest.DEFAULT_FEE_PER_KB = BigInteger.ZERO;
unitTestParams = UnitTestParams.get(); unitTestParams = UnitTestParams.get();
wallet = new Wallet(unitTestParams); wallet = new Wallet(unitTestParams);

View File

@ -52,6 +52,7 @@ public class TransactionBroadcastTest extends TestWithPeerGroup {
@Override @Override
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
Utils.setMockClock(); // Use mock clock
super.setUp(new MemoryBlockStore(UnitTestParams.get())); super.setUp(new MemoryBlockStore(UnitTestParams.get()));
peerGroup.addWallet(wallet); peerGroup.addWallet(wallet);
// Fix the random permutation that TransactionBroadcast uses to shuffle the peers. // Fix the random permutation that TransactionBroadcast uses to shuffle the peers.

View File

@ -63,6 +63,7 @@ public class ChannelConnectionTest extends TestWithWallet {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
Utils.setMockClock(); // Use mock clock
sendMoneyToWallet(Utils.COIN, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(Utils.COIN, AbstractBlockChain.NewBlockType.BEST_CHAIN);
sendMoneyToWallet(Utils.COIN, AbstractBlockChain.NewBlockType.BEST_CHAIN); sendMoneyToWallet(Utils.COIN, AbstractBlockChain.NewBlockType.BEST_CHAIN);
wallet.addExtension(new StoredPaymentChannelClientStates(wallet, failBroadcaster)); wallet.addExtension(new StoredPaymentChannelClientStates(wallet, failBroadcaster));

View File

@ -114,7 +114,7 @@ public class PaymentChannelStateTest extends TestWithWallet {
public void basic() throws Exception { public void basic() throws Exception {
// Check it all works when things are normal (no attacks, no problems). // Check it all works when things are normal (no attacks, no problems).
Utils.rollMockClock(0); // Use mock clock Utils.setMockClock(); // Use mock clock
final long EXPIRE_TIME = Utils.currentTimeMillis()/1000 + 60*60*24; final long EXPIRE_TIME = Utils.currentTimeMillis()/1000 + 60*60*24;
serverState = new PaymentChannelServerState(mockBroadcaster, serverWallet, serverKey, EXPIRE_TIME); serverState = new PaymentChannelServerState(mockBroadcaster, serverWallet, serverKey, EXPIRE_TIME);
@ -228,7 +228,7 @@ public class PaymentChannelStateTest extends TestWithWallet {
StoredPaymentChannelClientStates stateStorage = new StoredPaymentChannelClientStates(wallet, mockBroadcaster); StoredPaymentChannelClientStates stateStorage = new StoredPaymentChannelClientStates(wallet, mockBroadcaster);
wallet.addOrUpdateExtension(stateStorage); wallet.addOrUpdateExtension(stateStorage);
Utils.rollMockClock(0); // Use mock clock Utils.setMockClock(); // Use mock clock
final long EXPIRE_TIME = Utils.currentTimeMillis()/1000 + 60*60*24; final long EXPIRE_TIME = Utils.currentTimeMillis()/1000 + 60*60*24;
serverState = new PaymentChannelServerState(mockBroadcaster, serverWallet, serverKey, EXPIRE_TIME); serverState = new PaymentChannelServerState(mockBroadcaster, serverWallet, serverKey, EXPIRE_TIME);
@ -330,7 +330,7 @@ public class PaymentChannelStateTest extends TestWithWallet {
// We'll broadcast only one tx: multisig contract // We'll broadcast only one tx: multisig contract
Utils.rollMockClock(0); // Use mock clock Utils.setMockClock(); // Use mock clock
final long EXPIRE_TIME = Utils.currentTimeMillis()/1000 + 60*60*24; final long EXPIRE_TIME = Utils.currentTimeMillis()/1000 + 60*60*24;
serverState = new PaymentChannelServerState(mockBroadcaster, serverWallet, serverKey, EXPIRE_TIME); serverState = new PaymentChannelServerState(mockBroadcaster, serverWallet, serverKey, EXPIRE_TIME);
@ -541,7 +541,7 @@ public class PaymentChannelStateTest extends TestWithWallet {
chain.add(makeSolvedTestBlock(blockStore.getChainHead().getHeader(), createFakeTx(params, Utils.CENT, myAddress))); chain.add(makeSolvedTestBlock(blockStore.getChainHead().getHeader(), createFakeTx(params, Utils.CENT, myAddress)));
assertEquals(Utils.CENT, wallet.getBalance()); assertEquals(Utils.CENT, wallet.getBalance());
Utils.rollMockClock(0); // Use mock clock Utils.setMockClock(); // Use mock clock
final long EXPIRE_TIME = Utils.currentTimeMillis()/1000 + 60*60*24; final long EXPIRE_TIME = Utils.currentTimeMillis()/1000 + 60*60*24;
serverState = new PaymentChannelServerState(mockBroadcaster, serverWallet, serverKey, EXPIRE_TIME); serverState = new PaymentChannelServerState(mockBroadcaster, serverWallet, serverKey, EXPIRE_TIME);
@ -644,7 +644,7 @@ public class PaymentChannelStateTest extends TestWithWallet {
public void serverAddsFeeTest() throws Exception { public void serverAddsFeeTest() throws Exception {
// Test that the server properly adds the necessary fee at the end (or just drops the payment if its not worth it) // Test that the server properly adds the necessary fee at the end (or just drops the payment if its not worth it)
Utils.rollMockClock(0); // Use mock clock Utils.setMockClock(); // Use mock clock
final long EXPIRE_TIME = Utils.currentTimeMillis()/1000 + 60*60*24; final long EXPIRE_TIME = Utils.currentTimeMillis()/1000 + 60*60*24;
serverState = new PaymentChannelServerState(mockBroadcaster, serverWallet, serverKey, EXPIRE_TIME); serverState = new PaymentChannelServerState(mockBroadcaster, serverWallet, serverKey, EXPIRE_TIME);
@ -730,7 +730,7 @@ public class PaymentChannelStateTest extends TestWithWallet {
// Tests that if the client double-spends the multisig contract after it is sent, no more payments are accepted // Tests that if the client double-spends the multisig contract after it is sent, no more payments are accepted
// Start with a copy of basic().... // Start with a copy of basic()....
Utils.rollMockClock(0); // Use mock clock Utils.setMockClock(); // Use mock clock
final long EXPIRE_TIME = Utils.currentTimeMillis()/1000 + 60*60*24; final long EXPIRE_TIME = Utils.currentTimeMillis()/1000 + 60*60*24;
serverState = new PaymentChannelServerState(mockBroadcaster, serverWallet, serverKey, EXPIRE_TIME); serverState = new PaymentChannelServerState(mockBroadcaster, serverWallet, serverKey, EXPIRE_TIME);

View File

@ -39,6 +39,7 @@ public class DefaultCoinSelectorTest extends TestWithWallet {
@Override @Override
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
Utils.setMockClock(); // Use mock clock
} }
@After @After