Skip to content

Commit

Permalink
Make a couple of tests use the mock clock to improve test reproducibi…
Browse files Browse the repository at this point in the history
…lity.
  • Loading branch information
Andreas Schildbach committed Dec 8, 2019
1 parent 40ac403 commit a308cd7
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public Date getDate() {
* This should always be called before attempting to call sendPayment.
*/
public boolean isExpired() {
return paymentDetails.hasExpires() && System.currentTimeMillis() / 1000L > paymentDetails.getExpires();
return paymentDetails.hasExpires() && Utils.currentTimeSeconds() > paymentDetails.getExpires();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion core/src/test/java/org/bitcoinj/core/BlockChainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ private void resetBlockStore() {
@Before
public void setUp() throws Exception {
BriefLogFormatter.initVerbose();
Utils.setMockClock(); // Use mock clock
Context.propagate(new Context(TESTNET, 100, Coin.ZERO, false));
testNetChain = new BlockChain(TESTNET, new Wallet(TESTNET), new MemoryBlockStore(TESTNET));
Context.propagate(new Context(UNITTEST, 100, Coin.ZERO, false));
Expand Down Expand Up @@ -156,7 +157,7 @@ public void difficultyTransitions() throws Exception {
// Add a bunch of blocks in a loop until we reach a difficulty transition point. The unit test params have an
// artificially shortened period.
Block prev = UNITTEST.getGenesisBlock();
Utils.setMockClock(System.currentTimeMillis()/1000);
Utils.setMockClock(Utils.currentTimeSeconds());
for (int height = 0; height < UNITTEST.getInterval() - 1; height++) {
Block newBlock = prev.createNextBlock(coinbaseTo, 1, Utils.currentTimeSeconds(), height);
assertTrue(chain.add(newBlock));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ private void resetBlockStore() {

@Before
public void setUp() throws Exception {
Utils.setMockClock(); // Use mock clock
Context context = new Context(UNITTEST);
Wallet wallet = new Wallet(context);
wallet.freshReceiveKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.bitcoinj.core.LegacyAddress;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.Transaction;
import org.bitcoinj.core.Utils;
import org.bitcoinj.crypto.X509Utils;
import org.bitcoinj.params.TestNet3Params;
import org.bitcoinj.params.UnitTestParams;
Expand Down Expand Up @@ -99,7 +100,7 @@ public void testSignAndVerifyExpired() throws Exception {

private Protos.PaymentRequest minimalPaymentRequest() {
Protos.PaymentDetails.Builder paymentDetails = Protos.PaymentDetails.newBuilder();
paymentDetails.setTime(System.currentTimeMillis());
paymentDetails.setTime(Utils.currentTimeSeconds());
Protos.PaymentRequest.Builder paymentRequest = Protos.PaymentRequest.newBuilder();
paymentRequest.setSerializedPaymentDetails(paymentDetails.build().toByteString());
return paymentRequest.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.bitcoinj.core.Transaction;
import org.bitcoinj.core.TransactionInput;
import org.bitcoinj.core.TransactionOutput;
import org.bitcoinj.core.Utils;
import org.bitcoinj.crypto.TrustStoreLoader;
import org.bitcoinj.params.MainNetParams;
import org.bitcoinj.params.TestNet3Params;
Expand All @@ -52,7 +53,7 @@ public class PaymentSessionTest {
private static final String paymentRequestMemo = "send coinz noa plz kthx";
private static final String paymentMemo = "take ze coinz";
private static final ByteString merchantData = ByteString.copyFromUtf8("merchant data");
private static final long time = System.currentTimeMillis() / 1000L;
private static final long time = Utils.currentTimeSeconds();
private ECKey serverKey;
private Transaction tx;
private TransactionOutput outputToMe;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class ExponentialBackoffTest {

@Before
public void setUp() {
Utils.setMockClock(System.currentTimeMillis() / 1000);
Utils.setMockClock(Utils.currentTimeSeconds());
params = new ExponentialBackoff.Params();
backoff = new ExponentialBackoff(params);
}
Expand Down

0 comments on commit a308cd7

Please sign in to comment.