Skip to content

Commit

Permalink
Implement native test for the gRPC / Hibernate ITs
Browse files Browse the repository at this point in the history
Fix #22619
  • Loading branch information
cescoffier committed Nov 21, 2022
1 parent 481d355 commit a1ee776
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 30 deletions.
@@ -0,0 +1,45 @@
package com.example.grpc.hibernate;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;

import com.example.test.MutinyTestGrpc;
import com.example.test.Test;
import com.example.test.TestClient;

import io.quarkus.test.junit.QuarkusIntegrationTest;

@QuarkusIntegrationTest
public class BlockingMutinyIT extends BlockingMutinyTestBase {

@BeforeAll
static void init() {
GrpcIntegrationTestHelper.init();
}

@AfterAll
static void cleanup() {
GrpcIntegrationTestHelper.cleanup();
}

@AfterEach
void close() {
if (client != null) {
client = null;
}
}

/**
* Native tests cannot get the injected client, thus we build the client directly.
*
* @return the test client
*/
@Override
Test getClient() {
if (client == null) {
client = GrpcIntegrationTestHelper.createClient(9000, TestClient.class, MutinyTestGrpc::newMutinyStub);
}
return client;
}
}
Expand Up @@ -24,9 +24,13 @@ public class BlockingMutinyTestBase {
@GrpcClient
Test client;

Test getClient() {
return client;
}

@BeforeEach
void clear() {
client.clear(EMPTY).onFailure().invoke(e -> {
getClient().clear(EMPTY).onFailure().invoke(e -> {
throw new RuntimeException("Failed to clear items", e);
}).await().atMost(Duration.ofSeconds(20));
}
Expand All @@ -39,19 +43,15 @@ void shouldAddItems() {
String text = "text " + i;
expected.add(text);
final int attempt = i;
client.add(TestOuterClass.Item.newBuilder().setText(text).build())
getClient().add(TestOuterClass.Item.newBuilder().setText(text).build())
.onFailure().invoke(e -> {
throw new RuntimeException("Failed to add on attempt " + attempt, e);
})
.await().atMost(Duration.ofSeconds(5));
}

List<String> actual = new ArrayList<>();
Multi<TestOuterClass.Item> all = client.getAll(EMPTY)
.onFailure().invoke(th -> {
System.out.println("Failed to read");
th.printStackTrace();
});
Multi<TestOuterClass.Item> all = getClient().getAll(EMPTY);
all.subscribe().with(item -> actual.add(item.getText()));
await().atMost(Duration.ofSeconds(TIMEOUT / 2))
.until(() -> actual.size() == NO_OF_ELTS);
Expand All @@ -74,17 +74,13 @@ void shouldAddViaBidi() {
}
m.complete();
});
client.bidi(request).subscribe().with(item -> echoed.add(item.getText()));
getClient().bidi(request).subscribe().with(item -> echoed.add(item.getText()));

await().atMost(Duration.ofSeconds(TIMEOUT / 2))
.until(() -> echoed.size() == NO_OF_ELTS);
assertThat(echoed).containsExactlyInAnyOrderElementsOf(expected);

Multi<TestOuterClass.Item> all = client.getAll(EMPTY)
.onFailure().invoke(th -> {
System.out.println("Failed to read");
th.printStackTrace();
});
Multi<TestOuterClass.Item> all = getClient().getAll(EMPTY);
all.subscribe().with(item -> actual.add(item.getText()));
await().atMost(Duration.ofSeconds(TIMEOUT / 2))
.until(() -> actual.size() == NO_OF_ELTS);
Expand Down
@@ -0,0 +1,45 @@
package com.example.grpc.hibernate;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;

import com.example.test.MutinyTestRawGrpc;
import com.example.test.TestRaw;
import com.example.test.TestRawClient;

import io.quarkus.test.junit.QuarkusIntegrationTest;

@QuarkusIntegrationTest
public class BlockingRawIT extends BlockingRawTestBase {

@BeforeAll
static void init() {
GrpcIntegrationTestHelper.init();
}

@AfterAll
static void cleanup() {
GrpcIntegrationTestHelper.cleanup();
}

@AfterEach
void close() {
if (client != null) {
client = null;
}
}

/**
* Native tests cannot get the injected client, thus we build the client directly.
*
* @return the test client
*/
@Override
TestRaw getClient() {
if (client == null) {
client = GrpcIntegrationTestHelper.createClient(9000, TestRawClient.class, MutinyTestRawGrpc::newMutinyStub);
}
return client;
}
}
Expand Up @@ -27,9 +27,13 @@ public class BlockingRawTestBase {
@GrpcClient
TestRaw client;

TestRaw getClient() {
return client;
}

@BeforeEach
void clear() {
client.clear(EMPTY).onFailure().invoke(e -> {
getClient().clear(EMPTY).onFailure().invoke(e -> {
throw new RuntimeException("Failed to clear items", e);
}).await().atMost(Duration.ofSeconds(20));
}
Expand All @@ -43,19 +47,15 @@ void shouldAdd() {
String text = "text " + i;
expected.add(text);
final int attempt = i;
client.add(TestOuterClass.Item.newBuilder().setText(text).build())
getClient().add(TestOuterClass.Item.newBuilder().setText(text).build())
.onFailure().invoke(e -> {
throw new RuntimeException("Failed to add on attempt " + attempt, e);
})
.await().atMost(Duration.ofSeconds(5));
}

List<String> actual = new CopyOnWriteArrayList<>();
Multi<TestOuterClass.Item> all = client.getAll(EMPTY)
.onFailure().invoke(th -> {
System.out.println("Failed to read");
th.printStackTrace();
});
Multi<TestOuterClass.Item> all = getClient().getAll(EMPTY);
all.subscribe().with(item -> actual.add(item.getText()));
await().atMost(Duration.ofSeconds(TIMEOUT / 2))
.until(() -> actual.size() == NO_OF_ELTS);
Expand All @@ -78,23 +78,16 @@ void shouldAddViaBidi() {
}
m.complete();
});
client.bidi(request).subscribe().with(item -> echoed.add(item.getText()));
getClient().bidi(request).subscribe().with(item -> echoed.add(item.getText()));

await().atMost(Duration.ofSeconds(TIMEOUT / 2))
.until(() -> echoed.size() == NO_OF_ELTS);
assertThat(echoed).containsExactlyInAnyOrderElementsOf(expected);

Multi<TestOuterClass.Item> all = client.getAll(EMPTY)
.onFailure().invoke(th -> {
System.out.println("Failed to read");
th.printStackTrace();
});
Multi<TestOuterClass.Item> all = getClient().getAll(EMPTY);
all.subscribe().with(item -> actual.add(item.getText()));
await().atMost(Duration.ofSeconds(TIMEOUT / 2))
.until(() -> {
System.out.println("no of elements: " + actual.size());
return actual.size() == NO_OF_ELTS;
});
.until(() -> actual.size() == NO_OF_ELTS);
assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
}
}
@@ -0,0 +1,39 @@
package com.example.grpc.hibernate;

