Skip to content

Instantly share code, notes, and snippets.

@batiati
Created October 31, 2022 11:54
Show Gist options
  • Save batiati/9d4cbabfe76107260d410cafd546d27c to your computer and use it in GitHub Desktop.
Save batiati/9d4cbabfe76107260d410cafd546d27c to your computer and use it in GitHub Desktop.
package com.tigerbeetle;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
/**
* Integration tests using a TigerBeetle instance.
*/
public class Tests {
/**
* This test asserts that async tasks will respect client's maxConcurrency.
*/
@Test
public void testAsyncTasks() throws Throwable {
try (var server = new Server()) {
// Defining the maxConcurrency greater than tasks_qty
// The goal here is to allow to all requests being submitted at once simultaneously
final int tasks_qty = 64;
final int max_concurrency = tasks_qty;
try (var client = new Client(0, new String[] {Server.TB_PORT}, max_concurrency)) {
var errors = client.createAccounts(accounts);
assertTrue(errors.getLength() == 0);
final var tasks = new CompletableFuture[tasks_qty];
for (int i = 0; i < tasks_qty; i++) {
final var transfers = new TransferBatch(1);
transfers.add();
transfers.setId(UInt128.asBytes(UUID.randomUUID()));
transfers.setCreditAccountId(account1Id);
transfers.setDebitAccountId(account2Id);
transfers.setLedger(720);
transfers.setCode((short) 1);
transfers.setAmount(100);
// Starting async batch
tasks[i] = client.createTransfersAsync(transfers);
}
// Wait for all tasks, it hangs here.
for (int i = 0; i < tasks_qty; i++) {
@SuppressWarnings("unchecked")
final var future = (CompletableFuture<CreateTransferResultBatch>) tasks[i];
final var result = future.get();
assertEquals(0, result.getLength());
}
} catch (Throwable any) {
throw any;
}
} catch (Throwable any) {
throw any;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment