Skip to content
Permalink
Browse files
Add method not found sample
  • Loading branch information
guohao committed Sep 2, 2021
1 parent 2e932ca commit 114a7d42741fb46b3bc6c4850b6eff52db74783a
Showing 6 changed files with 57 additions and 2 deletions.
@@ -36,6 +36,17 @@ public StreamObserver<GreeterRequest> greetStream(StreamObserver<GreeterReply> r
return new GrpcStreamObserverAdapter<>(delegate.greetStream(new StreamObserverAdapter<>(responseObserver)));
}

@Override
public void greetWithAttachment(GreeterRequest request, StreamObserver<GreeterReply> responseObserver) {
try {
final GreeterReply response = delegate.greetWithAttachment(request);
responseObserver.onNext(response);
responseObserver.onCompleted();
} catch (Throwable t) {
responseObserver.onError(t);
}
}

@Override
public void greetServerStream(GreeterRequest request, StreamObserver<GreeterReply> responseObserver) {
delegate.greetServerStream(request, new StreamObserverAdapter<>(responseObserver));
@@ -1,15 +1,37 @@
package org.apache.dubbo.sample.tri;

import io.grpc.ForwardingServerCall;
import io.grpc.Metadata;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.ServerCall;
import io.grpc.ServerCallHandler;
import io.grpc.ServerInterceptor;
import io.grpc.ServerInterceptors;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class GrpcProvider {
public static void main(String[] args) throws IOException, InterruptedException {
final Server server = ServerBuilder.forPort(50051)
.addService(new GrpcPbGreeterImpl(new PbGreeterImpl()))
.addService(ServerInterceptors.intercept(new GrpcPbGreeterImpl(new PbGreeterImpl()), new ServerInterceptor() {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> serverCall,
Metadata metadata, ServerCallHandler<ReqT, RespT> serverCallHandler) {
return serverCallHandler.startCall(new ForwardingServerCall.SimpleForwardingServerCall(serverCall){
@Override
public void sendHeaders(Metadata headers) {
final String key = "user-attachment";
final Metadata.Key<String> metaKey = Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER);
if(metadata.containsKey(metaKey)){
headers.put(metaKey,metadata.get(metaKey));
}
super.sendHeaders(headers);
}
}, metadata);
};
}))
.build();
server.start();
Runtime.getRuntime().addShutdownHook(new Thread() {
@@ -7,6 +7,8 @@ public interface PbGreeter {

GreeterReply greet(GreeterRequest request);

GreeterReply methodNonExist(GreeterRequest request);

GreeterReply greetException(GreeterRequest request);

StreamObserver<GreeterRequest> greetStream(StreamObserver<GreeterReply> replyStream);
@@ -20,6 +20,11 @@ public GreeterReply greet(GreeterRequest request) {
.build();
}

@Override
public GreeterReply methodNonExist(GreeterRequest request) {
throw new RuntimeException("not found");
}

public GreeterReply greetException(GreeterRequest request) {
RpcContext.getServerContext().setAttachment("str", "str")
.setAttachment("integer", 1)
@@ -10,6 +10,7 @@

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;
@@ -87,23 +88,35 @@ public void unaryGreeter() {


@Test(expected = RpcException.class)
public void clientSendLargeSizeHeader() throws InterruptedException {
@Ignore
public void clientSendLargeSizeHeader() {
StringBuilder sb = new StringBuilder("a");
for (int j = 0; j < 15; j++) {
sb.append(sb);
}
sb.setLength(8191);
RpcContext.getClientAttachment().setObjectAttachment("large-size-meta", sb.toString());
delegate.greet(GreeterRequest.newBuilder().setName("meta").build());
RpcContext.getClientAttachment().clearAttachments();
}

@Test
public void attachmentTest() {
final String key = "user-attachment";
final String value = "attachment-value";
RpcContext.removeClientAttachment();
RpcContext.getClientAttachment().setAttachment(key, value);
delegate.greetWithAttachment(GreeterRequest.newBuilder().setName("meta").build());
final String returned = (String) RpcContext.getServiceContext().getObjectAttachment(key);
Assert.assertEquals(value, returned);
}
@Test
public void methodNotFound() {
try {
delegate.methodNonExist(GreeterRequest.newBuilder().setName("meta").build());
TimeUnit.SECONDS.sleep(1);
}catch (RpcException | InterruptedException e){
Assert.assertTrue(e.getMessage().contains("not found"));
}
}
}
@@ -18,6 +18,8 @@ message GreeterReply {
service PbGreeter{
rpc greet(GreeterRequest) returns (GreeterReply);

rpc greetWithAttachment (GreeterRequest) returns (GreeterReply);

rpc greetException(GreeterRequest) returns (GreeterReply);

rpc greetStream(stream GreeterRequest) returns (stream GreeterReply);

0 comments on commit 114a7d4

Please sign in to comment.