Skip to content

Commit

Permalink
chore: Make integration tests tests run concurrently (#2069)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Ivanov <[email protected]>
  • Loading branch information
0xivanov authored Nov 13, 2024
1 parent 278a2e9 commit f451eee
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ tasks.withType<Javadoc>().configureEach {
}
}

tasks.withType<Test>().configureEach {
failFast = true
maxParallelForks = (Runtime.getRuntime().availableProcessors() / 2).coerceAtLeast(1)
}

tasks.jacocoTestReport {
dependsOn(tasks.withType<Test>())
// make sure to use any/all test coverage data for the report and run all tests before this report is made
executionData.from(
tasks.test.map { it.extensions.getByType<JacocoTaskExtension>().destinationFile!! },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.time.Duration;
import java.time.Instant;
import java.util.Objects;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicLong;
Expand Down Expand Up @@ -68,6 +69,7 @@ public final class TransactionId implements Comparable<TransactionId> {

private static final long NANOSECONDS_TO_REMOVE = 10000000000L;

private static final Random randomNanosecs = new Random();
private static final AtomicLong monotonicTime = new AtomicLong();


Expand Down Expand Up @@ -115,6 +117,7 @@ public static TransactionId generate(AccountId accountId) {
// between the client and the receiving node and prevented spurious INVALID_TRANSACTION_START.
currentTime = System.currentTimeMillis() * NANOSECONDS_PER_MILLISECOND - NANOSECONDS_TO_REMOVE;


// Get the last recorded timestamp.
lastTime = monotonicTime.get();

Expand All @@ -125,7 +128,7 @@ public static TransactionId generate(AccountId accountId) {
}
} while (!monotonicTime.compareAndSet(lastTime, currentTime));

return new TransactionId(accountId, Instant.ofEpochSecond(0, currentTime));
return new TransactionId(accountId, Instant.ofEpochSecond(0, currentTime + randomNanosecs.nextLong(1_000)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.annotation.Nullable;
import org.junit.jupiter.api.Assumptions;

Expand All @@ -48,6 +50,7 @@ public class IntegrationTestEnv implements AutoCloseable {
public PublicKey operatorKey;
public AccountId operatorId;
public boolean isLocalNode = false;
private static ExecutorService clientExecutor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

public IntegrationTestEnv() throws Exception {
this(0);
Expand Down Expand Up @@ -104,7 +107,7 @@ private static Client createTestEnvClient() throws Exception {
network.put(LOCAL_CONSENSUS_NODE_ENDPOINT, LOCAL_CONSENSUS_NODE_ACCOUNT_ID);

return Client
.forNetwork(network)
.forNetwork(network, clientExecutor)
.setMirrorNetwork(List.of(LOCAL_MIRROR_NODE_GRPC_ENDPOINT));
} else if (!System.getProperty("CONFIG_FILE").equals("")) {
try {
Expand Down Expand Up @@ -152,19 +155,23 @@ public void assumeNotLocalNode() throws Exception {
@Override
public void close() throws Exception {
if (!operatorId.equals(originalClient.getOperatorAccountId())) {
var hbarsBalance = new AccountBalanceQuery()
.setAccountId(operatorId)
.execute(originalClient)
.hbars;
new TransferTransaction()
.addHbarTransfer(operatorId, hbarsBalance.negated())
.addHbarTransfer(Objects.requireNonNull(originalClient.getOperatorAccountId()), hbarsBalance)
.freezeWith(originalClient)
.signWithOperator(client)
.execute(originalClient);
client.close();
try {
var hbarsBalance = new AccountBalanceQuery()
.setAccountId(operatorId)
.execute(originalClient)
.hbars;
new TransferTransaction()
.addHbarTransfer(operatorId, hbarsBalance.negated())
.addHbarTransfer(Objects.requireNonNull(originalClient.getOperatorAccountId()), hbarsBalance)
.freezeWith(originalClient)
.signWithOperator(client)
.execute(originalClient)
.getReceipt(originalClient);

} catch (Exception e) {
client.close();
}
}

originalClient.close();
}

Expand Down

0 comments on commit f451eee

Please sign in to comment.