Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tidy up apitest #5024

Merged
merged 12 commits into from
Jan 1, 2021
15 changes: 4 additions & 11 deletions apitest/src/test/java/bisq/apitest/ApiTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import java.net.InetAddress;

import java.io.File;
import java.io.IOException;

import java.util.HashMap;
Expand Down Expand Up @@ -73,16 +72,6 @@ public class ApiTestCase {
// gRPC service stubs are used by method & scenario tests, but not e2e tests.
private static final Map<BisqAppConfig, GrpcStubs> grpcStubsCache = new HashMap<>();

public static void setUpScaffold(File callRateMeteringConfigFile,
Enum<?>... supportingApps)
throws InterruptedException, ExecutionException, IOException {
scaffold = new Scaffold(stream(supportingApps).map(Enum::name)
.collect(Collectors.joining(",")))
.setUp();
config = scaffold.config;
bitcoinCli = new BitcoinCliHelper((config));
}

public static void setUpScaffold(Enum<?>... supportingApps)
throws InterruptedException, ExecutionException, IOException {
scaffold = new Scaffold(stream(supportingApps).map(Enum::name)
Expand All @@ -107,6 +96,10 @@ public static void tearDownScaffold() {
scaffold.tearDown();
}

protected static String getEnumArrayAsString(Enum<?>[] supportingApps) {
return stream(supportingApps).map(Enum::name).collect(Collectors.joining(","));
}

protected static GrpcStubs grpcStubs(BisqAppConfig bisqAppConfig) {
if (grpcStubsCache.containsKey(bisqAppConfig)) {
return grpcStubsCache.get(bisqAppConfig);
Expand Down
2 changes: 1 addition & 1 deletion apitest/src/test/java/bisq/apitest/method/MethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static void startSupportingApps(boolean registerDisputeAgents,
}
}

private static void doPostStartup(boolean registerDisputeAgents,
protected static void doPostStartup(boolean registerDisputeAgents,
boolean generateBtcBlock,
Enum<?>... supportingApps) {
if (registerDisputeAgents) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import static java.lang.String.format;
import static java.lang.System.getProperty;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;



Expand Down Expand Up @@ -176,7 +176,6 @@ private File fillPaymentAccountForm() {
tmpJsonForm = File.createTempFile("temp_acct_form_",
".json",
Paths.get(getProperty("java.io.tmpdir")).toFile());
tmpJsonForm.deleteOnExit();
JsonWriter writer = new JsonWriter(new OutputStreamWriter(new FileOutputStream(tmpJsonForm), UTF_8));
writer.beginObject();

Expand All @@ -199,6 +198,7 @@ private File fillPaymentAccountForm() {
log.error("", ex);
fail(format("Could not write json file from form entries %s", COMPLETED_FORM_MAP));
}
tmpJsonForm.deleteOnExit();
return tmpJsonForm;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void testTakeAlicesBuyOffer(final TestInfo testInfo) {
// Cache the trade id for the other tests.
tradeId = trade.getTradeId();

genBtcBlocksThenWait(1, 2250);
genBtcBlocksThenWait(1, 1000);
assertEquals(0, getOpenOffersCount(aliceStubs, "buy", "usd"));

trade = getTrade(bobdaemon, trade.getTradeId());
Expand All @@ -89,7 +89,7 @@ public void testTakeAlicesBuyOffer(final TestInfo testInfo) {
verifyExpectedProtocolStatus(trade);
logTrade(log, testInfo, "Bob's view after taking offer and sending deposit", trade);

genBtcBlocksThenWait(1, 2250);
genBtcBlocksThenWait(1, 1000);
trade = getTrade(bobdaemon, trade.getTradeId());
EXPECTED_PROTOCOL_STATUS.setState(DEPOSIT_CONFIRMED_IN_BLOCK_CHAIN)
.setPhase(DEPOSIT_CONFIRMED)
Expand Down Expand Up @@ -142,22 +142,22 @@ public void testBobsConfirmPaymentReceived(final TestInfo testInfo) {
@Test
@Order(4)
public void testAlicesKeepFunds(final TestInfo testInfo) {
genBtcBlocksThenWait(1, 2250);
genBtcBlocksThenWait(1, 1000);

var trade = getTrade(alicedaemon, tradeId);
logTrade(log, testInfo, "Alice's view before keeping funds", trade);

keepFunds(alicedaemon, tradeId);

genBtcBlocksThenWait(1, 2250);
genBtcBlocksThenWait(1, 1000);

trade = getTrade(alicedaemon, tradeId);
EXPECTED_PROTOCOL_STATUS.setState(BUYER_RECEIVED_PAYOUT_TX_PUBLISHED_MSG)
.setPhase(PAYOUT_PUBLISHED);
verifyExpectedProtocolStatus(trade);
logTrade(log, testInfo, "Alice's view after keeping funds", trade);
BtcBalanceInfo currentBalance = getBtcBalances(bobdaemon);
log.info("{} Alice's current available balance: {} BTC",
log.debug("{} Alice's current available balance: {} BTC",
testName(testInfo),
formatSatoshis(currentBalance.getAvailableBalance()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void testTakeAlicesSellOffer(final TestInfo testInfo) {

logTrade(log, testInfo, "Bob's view after taking offer and sending deposit", trade);

genBtcBlocksThenWait(1, 2250);
genBtcBlocksThenWait(1, 1000);
trade = getTrade(bobdaemon, trade.getTradeId());
EXPECTED_PROTOCOL_STATUS.setState(DEPOSIT_CONFIRMED_IN_BLOCK_CHAIN)
.setPhase(DEPOSIT_CONFIRMED)
Expand Down Expand Up @@ -143,15 +143,15 @@ public void testAlicesConfirmPaymentReceived(final TestInfo testInfo) {
@Test
@Order(4)
public void testBobsBtcWithdrawalToExternalAddress(final TestInfo testInfo) {
genBtcBlocksThenWait(1, 2250);
genBtcBlocksThenWait(1, 1000);

var trade = getTrade(bobdaemon, tradeId);
logTrade(log, testInfo, "Bob's view before withdrawing funds to external wallet", trade);

String toAddress = bitcoinCli.getNewBtcAddress();
withdrawFunds(bobdaemon, tradeId, toAddress, WITHDRAWAL_TX_MEMO);

genBtcBlocksThenWait(1, 2250);
genBtcBlocksThenWait(1, 1000);

trade = getTrade(bobdaemon, tradeId);
EXPECTED_PROTOCOL_STATUS.setState(WITHDRAW_COMPLETED)
Expand Down
66 changes: 13 additions & 53 deletions apitest/src/test/java/bisq/apitest/method/wallet/BsqWalletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
import static bisq.apitest.config.BisqAppConfig.arbdaemon;
import static bisq.apitest.config.BisqAppConfig.bobdaemon;
import static bisq.apitest.config.BisqAppConfig.seednode;
import static bisq.apitest.method.wallet.WalletTestUtil.ALICES_INITIAL_BSQ_BALANCES;
import static bisq.apitest.method.wallet.WalletTestUtil.BOBS_INITIAL_BSQ_BALANCES;
import static bisq.apitest.method.wallet.WalletTestUtil.bsqBalanceModel;
import static bisq.apitest.method.wallet.WalletTestUtil.verifyBsqBalances;
import static bisq.cli.TableFormat.formatBsqBalanceInfoTbl;
import static org.bitcoinj.core.NetworkParameters.PAYMENT_PROTOCOL_ID_MAINNET;
import static org.bitcoinj.core.NetworkParameters.PAYMENT_PROTOCOL_ID_REGTEST;
import static org.bitcoinj.core.NetworkParameters.PAYMENT_PROTOCOL_ID_TESTNET;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -40,24 +43,6 @@
@TestMethodOrder(OrderAnnotation.class)
public class BsqWalletTest extends MethodTest {

// Alice's regtest BSQ wallet is initialized with 1,000,000 BSQ.
private static final bisq.core.api.model.BsqBalanceInfo ALICES_INITIAL_BSQ_BALANCES =
expectedBsqBalanceModel(100000000,
0,
0,
0,
0,
0);

// Bob's regtest BSQ wallet is initialized with 1,500,000 BSQ.
private static final bisq.core.api.model.BsqBalanceInfo BOBS_INITIAL_BSQ_BALANCES =
expectedBsqBalanceModel(150000000,
0,
0,
0,
0,
0);

private static final String SEND_BSQ_AMOUNT = "25000.50";

@BeforeAll
Expand Down Expand Up @@ -112,23 +97,23 @@ public void testSendBsqAndCheckBalancesBeforeGeneratingBtcBlock(final TestInfo t
sleep(2000);

BsqBalanceInfo alicesBsqBalances = getBsqBalances(alicedaemon);
BsqBalanceInfo bobsBsqBalances = waitForNonZeroUnverifiedBalance(bobdaemon);
BsqBalanceInfo bobsBsqBalances = waitForNonZeroBsqUnverifiedBalance(bobdaemon);

log.debug("BSQ Balances Before BTC Block Gen...");
printBobAndAliceBsqBalances(testInfo,
bobsBsqBalances,
alicesBsqBalances,
alicedaemon);

verifyBsqBalances(expectedBsqBalanceModel(150000000,
verifyBsqBalances(bsqBalanceModel(150000000,
2500050,
0,
0,
0,
0),
bobsBsqBalances);

verifyBsqBalances(expectedBsqBalanceModel(97499950,
verifyBsqBalances(bsqBalanceModel(97499950,
97499950,
97499950,
0,
Expand All @@ -145,23 +130,23 @@ public void testBalancesAfterSendingBsqAndGeneratingBtcBlock(final TestInfo test
genBtcBlocksThenWait(1, 4000);

BsqBalanceInfo alicesBsqBalances = getBsqBalances(alicedaemon);
BsqBalanceInfo bobsBsqBalances = waitForNewAvailableConfirmedBalance(bobdaemon, 150000000);
BsqBalanceInfo bobsBsqBalances = waitForBsqNewAvailableConfirmedBalance(bobdaemon, 150000000);

log.debug("See Available Confirmed BSQ Balances...");
printBobAndAliceBsqBalances(testInfo,
bobsBsqBalances,
alicesBsqBalances,
alicedaemon);

verifyBsqBalances(expectedBsqBalanceModel(152500050,
verifyBsqBalances(bsqBalanceModel(152500050,
0,
0,
0,
0,
0),
bobsBsqBalances);

verifyBsqBalances(expectedBsqBalanceModel(97499950,
verifyBsqBalances(bsqBalanceModel(97499950,
0,
0,
0,
Expand All @@ -175,17 +160,7 @@ public static void tearDown() {
tearDownScaffold();
}

private void verifyBsqBalances(bisq.core.api.model.BsqBalanceInfo expected,
BsqBalanceInfo actual) {
assertEquals(expected.getAvailableConfirmedBalance(), actual.getAvailableConfirmedBalance());
assertEquals(expected.getUnverifiedBalance(), actual.getUnverifiedBalance());
assertEquals(expected.getUnconfirmedChangeBalance(), actual.getUnconfirmedChangeBalance());
assertEquals(expected.getLockedForVotingBalance(), actual.getLockedForVotingBalance());
assertEquals(expected.getLockupBondsBalance(), actual.getLockupBondsBalance());
assertEquals(expected.getUnlockingBondsBalance(), actual.getUnlockingBondsBalance());
}

private BsqBalanceInfo waitForNonZeroUnverifiedBalance(BisqAppConfig daemon) {
private BsqBalanceInfo waitForNonZeroBsqUnverifiedBalance(BisqAppConfig daemon) {
// A BSQ recipient needs to wait for her daemon to detect a new tx.
// Loop here until her unverifiedBalance != 0, or give up after 15 seconds.
// A slow test is preferred over a flaky test.
Expand All @@ -197,8 +172,8 @@ private BsqBalanceInfo waitForNonZeroUnverifiedBalance(BisqAppConfig daemon) {
return bsqBalance;
}

private BsqBalanceInfo waitForNewAvailableConfirmedBalance(BisqAppConfig daemon,
long staleBalance) {
private BsqBalanceInfo waitForBsqNewAvailableConfirmedBalance(BisqAppConfig daemon,
long staleBalance) {
BsqBalanceInfo bsqBalance = getBsqBalances(daemon);
for (int numRequests = 1;
numRequests <= 15 && bsqBalance.getAvailableConfirmedBalance() == staleBalance;
Expand Down Expand Up @@ -226,19 +201,4 @@ private void printBobAndAliceBsqBalances(final TestInfo testInfo,
SEND_BSQ_AMOUNT,
formatBsqBalanceInfoTbl(alicesBsqBalances));
}

@SuppressWarnings("SameParameterValue")
private static bisq.core.api.model.BsqBalanceInfo expectedBsqBalanceModel(long availableConfirmedBalance,
long unverifiedBalance,
long unconfirmedChangeBalance,
long lockedForVotingBalance,
long lockupBondsBalance,
long unlockingBondsBalance) {
return bisq.core.api.model.BsqBalanceInfo.valueOf(availableConfirmedBalance,
unverifiedBalance,
unconfirmedChangeBalance,
lockedForVotingBalance,
lockupBondsBalance,
unlockingBondsBalance);
}
}
36 changes: 11 additions & 25 deletions apitest/src/test/java/bisq/apitest/method/wallet/BtcWalletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import static bisq.apitest.config.BisqAppConfig.alicedaemon;
import static bisq.apitest.config.BisqAppConfig.bobdaemon;
import static bisq.apitest.config.BisqAppConfig.seednode;
import static bisq.apitest.method.wallet.WalletTestUtil.INITIAL_BTC_BALANCES;
import static bisq.apitest.method.wallet.WalletTestUtil.verifyBtcBalances;
import static bisq.cli.TableFormat.formatAddressBalanceTbl;
import static bisq.cli.TableFormat.formatBtcBalanceInfoTbl;
import static java.util.Collections.singletonList;
Expand All @@ -36,14 +38,6 @@ public class BtcWalletTest extends MethodTest {

private static final String TX_MEMO = "tx memo";

// All api tests depend on the DAO / regtest environment, and Bob & Alice's wallets
// are initialized with 10 BTC during the scaffolding setup.
private static final bisq.core.api.model.BtcBalanceInfo INITIAL_BTC_BALANCES =
bisq.core.api.model.BtcBalanceInfo.valueOf(1000000000,
0,
1000000000,
0);

@BeforeAll
public static void setUp() {
startSupportingApps(false,
Expand All @@ -60,10 +54,10 @@ public void testInitialBtcBalances(final TestInfo testInfo) {
// Bob & Alice's regtest Bisq wallets were initialized with 10 BTC.

BtcBalanceInfo alicesBalances = getBtcBalances(alicedaemon);
log.info("{} Alice's BTC Balances:\n{}", testName(testInfo), formatBtcBalanceInfoTbl(alicesBalances));
log.debug("{} Alice's BTC Balances:\n{}", testName(testInfo), formatBtcBalanceInfoTbl(alicesBalances));

BtcBalanceInfo bobsBalances = getBtcBalances(bobdaemon);
log.info("{} Bob's BTC Balances:\n{}", testName(testInfo), formatBtcBalanceInfoTbl(bobsBalances));
log.debug("{} Bob's BTC Balances:\n{}", testName(testInfo), formatBtcBalanceInfoTbl(bobsBalances));

assertEquals(INITIAL_BTC_BALANCES.getAvailableBalance(), alicesBalances.getAvailableBalance());
assertEquals(INITIAL_BTC_BALANCES.getAvailableBalance(), bobsBalances.getAvailableBalance());
Expand All @@ -74,13 +68,13 @@ public void testInitialBtcBalances(final TestInfo testInfo) {
public void testFundAlicesBtcWallet(final TestInfo testInfo) {
String newAddress = getUnusedBtcAddress(alicedaemon);
bitcoinCli.sendToAddress(newAddress, "2.5");
genBtcBlocksThenWait(1, 1500);
genBtcBlocksThenWait(1, 1000);

BtcBalanceInfo btcBalanceInfo = getBtcBalances(alicedaemon);
// New balance is 12.5 BTC
assertEquals(1250000000, btcBalanceInfo.getAvailableBalance());

log.info("{} -> Alice's Funded Address Balance -> \n{}",
log.debug("{} -> Alice's Funded Address Balance -> \n{}",
testName(testInfo),
formatAddressBalanceTbl(singletonList(getAddressBalance(alicedaemon, newAddress))));

Expand All @@ -92,7 +86,7 @@ public void testFundAlicesBtcWallet(final TestInfo testInfo) {
1250000000,
0);
verifyBtcBalances(alicesExpectedBalances, btcBalanceInfo);
log.info("{} -> Alice's BTC Balances After Sending 2.5 BTC -> \n{}",
log.debug("{} -> Alice's BTC Balances After Sending 2.5 BTC -> \n{}",
testName(testInfo),
formatBtcBalanceInfoTbl(btcBalanceInfo));
}
Expand All @@ -112,7 +106,7 @@ public void testAliceSendBTCToBob(TestInfo testInfo) {

// Note that the memo is not set on the tx yet.
assertTrue(txInfo.getMemo().isEmpty());
genBtcBlocksThenWait(1, 3000);
genBtcBlocksThenWait(1, 1000);

// Fetch the tx and check for confirmation and memo.
txInfo = getTransaction(alicedaemon, txInfo.getTxId());
Expand All @@ -134,9 +128,9 @@ public void testAliceSendBTCToBob(TestInfo testInfo) {
log.debug("{} Bob's BTC Balances:\n{}",
testName(testInfo),
formatBtcBalanceInfoTbl(bobsBalances));
// We cannot (?) predict the exact tx size and calculate how much in tx fees were
// deducted from the 5.5 BTC sent to Bob, but we do know Bob should have something
// between 15.49978000 and 15.49978100 BTC.
// The sendbtc tx weight and size randomly varies between two distinct values
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is a 1 byte difference in address length that is allowed.

// (876 wu, 219 bytes, OR 880 wu, 220 bytes) from test run to test run, hence
// the assertion of an available balance range [1549978000, 1549978100].
assertTrue(bobsBalances.getAvailableBalance() >= 1549978000);
assertTrue(bobsBalances.getAvailableBalance() <= 1549978100);
}
Expand All @@ -145,12 +139,4 @@ public void testAliceSendBTCToBob(TestInfo testInfo) {
public static void tearDown() {
tearDownScaffold();
}

private void verifyBtcBalances(bisq.core.api.model.BtcBalanceInfo expected,
BtcBalanceInfo actual) {
assertEquals(expected.getAvailableBalance(), actual.getAvailableBalance());
assertEquals(expected.getReservedBalance(), actual.getReservedBalance());
assertEquals(expected.getTotalAvailableBalance(), actual.getTotalAvailableBalance());
assertEquals(expected.getLockedBalance(), actual.getLockedBalance());
}
}
Loading