Fixing unit tests

This commit is contained in:
catbref
2019-10-30 13:19:56 +00:00
parent fef16e7620
commit 62e2fd759c
12 changed files with 93 additions and 44 deletions

View File

@@ -18,11 +18,14 @@ public class BTCTests {
BTC btc = BTC.getInstance();
btc.watch(testAddress, testStartTime);
// Disabled for now, pending further work
// btc.watch(testAddress, testStartTime);
Thread.sleep(5000);
// Disabled for now, pending further work
// Thread.sleep(5000);
btc.watch(testAddress, testStartTime);
// Disabled for now, pending further work
// btc.watch(testAddress, testStartTime);
btc.shutdown();
}
@@ -37,10 +40,13 @@ public class BTCTests {
Script redeemScript = ScriptBuilder.createP2SHOutputScript(redeemScriptHash);
redeemScript.setCreationTimeSeconds(testStartTime);
// Disabled for now, pending further work
// btc.watch(redeemScript);
// Disabled for now, pending further work
Thread.sleep(5000);
// Disabled for now, pending further work
// btc.watch(redeemScript);
btc.shutdown();

View File

@@ -7,7 +7,6 @@ import org.junit.Before;
import org.junit.Test;
import org.qora.account.PrivateKeyAccount;
import org.qora.block.Block;
import org.qora.block.BlockMinter;
import org.qora.block.GenesisBlock;
import org.qora.data.at.ATStateData;
import org.qora.data.block.BlockData;
@@ -15,6 +14,7 @@ import org.qora.data.transaction.TransactionData;
import org.qora.repository.DataException;
import org.qora.repository.Repository;
import org.qora.repository.RepositoryManager;
import org.qora.test.common.BlockUtils;
import org.qora.test.common.Common;
import org.qora.test.common.TransactionUtils;
import org.qora.transaction.Transaction;
@@ -106,7 +106,7 @@ public class BlockTests extends Common {
} catch (InterruptedException e) {
}
BlockMinter.mintTestingBlock(repository, signingAccount);
BlockUtils.mintBlock(repository);
BlockData blockData = repository.getBlockRepository().getLastBlock();
Block block = new Block(repository, blockData);

View File

@@ -6,6 +6,8 @@ import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.qora.utils.ExecuteProduceConsume;
@@ -48,16 +50,25 @@ public class EPCTests {
}
private void testEPC(ExecuteProduceConsume testEPC) throws InterruptedException {
final long start = System.currentTimeMillis();
testEPC.start();
// Status reports every second (bar waiting for synchronization)
ScheduledExecutorService statusExecutor = Executors.newSingleThreadScheduledExecutor();
statusExecutor.scheduleAtFixedRate(() -> {
synchronized (testEPC) {
final long seconds = (System.currentTimeMillis() - start) / 1000L;
System.out.println(String.format("After %d second%s, active threads: %d, greatest thread count: %d, tasks produced: %d, tasks consumed: %d",
seconds, (seconds != 1 ? "s" : ""),
testEPC.getActiveThreadCount(), testEPC.getGreatestActiveThreadCount(),
testEPC.getTasksProduced(), testEPC.getTasksConsumed()));
}
}, 1L, 1L, TimeUnit.SECONDS);
// Let it run for a minute
for (int s = 1; s <= 60; ++s) {
Thread.sleep(1000);
System.out.println(String.format("After %d second%s, active threads: %d, greatest thread count: %d, tasks produced: %d, tasks consumed: %d",
s, (s != 1 ? "s" : ""),
testEPC.getActiveThreadCount(), testEPC.getGreatestActiveThreadCount(),
testEPC.getTasksProduced(), testEPC.getTasksConsumed()));
}
Thread.sleep(60_000L);
statusExecutor.shutdownNow();
final long before = System.currentTimeMillis();
testEPC.shutdown(30 * 1000);

View File

@@ -46,6 +46,7 @@ public class SerializationTests extends Common {
case DELEGATION:
case SUPERNODE:
case AIRDROP:
case ENABLE_FORGING:
continue;
default:

View File

@@ -2,14 +2,22 @@ package org.qora.test.common;
import java.math.BigDecimal;
import org.qora.account.PrivateKeyAccount;
import org.qora.block.Block;
import org.qora.block.BlockChain;
import org.qora.block.BlockMinter;
import org.qora.data.block.BlockData;
import org.qora.repository.DataException;
import org.qora.repository.Repository;
public class BlockUtils {
/** Mints a new block using "alice-reward-share" test account. */
public static void mintBlock(Repository repository) throws DataException {
PrivateKeyAccount mintingAccount = Common.getTestAccount(repository, "alice-reward-share");
BlockMinter.mintTestingBlock(repository, mintingAccount);
}
public static BigDecimal getNextBlockReward(Repository repository) throws DataException {
int currentHeight = repository.getBlockRepository().getBlockchainHeight();

View File

@@ -7,7 +7,6 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.qora.account.PrivateKeyAccount;
import org.qora.block.BlockMinter;
import org.qora.data.transaction.TransactionData;
import org.qora.repository.DataException;
import org.qora.repository.Repository;
@@ -36,13 +35,12 @@ public class TransactionUtils {
assertEquals("Transaction invalid", ValidationResult.OK, result);
}
/** Signs transaction using given account and forges a new block, using "alice" self-reward-share key. */
/** Signs transaction using given account and mints a new block.<br> See {@link BlockUtils#mintBlock(Repository)} */
public static void signAndMint(Repository repository, TransactionData transactionData, PrivateKeyAccount signingAccount) throws DataException {
signAsUnconfirmed(repository, transactionData, signingAccount);
// Generate block
PrivateKeyAccount minterAccount = Common.getTestAccount(repository, "alice-reward-share");
BlockMinter.mintTestingBlock(repository, minterAccount);
// Mint block
BlockUtils.mintBlock(repository);
}
public static TransactionData randomTransaction(Repository repository, PrivateKeyAccount account, TransactionType txType, boolean wantValid) throws DataException {

View File

@@ -0,0 +1,20 @@
package org.qora.test.common.transaction;
import java.util.Random;
import org.qora.account.PrivateKeyAccount;
import org.qora.data.transaction.AccountLevelTransactionData;
import org.qora.data.transaction.TransactionData;
import org.qora.repository.DataException;
import org.qora.repository.Repository;
public class AccountLevelTestTransaction extends TestTransaction {
public static TransactionData randomTransaction(Repository repository, PrivateKeyAccount account, boolean wantValid) throws DataException {
Random random = new Random();
final int level = random.nextInt(10);
return new AccountLevelTransactionData(generateBase(account), account.getAddress(), level);
}
}

View File

@@ -5,7 +5,6 @@ import org.junit.Before;
import org.junit.Test;
import org.qora.account.PrivateKeyAccount;
import org.qora.asset.Asset;
import org.qora.block.BlockMinter;
import org.qora.data.transaction.BaseTransactionData;
import org.qora.data.transaction.IssueAssetTransactionData;
import org.qora.data.transaction.PaymentTransactionData;
@@ -125,7 +124,7 @@ public class GroupApprovalTests extends Common {
// Now forge a few blocks so transaction is approved
for (int blockCount = 0; blockCount < minBlockDelay; ++blockCount)
BlockMinter.mintTestingBlock(repository, aliceAccount);
BlockUtils.mintBlock(repository);
// Confirm transaction now approved
approvalStatus = GroupUtils.getApprovalStatus(repository, bobAssetTransaction.getTransactionData().getSignature());
@@ -176,7 +175,6 @@ public class GroupApprovalTests extends Common {
/** Test generic approval. */
public void testApproval() throws DataException {
try (final Repository repository = RepositoryManager.getRepository()) {
PrivateKeyAccount aliceAccount = Common.getTestAccount(repository, "alice");
int groupId = GroupUtils.createGroup(repository, "alice", "test", true, ApprovalThreshold.ONE, minBlockDelay, maxBlockDelay);
PrivateKeyAccount bobAccount = Common.getTestAccount(repository, "bob");
@@ -199,7 +197,7 @@ public class GroupApprovalTests extends Common {
// Now forge a few blocks so transaction is approved
for (int blockCount = 0; blockCount < minBlockDelay; ++blockCount)
BlockMinter.mintTestingBlock(repository, aliceAccount);
BlockUtils.mintBlock(repository);
// Confirm transaction now approved
approvalStatus = GroupUtils.getApprovalStatus(repository, bobAssetTransaction.getTransactionData().getSignature());
@@ -238,7 +236,6 @@ public class GroupApprovalTests extends Common {
/** Test generic rejection. */
public void testRejection() throws DataException {
try (final Repository repository = RepositoryManager.getRepository()) {
PrivateKeyAccount aliceAccount = Common.getTestAccount(repository, "alice");
int groupId = GroupUtils.createGroup(repository, "alice", "test", true, ApprovalThreshold.ONE, minBlockDelay, maxBlockDelay);
PrivateKeyAccount bobAccount = Common.getTestAccount(repository, "bob");
@@ -261,7 +258,7 @@ public class GroupApprovalTests extends Common {
// Now forge a few blocks so transaction is approved
for (int blockCount = 0; blockCount < minBlockDelay; ++blockCount)
BlockMinter.mintTestingBlock(repository, aliceAccount);
BlockUtils.mintBlock(repository);
// Confirm transaction now rejected
approvalStatus = GroupUtils.getApprovalStatus(repository, bobAssetTransaction.getTransactionData().getSignature());
@@ -300,7 +297,6 @@ public class GroupApprovalTests extends Common {
/** Test generic expiry. */
public void testExpiry() throws DataException {
try (final Repository repository = RepositoryManager.getRepository()) {
PrivateKeyAccount aliceAccount = Common.getTestAccount(repository, "alice");
int groupId = GroupUtils.createGroup(repository, "alice", "test", true, ApprovalThreshold.ONE, minBlockDelay, maxBlockDelay);
PrivateKeyAccount bobAccount = Common.getTestAccount(repository, "bob");
@@ -320,7 +316,7 @@ public class GroupApprovalTests extends Common {
// Now forge a few blocks so group-approval for transaction expires
for (int blockCount = 0; blockCount <= maxBlockDelay; ++blockCount)
BlockMinter.mintTestingBlock(repository, aliceAccount);
BlockUtils.mintBlock(repository);
// Confirm transaction now expired
approvalStatus = GroupUtils.getApprovalStatus(repository, bobAssetTransaction.getTransactionData().getSignature());
@@ -380,7 +376,7 @@ public class GroupApprovalTests extends Common {
// Now forge a few blocks so transaction is approved
for (int blockCount = 0; blockCount < minBlockDelay; ++blockCount)
BlockMinter.mintTestingBlock(repository, aliceAccount);
BlockUtils.mintBlock(repository);
// Confirm Bob's transaction now invalid
approvalStatus = GroupUtils.getApprovalStatus(repository, bobAssetTransaction.getTransactionData().getSignature());

View File

@@ -37,11 +37,9 @@ public class RewardTests extends Common {
try (final Repository repository = RepositoryManager.getRepository()) {
Map<String, Map<Long, BigDecimal>> initialBalances = AccountUtils.getBalances(repository, Asset.QORT);
PrivateKeyAccount mintingAccount = Common.getTestAccount(repository, "alice");
BigDecimal blockReward = BlockUtils.getNextBlockReward(repository);
BlockMinter.mintTestingBlock(repository, mintingAccount);
BlockUtils.mintBlock(repository);
BigDecimal expectedBalance = initialBalances.get("alice").get(Asset.QORT).add(blockReward);
AccountUtils.assertBalance(repository, "alice", Asset.QORT, expectedBalance);
@@ -53,8 +51,6 @@ public class RewardTests extends Common {
try (final Repository repository = RepositoryManager.getRepository()) {
Map<String, Map<Long, BigDecimal>> initialBalances = AccountUtils.getBalances(repository, Asset.QORT);
PrivateKeyAccount mintingAccount = Common.getTestAccount(repository, "alice");
List<RewardByHeight> rewards = BlockChain.getInstance().getBlockRewardsByHeight();
int rewardIndex = rewards.size() - 1;
@@ -68,7 +64,7 @@ public class RewardTests extends Common {
rewardInfo = rewards.get(rewardIndex);
}
BlockMinter.mintTestingBlock(repository, mintingAccount);
BlockUtils.mintBlock(repository);
expectedBalance = expectedBalance.add(rewardInfo.reward);
}

View File

@@ -9,7 +9,6 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.qora.account.PrivateKeyAccount;
import org.qora.block.BlockMinter;
import org.qora.data.naming.NameData;
import org.qora.data.transaction.BuyNameTransactionData;
import org.qora.data.transaction.RegisterNameTransactionData;
@@ -29,6 +28,7 @@ public class OrphaningTests extends Common {
private Repository repository;
private PrivateKeyAccount alice;
private PrivateKeyAccount bob;
private String name;
private BigDecimal price;
@@ -51,6 +51,7 @@ public class OrphaningTests extends Common {
alice = null;
bob = null;
repository = null;
Common.orphanCheck();
@@ -74,7 +75,7 @@ public class OrphaningTests extends Common {
assertFalse(repository.getNameRepository().nameExists(name));
// Re-process register-name
BlockMinter.mintTestingBlock(repository, alice);
BlockUtils.mintBlock(repository);
// Check name does exist
assertTrue(repository.getNameRepository().nameExists(name));
@@ -105,7 +106,7 @@ public class OrphaningTests extends Common {
// Not concerned about price
// Re-process sell-name
BlockMinter.mintTestingBlock(repository, alice);
BlockUtils.mintBlock(repository);
// Check name is for sale
nameData = repository.getNameRepository().fromName(name);
@@ -121,10 +122,10 @@ public class OrphaningTests extends Common {
assertNull(nameData);
// Re-process register-name and sell-name
BlockMinter.mintTestingBlock(repository, alice);
BlockUtils.mintBlock(repository);
// Unconfirmed sell-name transaction not included in previous block
// as it isn't valid until name exists thanks to register-name transaction.
BlockMinter.mintTestingBlock(repository, alice);
BlockUtils.mintBlock(repository);
// Check name does exist
assertTrue(repository.getNameRepository().nameExists(name));
@@ -162,7 +163,7 @@ public class OrphaningTests extends Common {
assertEqualBigDecimals("price incorrect", price, nameData.getSalePrice());
// Re-process buy-name
BlockMinter.mintTestingBlock(repository, alice);
BlockUtils.mintBlock(repository);
// Check name is sold
nameData = repository.getNameRepository().fromName(name);
@@ -180,10 +181,10 @@ public class OrphaningTests extends Common {
assertEquals(alice.getAddress(), nameData.getOwner());
// Re-process sell-name and buy-name
BlockMinter.mintTestingBlock(repository, alice);
BlockUtils.mintBlock(repository);
// Unconfirmed buy-name transaction not included in previous block
// as it isn't valid until name is for sale thanks to sell-name transaction.
BlockMinter.mintTestingBlock(repository, alice);
BlockUtils.mintBlock(repository);
// Check name is sold
nameData = repository.getNameRepository().fromName(name);
@@ -218,7 +219,7 @@ public class OrphaningTests extends Common {
// Not concerned about price
// Re-process sell-name
BlockMinter.mintTestingBlock(repository, alice);
BlockUtils.mintBlock(repository);
// Check name is for sale
nameData = repository.getNameRepository().fromName(name);
@@ -236,10 +237,10 @@ public class OrphaningTests extends Common {
assertEquals(alice.getAddress(), nameData.getOwner());
// Re-process buy-name and sell-name
BlockMinter.mintTestingBlock(repository, bob);
BlockUtils.mintBlock(repository);
// Unconfirmed sell-name transaction not included in previous block
// as it isn't valid until name owned by bob thanks to buy-name transaction.
BlockMinter.mintTestingBlock(repository, bob);
BlockUtils.mintBlock(repository);
// Check name does exist
assertTrue(repository.getNameRepository().nameExists(name));