Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@
<artifactId>vertx-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-pg-client</artifactId>
Expand All @@ -160,7 +165,6 @@
<artifactId>vertx-jdbc-client</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion api/src/main/java/io/bosonnetwork/PeerInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class PeerInfo {
*/
public static final Object ATTRIBUTE_PEER_ID = new Object();

private final Id publicKey; // Peer ID
private final Id publicKey; // Peer ID
private final byte[] privateKey; // Private key to sign the peer info
private final Id nodeId; // The node that provide the service peer
private final Id origin; // The node that announces the peer
Expand Down
2 changes: 1 addition & 1 deletion api/src/main/java/io/bosonnetwork/database/Pagination.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public String toSql() {
if (offset == 0 && limit == 0)
return ""; // caller may omit OFFSET/LIMIT completely

return " OFFSET " + offset + " LIMIT " + limit;
return " LIMIT " + limit + " OFFSET " + offset;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
public interface ClientAuthenticator {
CompletableFuture<Boolean> authenticateUser(Id userId, byte[] nonce, byte[] signature);

CompletableFuture<Boolean> authenticateDevice(Id userId, Id deviceId, byte[] nonce, byte[] signature);
CompletableFuture<Boolean> authenticateDevice(Id userId, Id deviceId, byte[] nonce, byte[] signature, String address);
}
2 changes: 1 addition & 1 deletion api/src/main/java/io/bosonnetwork/service/Federation.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ default CompletableFuture<? extends FederatedNode> getNode(Id nodeId) {

// public CompletableFuture<List<FederatedService>> getAllServices(Id nodeId);

public CompletableFuture<? extends FederatedService> getService(Id peerId);
public CompletableFuture<? extends FederatedService> getService(Id nodeId, Id peerId);
}
18 changes: 18 additions & 0 deletions api/src/main/java/io/bosonnetwork/vertx/VertxFuture.java
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,15 @@ public boolean isCompletedExceptionally() {
*/
@Override
public T get() throws InterruptedException, ExecutionException {
if (future.isComplete()) {
if (future.succeeded())
return future.result();
else if (future.failed())
throw new ExecutionException(future.cause());
else
throw new InterruptedException("Context closed");
}

if (Context.isOnVertxThread() || Context.isOnEventLoopThread())
throw new IllegalStateException("Cannot not be called on vertx thread or event loop thread");

Expand Down Expand Up @@ -716,6 +725,15 @@ else if (future.failed())
*/
@Override
public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
if (future.isComplete()) {
if (future.succeeded())
return future.result();
else if (future.failed())
throw new ExecutionException(future.cause());
else
throw new InterruptedException("Context closed");
}

if (Context.isOnVertxThread() || Context.isOnEventLoopThread())
throw new IllegalStateException("Cannot not be called on vertx thread or event loop thread");

Expand Down
45 changes: 45 additions & 0 deletions api/src/test/java/io/bosonnetwork/database/PostgresqlServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.bosonnetwork.database;

import org.testcontainers.postgresql.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName;

public class PostgresqlServer {
private PostgreSQLContainer container;

private PostgresqlServer(PostgreSQLContainer container) {
this.container = container;
}

public static PostgresqlServer start(String database, String username, String password) {
DockerImageName image = DockerImageName
.parse("postgres:18-alpine");

PostgreSQLContainer container = new PostgreSQLContainer(image)
.withDatabaseName(database)
.withUsername(username)
.withPassword(password);

return new PostgresqlServer(container).start();
}

private PostgresqlServer start() {
container.start();
return this;
}

public void stop() {
if (container != null) {
container.stop();
container = null;
}
}

public String getDatabaseUrl() {
return "postgresql://" +
container.getUsername() + ":" +
container.getPassword() + "@" +
container.getHost() + ":" +
container.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT) + "/" +
container.getDatabaseName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.jdbcclient.JDBCConnectOptions;
import io.vertx.jdbcclient.JDBCPool;
Expand Down Expand Up @@ -36,34 +37,44 @@ public class VersionedSchemaTests {

private static final List<Arguments> databases = new ArrayList<>();

private static PostgresqlServer pgServer;
private static SqlClient postgres;
private static SqlClient sqlite;

@BeforeAll
static void setup(Vertx vertx, VertxTestContext context) throws Exception {
FileUtils.deleteFile(testDir);
Files.createDirectories(testDir);

var sqliteURL = "jdbc:sqlite:" + testDir.resolve("test.db");
JDBCConnectOptions sqliteConnectOptions = new JDBCConnectOptions()
.setJdbcUrl(sqliteURL);
// Single connection recommended for SQLite
PoolOptions sqlitePoolOptions = new PoolOptions().setMaxSize(1);
SqlClient sqliteClient = JDBCPool.pool(vertx, sqliteConnectOptions, sqlitePoolOptions);
databases.add(Arguments.of("sqlite", sqliteClient));
pgServer = PostgresqlServer.start("migration", "test", "secret");

var postgresURL = "postgresql://jingyu:secret@localhost:5432/test";
var postgresURL = pgServer.getDatabaseUrl();
PgConnectOptions pgConnectOptions = PgConnectOptions.fromUri(postgresURL);
PoolOptions pgPoolOptions = new PoolOptions().setMaxSize(8);
SqlClient pgClient = PgBuilder.pool()
postgres = PgBuilder.pool()
.with(pgPoolOptions)
.connectingTo(pgConnectOptions)
.using(vertx)
.build();
// databases.add(Arguments.of("postgres", pgClient));
databases.add(Arguments.of("postgres", postgres));

var sqliteURL = "jdbc:sqlite:" + testDir.resolve("test.db");
JDBCConnectOptions sqliteConnectOptions = new JDBCConnectOptions()
.setJdbcUrl(sqliteURL);
// Single connection recommended for SQLite
PoolOptions sqlitePoolOptions = new PoolOptions().setMaxSize(1);
sqlite = JDBCPool.pool(vertx, sqliteConnectOptions, sqlitePoolOptions);
databases.add(Arguments.of("sqlite", sqlite));

context.completeNow();
}

@AfterAll
static void teardown() throws Exception {
FileUtils.deleteFile(testRoot);
static void teardown(VertxTestContext context) throws Exception {
Future.all(postgres.close(), sqlite.close()).onComplete(ar -> {
pgServer.stop();
context.completeNow();
});
}

static Stream<Arguments> testDatabaseProvider() {
Expand Down
Loading
Loading