Skip to content

Commit

Permalink
Merge pull request #533 from alvasw/single_electrum_instace_for_all_t…
Browse files Browse the repository at this point in the history
…ests

Use single Electrum instance for all integration tests
  • Loading branch information
alvasw authored Oct 30, 2022
2 parents c1b2a4c + 590d9c5 commit bef491f
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void setUp() {
@Test
void getBalanceTest() {
double balance = electrumDaemon.getBalance();
assertThat(balance).isEqualTo(0);
assertThat(balance).isGreaterThanOrEqualTo(0);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ void setUp() {

@Test
void sendBtcTest() throws InterruptedException {
double balanceBeforeTest = electrumDaemon.getBalance();

var electrumProcessedTxLatch = new CountDownLatch(1);
ElectrumNotifyApi.registerListener((address, status) -> {
if (status != null) {
Expand All @@ -76,7 +78,8 @@ void sendBtcTest() throws InterruptedException {
boolean isSuccess = electrumProcessedTxLatch.await(30, TimeUnit.SECONDS);
assertThat(isSuccess).isTrue();

assertThat(electrumDaemon.getBalance()).isEqualTo(10);
double newBalance = electrumDaemon.getBalance() - balanceBeforeTest;
assertThat(newBalance).isEqualTo(10);

BitcoindWallet minerWallet = remoteBitcoind.getMinerWallet();
double balanceBefore = minerWallet.getBalance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public ElectrumSetupIntegrationTests() throws IOException {
}

@BeforeAll
void beforeAll() throws IOException, InterruptedException {
void beforeAll() throws InterruptedException {
electrumRegtestSetup.start();
electrumDaemon = electrumRegtestSetup.getElectrumDaemon();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import bisq.wallets.electrum.rpc.responses.*;
import bisq.wallets.json_rpc.JsonRpcResponse;
import bisq.wallets.regtest.bitcoind.RemoteBitcoind;
import org.assertj.core.api.Condition;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -84,11 +85,12 @@ void listUnspentGetTxAndHistoryTest() throws InterruptedException {
.map(JsonRpcResponse::getResult)
.flatMap(List::stream)
.toList();
assertThat(unspentResponseEntries).hasSize(1);
assertThat(unspentResponseEntries).hasSizeGreaterThanOrEqualTo(1);

ElectrumListUnspentResponse.Result firstEntry = unspentResponseEntries.get(0);
assertThat(firstEntry.getAddress()).isEqualTo(fundingAddress);
assertThat(firstEntry.getValue()).isEqualTo("10");
Condition<ElectrumListUnspentResponse.Result> hasFundingTxCondition = new Condition<>(s ->
s.getAddress().equals(fundingAddress) && s.getValue().equals("10"), "hasFundingTx");
assertThat(unspentResponseEntries)
.have(hasFundingTxCondition);

// Transaction
String tx = electrumDaemon.getTransaction(fundingTxId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void setUp() {

@Test
void unconfirmedBalanceTest() throws InterruptedException {
double balanceBeforeTest = electrumDaemon.getBalance();
Map<String, CountDownLatch> addressToLatchMap = new ConcurrentHashMap<>();
ElectrumNotifyApi.registerListener((address, status) -> {
CountDownLatch latch = addressToLatchMap.get(address);
Expand All @@ -80,7 +81,8 @@ void unconfirmedBalanceTest() throws InterruptedException {
boolean isSuccess = electrumProcessedTxLatch.await(30, TimeUnit.SECONDS);
assertThat(isSuccess).isTrue();

assertThat(electrumDaemon.getBalance()).isEqualTo(10);
double newBalance = electrumDaemon.getBalance() - balanceBeforeTest;
assertThat(newBalance).isEqualTo(10);

BitcoindWallet minerWallet = remoteBitcoind.getMinerWallet();
String newAddress = minerWallet.getNewAddress(AddressType.BECH32, "");
Expand All @@ -98,8 +100,8 @@ void unconfirmedBalanceTest() throws InterruptedException {
isSuccess = electrumTxLatch.await(30, TimeUnit.SECONDS);
assertThat(isSuccess).isTrue();

double balance = electrumDaemon.getBalance();
assertThat(balance)
newBalance = electrumDaemon.getBalance();
assertThat(newBalance)
.isGreaterThan(4.9) // Because of tx fees
.isLessThan(5);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,43 @@

import java.io.IOException;

public class ElectrumExtension implements BeforeAllCallback, AfterAllCallback, ParameterResolver {
private final ElectrumRegtestSetup electrumRegtestSetup;
import static org.junit.jupiter.api.extension.ExtensionContext.Namespace.GLOBAL;

public ElectrumExtension() throws IOException {
electrumRegtestSetup = !WindowsElectrumRegtestSetup.isExternalBitcoindAndElectrumXEnvironment() ?
new MacLinuxElectrumRegtestSetup(true) : new WindowsElectrumRegtestSetup(true);
public class ElectrumExtension implements BeforeAllCallback, ExtensionContext.Store.CloseableResource, ParameterResolver {
private static boolean isRunning;
private static final ElectrumRegtestSetup electrumRegtestSetup;

static {
try {
electrumRegtestSetup = !WindowsElectrumRegtestSetup.isExternalBitcoindAndElectrumXEnvironment() ?
new MacLinuxElectrumRegtestSetup(true) : new WindowsElectrumRegtestSetup(true);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public ElectrumExtension() {
}

@Override
public void beforeAll(ExtensionContext context) throws Exception {
electrumRegtestSetup.start();
public synchronized void beforeAll(ExtensionContext context) throws Exception {
if (!isRunning) {
electrumRegtestSetup.start();
isRunning = true;

// Register close hook
context.getRoot()
.getStore(GLOBAL)
.put("register_electrum_close_hook", this);
}
}

@Override
public void afterAll(ExtensionContext context) {
electrumRegtestSetup.shutdown();
public synchronized void close() {
if (isRunning) {
electrumRegtestSetup.shutdown();
isRunning = false;
}
}

@Override
Expand Down

0 comments on commit bef491f

Please sign in to comment.