import java.lang.reflect.Constructor;
import java.util.function.Function;

import io.grpc.stub.AbstractStub;
import io.vertx.core.Vertx;
import io.vertx.core.net.SocketAddress;
import io.vertx.grpc.client.GrpcClient;
import io.vertx.grpc.client.GrpcClientChannel;

public class GrpcIntegrationTestHelper {

private static Vertx vertx;
private static GrpcClient grpcClient;

static void init() {
vertx = Vertx.vertx();
grpcClient = GrpcClient.client(vertx);
}

static void cleanup() {
grpcClient.close().toCompletionStage().toCompletableFuture().join();
vertx.close().toCompletionStage().toCompletableFuture().join();
}

static <T> T createClient(int port, Class<T> clazz, Function<GrpcClientChannel, AbstractStub<?>> function) {
try {
GrpcClientChannel channel = new GrpcClientChannel(grpcClient, SocketAddress.inetSocketAddress(port, "localhost"));
var stub = function.apply(channel);
Constructor<T> constructor = clazz.getDeclaredConstructor(stub.getClass());
constructor.setAccessible(true);
return constructor.newInstance(stub);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

}
@@ -0,0 +1,48 @@
package com.example.grpc.hibernate;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;

import com.example.test.MutinyTestGrpc;
import com.example.test.Test;
import com.example.test.TestClient;

import io.quarkus.grpc.test.utils.VertxGRPCTestProfile;
import io.quarkus.test.junit.QuarkusIntegrationTest;
import io.quarkus.test.junit.TestProfile;

@QuarkusIntegrationTest
@TestProfile(VertxGRPCTestProfile.class)
public class VertxBlockingMutinyIT extends BlockingMutinyTestBase {

@BeforeAll
static void init() {
GrpcIntegrationTestHelper.init();
}

@AfterAll
static void cleanup() {
GrpcIntegrationTestHelper.cleanup();
}

@AfterEach
void close() {
if (client != null) {
client = null;
}
}

/**
* Native tests cannot get the injected client, thus we build the client directly.
*
* @return the test client
*/
@Override
Test getClient() {
if (client == null) {
client = GrpcIntegrationTestHelper.createClient(8081, TestClient.class, MutinyTestGrpc::newMutinyStub);
}
return client;
}
}
@@ -0,0 +1,47 @@
package com.example.grpc.hibernate;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;

import com.example.test.MutinyTestRawGrpc;
import com.example.test.TestRaw;
import com.example.test.TestRawClient;

import io.quarkus.grpc.test.utils.VertxGRPCTestProfile;
import io.quarkus.test.junit.QuarkusIntegrationTest;
import io.quarkus.test.junit.TestProfile;

@QuarkusIntegrationTest
@TestProfile(VertxGRPCTestProfile.class)
public class VertxBlockingRawIT extends BlockingRawTestBase {
@BeforeAll
static void init() {
GrpcIntegrationTestHelper.init();
}

@AfterAll
static void cleanup() {
GrpcIntegrationTestHelper.cleanup();
}

@AfterEach
void close() {
if (client != null) {
client = null;
}
}

/**
* Native tests cannot get the injected client, thus we build the client directly.
*
* @return the test client
*/
@Override
TestRaw getClient() {
if (client == null) {
client = GrpcIntegrationTestHelper.createClient(8081, TestRawClient.class, MutinyTestRawGrpc::newMutinyStub);
}
return client;
}
}

0 comments on commit a1ee776

Please sign in to comment.