Skip to content

Commit

Permalink
WIP: writing tests
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Bair <[email protected]>
  • Loading branch information
rbair23 committed Jun 28, 2024
1 parent e338216 commit f470f8c
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 79 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

23 changes: 0 additions & 23 deletions pbj-core/pbj-grpc-helidon/src/test/java/http/HttpTest.java

This file was deleted.

21 changes: 10 additions & 11 deletions pbj-core/pbj-grpc-helidon/src/test/java/pbj/ConsensusService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.hedera.hapi.node.transaction.TransactionResponse;
import com.hedera.pbj.runtime.ServiceInterface;
import com.hedera.pbj.runtime.io.buffer.Bytes;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.List;
import java.util.concurrent.BlockingQueue;

Expand All @@ -24,14 +25,17 @@ enum ConsensusMethod implements Method {
TransactionResponse submitMessage(Transaction tx);
Response getTopicInfo(Query q);

@NonNull
default String serviceName() {
return "ConsensusService";
}

@NonNull
default String fullName() {
return "proto.ConsensusService";
}

@NonNull
default List<Method> methods() {
return List.of(
ConsensusMethod.createTopic,
Expand All @@ -43,10 +47,10 @@ default List<Method> methods() {

@Override
default void open(
final /*@NonNull*/ RequestOptions options,
final /*@NonNull*/ Method method,
final /*@NonNull*/ BlockingQueue<Bytes> messages,
final /*@NonNull*/ ResponseCallback callback) {
final @NonNull RequestOptions options,
final @NonNull Method method,
final @NonNull BlockingQueue<Bytes> messages,
final @NonNull ResponseCallback callback) {

final var m = (ConsensusMethod) method;
Thread.ofVirtual().start(() -> {
Expand All @@ -55,7 +59,6 @@ default void open(
case ConsensusMethod.createTopic -> {
// Unary method
final var message = messages.take();
callback.start();
final var messageBytes = options.isProtobuf() // What if it isn't JSON or PROTOBUF?
? Transaction.PROTOBUF.parse(message)
: Transaction.JSON.parse(message);
Expand All @@ -67,7 +70,6 @@ default void open(
case ConsensusMethod.updateTopic -> {
// Unary method
final var message = messages.take();
callback.start();
final var messageBytes = options.isProtobuf()
? Transaction.PROTOBUF.parse(message)
: Transaction.JSON.parse(message);
Expand All @@ -79,7 +81,6 @@ default void open(
case ConsensusMethod.deleteTopic -> {
// Unary method
final var message = messages.take();
callback.start();
final var messageBytes = options.isProtobuf()
? Transaction.PROTOBUF.parse(message)
: Transaction.JSON.parse(message);
Expand All @@ -89,9 +90,8 @@ default void open(
callback.close();
}
case ConsensusMethod.submitMessage -> {
// Unary method
// Unary method.
final var message = messages.take();
callback.start();
final var messageBytes = options.isProtobuf()
? Transaction.PROTOBUF.parse(message)
: Transaction.JSON.parse(message);
Expand All @@ -103,7 +103,6 @@ default void open(
case ConsensusMethod.getTopicInfo -> {
// Unary method
final var message = messages.take();
callback.start();
final var messageBytes = options.isProtobuf()
? Query.PROTOBUF.parse(message)
: Query.JSON.parse(message);
Expand All @@ -113,7 +112,7 @@ default void open(
callback.close();
}
}
} catch (Exception e) {
} catch (Throwable e) {
e.printStackTrace();
callback.close();
}
Expand Down
68 changes: 68 additions & 0 deletions pbj-core/pbj-grpc-helidon/src/test/java/pbj/TestService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package pbj;

import com.hedera.pbj.runtime.ServiceInterface;
import com.hedera.pbj.runtime.io.buffer.Bytes;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.BlockingQueue;

public interface TestService extends ServiceInterface {
enum CustomMethod implements Method {
echoUnary,
echoBidi,
echoServerStream,
echoClientStream,
failUnary,
failBidi,
failServerStream,
failClientStream
}

String echo(String message);

@NonNull
default String serviceName() {
return "TestService";
}

@NonNull
default String fullName() {
return "proto.TestService";
}

@NonNull
default List<Method> methods() {
return Arrays.asList(CustomMethod.values());
}

@Override
default void open(
final @NonNull RequestOptions options,
final @NonNull Method method,
final @NonNull BlockingQueue<Bytes> messages,
final @NonNull ResponseCallback callback) {

final var m = (CustomMethod) method;
Thread.ofVirtual().start(() -> {
try {
switch (m) {
case CustomMethod.echoUnary -> {
final var message = messages.take();
final var ct = options.contentType();
if (options.isJson() || options.isProtobuf() || !ct.equals("application/grpc+string")) {
throw new IllegalArgumentException("Only 'string' is allowed");
}

final var response = echo(message.asUtf8String());
callback.send(Bytes.wrap(response));
callback.close();
}
}
} catch (Exception e) {
e.printStackTrace();
callback.close();
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.hedera.pbj.runtime;

import com.hedera.pbj.runtime.io.buffer.Bytes;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.List;
import java.util.concurrent.BlockingQueue;

Expand Down Expand Up @@ -54,8 +55,9 @@ interface Method {
}

interface RequestOptions {
String APPLICATION_GRPC_PROTO = "proto";
String APPLICATION_GRPC_JSON = "json";
String APPLICATION_GRPC = "application/grpc";
String APPLICATION_GRPC_PROTO = "application/grpc+proto";
String APPLICATION_GRPC_JSON = "application/grpc+json";

boolean isProtobuf();
boolean isJson();
Expand All @@ -64,26 +66,19 @@ interface RequestOptions {

/**
* Through this interface the {@link ServiceInterface} implementation will send responses back to the client.
* The {@link #start()} method is called before any responses are sent, and the {@link #close()} method
* is called after all responses have been sent.
* The {@link #close()} method is called after all responses have been sent.
*
* <p>It is not common for an application to implement or use this interface. It is typically implemented by
* a webserver to integrate PBJ into that server.
*/
interface ResponseCallback {
/**
* Called by the {@link ServiceInterface} implementation to before any responses have been sent to the client.
* This must be called before {@link #send(Bytes)} is called.
*/
void start();

/**
* Called to send a single response message to the client. For unary methods, this will be called once. For
* server-side streaming or bidi-streaming, this may be called many times.
*
* @param response A response message to send to the client.
*/
void send(/*@NonNull*/ Bytes response);
void send(@NonNull Bytes response);

/**
* Called to close the connection with the client, signaling that no more responses will be sent.
Expand All @@ -92,11 +87,11 @@ interface ResponseCallback {
}

/** Gets the simple name of the service. For example, "HelloService". */
/*@NonNull*/ String serviceName();
@NonNull String serviceName();
/** Gets the full name of the service. For example, "example.HelloService". */
/*@NonNull*/ String fullName();
@NonNull String fullName();
/** Gets a list of each method in the service. This list may be empty but should never be null. */
/*@NonNull*/ List<Method> methods();
@NonNull List<Method> methods();

/**
* Called by the webserver to open a new connection between the client and the service. This method may be called
Expand All @@ -110,8 +105,8 @@ interface ResponseCallback {
* @param callback A callback to send responses back to the client.
*/
void open(
/*@NonNull*/ RequestOptions opts,
/*@NonNull*/ Method method,
/*@NonNull*/ BlockingQueue<Bytes> messages,
/*@NonNull*/ ResponseCallback callback);
@NonNull RequestOptions opts,
@NonNull Method method,
@NonNull BlockingQueue<Bytes> messages,
@NonNull ResponseCallback callback);
}

0 comments on commit f470f8c

Please sign in to comment.