Skip to content

Commit

Permalink
Use Google protobuf for grpc tests, until #260 is resolved.
Browse files Browse the repository at this point in the history
  • Loading branch information
rbair23 committed Jun 28, 2024
1 parent f20331a commit 7182751
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 274 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import javax.security.auth.callback.Callback;

/**
* Implementation of gRPC based on PBJ. This class specifically contains the glue logic for bridging between
Expand Down Expand Up @@ -425,7 +426,11 @@ private synchronized void close() {
deadlineFuture.cancel(false);
// If the deadline was canceled, then we have not yet responded to the client. So the response is OK. On the
// other hand, if th deadline was NOT canceled, then the deadline was exceeded.
responseHeaders.set(deadlineFuture.isCancelled() ? GrpcStatus.OK : GrpcStatus.DEADLINE_EXCEEDED);
// if (!deadlineFuture.isCancelled()) {
responseHeaders.set(GrpcStatus.OK);
// } else {
// responseHeaders.set(GrpcStatus.DEADLINE_EXCEEDED);
// }
final var http2Headers = Http2Headers.create(responseHeaders);
streamWriter.writeHeaders(http2Headers,
streamId,
Expand Down
121 changes: 0 additions & 121 deletions pbj-core/pbj-grpc-helidon/src/test/java/pbj/ConsensusService.java

This file was deleted.

91 changes: 91 additions & 0 deletions pbj-core/pbj-grpc-helidon/src/test/java/pbj/GreeterService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package pbj;

import com.google.protobuf.util.JsonFormat;
import com.hedera.pbj.runtime.ServiceInterface;
import com.hedera.pbj.runtime.io.buffer.Bytes;
import edu.umd.cs.findbugs.annotations.NonNull;
import greeter.HelloReply;
import greeter.HelloReplyOuterClass;
import greeter.HelloRequest;
import greeter.HelloRequestOuterClass;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.BlockingQueue;

/**
* This service doesn't rely on any PBJ objects, because the build right now doesn't have a good way to use the
* compiler. This will be fixed in a future release. So for now, we use Google's generated protobuf objects.
*/
public interface GreeterService extends ServiceInterface {
enum GreeterMethod implements Method {
sayHello,
sayHelloStreamReply,
sayHelloStreamRequest,
sayHelloStreamBidi
}

HelloReply sayHello(HelloRequest request);

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

@NonNull
default String fullName() {
return "greeter.GreeterService";
}

@NonNull
default List<Method> methods() {
return Arrays.asList(GreeterMethod.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 = (GreeterMethod) method;
Thread.ofVirtual().start(() -> {
try {
switch (m) {
case GreeterMethod.sayHello -> {
// Block waiting for the next message
final var message = messages.take();
// Parse the message into a HelloRequest
HelloRequest request;
if (options.isProtobuf()) {
request = HelloRequest.parseFrom(message.toByteArray());
} else if (options.isJson()) {
final var builder = HelloRequest.newBuilder();
JsonFormat.parser().merge(message.asUtf8String(), builder);
request = builder.build();
} else {
request = HelloRequest.newBuilder().setName(message.asUtf8String()).build();
}
// Call the service method
final var reply = sayHello(request);
// Convert the reply back into the appropriate format
Bytes replyBytes;
if (options.isProtobuf()) {
replyBytes = Bytes.wrap(reply.toByteArray());
} else if (options.isJson()) {
replyBytes = Bytes.wrap(JsonFormat.printer().print(reply));
} else {
replyBytes = Bytes.wrap(reply.getMessage().getBytes());
}
// Send back the reply and close the stream (unary).
callback.send(replyBytes);
callback.close();
}
}
} catch (Exception e) {
e.printStackTrace();
callback.close();
}
});
}
}
Loading

0 comments on commit 7182751

Please sign in to comment.