use of io.grpc.stub.StreamObserver in project beam by apache.
the class BeamFnControlClientTest method testDelegation.
@Test
public void testDelegation() throws Exception {
AtomicBoolean clientClosedStream = new AtomicBoolean();
BlockingQueue<BeamFnApi.InstructionResponse> values = new LinkedBlockingQueue<>();
BlockingQueue<StreamObserver<BeamFnApi.InstructionRequest>> outboundServerObservers = new LinkedBlockingQueue<>();
CallStreamObserver<BeamFnApi.InstructionResponse> inboundServerObserver = TestStreams.withOnNext(values::add).withOnCompleted(() -> clientClosedStream.set(true)).build();
BeamFnApi.ApiServiceDescriptor apiServiceDescriptor = BeamFnApi.ApiServiceDescriptor.newBuilder().setUrl(this.getClass().getName() + "-" + UUID.randomUUID().toString()).build();
Server server = InProcessServerBuilder.forName(apiServiceDescriptor.getUrl()).addService(new BeamFnControlGrpc.BeamFnControlImplBase() {
@Override
public StreamObserver<BeamFnApi.InstructionResponse> control(StreamObserver<BeamFnApi.InstructionRequest> outboundObserver) {
Uninterruptibles.putUninterruptibly(outboundServerObservers, outboundObserver);
return inboundServerObserver;
}
}).build();
server.start();
try {
ManagedChannel channel = InProcessChannelBuilder.forName(apiServiceDescriptor.getUrl()).build();
EnumMap<BeamFnApi.InstructionRequest.RequestCase, ThrowingFunction<BeamFnApi.InstructionRequest, BeamFnApi.InstructionResponse.Builder>> handlers = new EnumMap<>(BeamFnApi.InstructionRequest.RequestCase.class);
handlers.put(BeamFnApi.InstructionRequest.RequestCase.PROCESS_BUNDLE, new ThrowingFunction<BeamFnApi.InstructionRequest, BeamFnApi.InstructionResponse.Builder>() {
@Override
public BeamFnApi.InstructionResponse.Builder apply(BeamFnApi.InstructionRequest value) throws Exception {
return BeamFnApi.InstructionResponse.newBuilder().setProcessBundle(BeamFnApi.ProcessBundleResponse.getDefaultInstance());
}
});
handlers.put(BeamFnApi.InstructionRequest.RequestCase.REGISTER, new ThrowingFunction<BeamFnApi.InstructionRequest, BeamFnApi.InstructionResponse.Builder>() {
@Override
public BeamFnApi.InstructionResponse.Builder apply(BeamFnApi.InstructionRequest value) throws Exception {
throw FAILURE;
}
});
BeamFnControlClient client = new BeamFnControlClient(apiServiceDescriptor, (BeamFnApi.ApiServiceDescriptor descriptor) -> channel, this::createStreamForTest, handlers);
// Get the connected client and attempt to send and receive an instruction
StreamObserver<BeamFnApi.InstructionRequest> outboundServerObserver = outboundServerObservers.take();
ExecutorService executor = Executors.newCachedThreadPool();
Future<Void> future = executor.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
client.processInstructionRequests(executor);
return null;
}
});
outboundServerObserver.onNext(SUCCESSFUL_REQUEST);
assertEquals(SUCCESSFUL_RESPONSE, values.take());
// Ensure that conversion of an unknown request type is properly converted to a
// failure response.
outboundServerObserver.onNext(UNKNOWN_HANDLER_REQUEST);
assertEquals(UNKNOWN_HANDLER_RESPONSE, values.take());
// Ensure that all exceptions are caught and translated to failures
outboundServerObserver.onNext(FAILURE_REQUEST);
assertEquals(FAILURE_RESPONSE, values.take());
// Ensure that the server completing the stream translates to the completable future
// being completed allowing for a successful shutdown of the client.
outboundServerObserver.onCompleted();
future.get();
} finally {
server.shutdownNow();
}
}
use of io.grpc.stub.StreamObserver in project beam by apache.
the class FnHarnessTest method testLaunchFnHarnessAndTeardownCleanly.
@Test
public void testLaunchFnHarnessAndTeardownCleanly() throws Exception {
PipelineOptions options = PipelineOptionsFactory.create();
List<BeamFnApi.LogEntry> logEntries = new ArrayList<>();
List<BeamFnApi.InstructionResponse> instructionResponses = new ArrayList<>();
BeamFnLoggingGrpc.BeamFnLoggingImplBase loggingService = new BeamFnLoggingGrpc.BeamFnLoggingImplBase() {
@Override
public StreamObserver<BeamFnApi.LogEntry.List> logging(StreamObserver<LogControl> responseObserver) {
return TestStreams.withOnNext((BeamFnApi.LogEntry.List entries) -> logEntries.addAll(entries.getLogEntriesList())).withOnCompleted(() -> responseObserver.onCompleted()).build();
}
};
BeamFnControlGrpc.BeamFnControlImplBase controlService = new BeamFnControlGrpc.BeamFnControlImplBase() {
@Override
public StreamObserver<InstructionResponse> control(StreamObserver<InstructionRequest> responseObserver) {
CountDownLatch waitForResponses = new CountDownLatch(1);
options.as(GcsOptions.class).getExecutorService().submit(new Runnable() {
@Override
public void run() {
responseObserver.onNext(INSTRUCTION_REQUEST);
Uninterruptibles.awaitUninterruptibly(waitForResponses);
responseObserver.onCompleted();
}
});
return TestStreams.withOnNext(new Consumer<BeamFnApi.InstructionResponse>() {
@Override
public void accept(InstructionResponse t) {
instructionResponses.add(t);
waitForResponses.countDown();
}
}).withOnCompleted(waitForResponses::countDown).build();
}
};
Server loggingServer = ServerBuilder.forPort(0).addService(loggingService).build();
loggingServer.start();
try {
Server controlServer = ServerBuilder.forPort(0).addService(controlService).build();
controlServer.start();
try {
BeamFnApi.ApiServiceDescriptor loggingDescriptor = BeamFnApi.ApiServiceDescriptor.newBuilder().setId("1L").setUrl("localhost:" + loggingServer.getPort()).build();
BeamFnApi.ApiServiceDescriptor controlDescriptor = BeamFnApi.ApiServiceDescriptor.newBuilder().setId("2L").setUrl("localhost:" + controlServer.getPort()).build();
FnHarness.main(options, loggingDescriptor, controlDescriptor);
assertThat(instructionResponses, contains(INSTRUCTION_RESPONSE));
} finally {
controlServer.shutdownNow();
}
} finally {
loggingServer.shutdownNow();
}
}
use of io.grpc.stub.StreamObserver in project grpc-java by grpc.
the class DetailErrorSample method asyncCall.
void asyncCall() {
GreeterStub stub = GreeterGrpc.newStub(channel);
HelloRequest request = HelloRequest.newBuilder().build();
final CountDownLatch latch = new CountDownLatch(1);
StreamObserver<HelloReply> responseObserver = new StreamObserver<HelloReply>() {
@Override
public void onNext(HelloReply value) {
// Won't be called.
}
@Override
public void onError(Throwable t) {
verifyErrorReply(t);
latch.countDown();
}
@Override
public void onCompleted() {
// Won't be called, since the server in this example always fails.
}
};
stub.sayHello(request, responseObserver);
if (!Uninterruptibles.awaitUninterruptibly(latch, 1, TimeUnit.SECONDS)) {
throw new RuntimeException("timeout!");
}
}
use of io.grpc.stub.StreamObserver in project grpc-java by grpc.
the class HeaderServerInterceptorTest method setUp.
@Before
public void setUp() throws Exception {
String uniqueServerName = "fake server for " + getClass();
GreeterImplBase greeterImplBase = new GreeterImplBase() {
@Override
public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
responseObserver.onNext(HelloReply.getDefaultInstance());
responseObserver.onCompleted();
}
};
fakeServer = InProcessServerBuilder.forName(uniqueServerName).addService(ServerInterceptors.intercept(greeterImplBase, new HeaderServerInterceptor())).directExecutor().build().start();
inProcessChannel = InProcessChannelBuilder.forName(uniqueServerName).directExecutor().build();
}
use of io.grpc.stub.StreamObserver in project grpc-java by grpc.
the class RouteGuideClientTest method getFeature.
/**
* Example for testing blocking unary call.
*/
@Test
public void getFeature() {
Point requestPoint = Point.newBuilder().setLatitude(-1).setLongitude(-1).build();
Point responsePoint = Point.newBuilder().setLatitude(-123).setLongitude(-123).build();
final AtomicReference<Point> pointDelivered = new AtomicReference<Point>();
final Feature responseFeature = Feature.newBuilder().setName("dummyFeature").setLocation(responsePoint).build();
// implement the fake service
RouteGuideImplBase getFeatureImpl = new RouteGuideImplBase() {
@Override
public void getFeature(Point point, StreamObserver<Feature> responseObserver) {
pointDelivered.set(point);
responseObserver.onNext(responseFeature);
responseObserver.onCompleted();
}
};
serviceRegistry.addService(getFeatureImpl);
client.getFeature(-1, -1);
assertEquals(requestPoint, pointDelivered.get());
verify(testHelper).onMessage(responseFeature);
verify(testHelper, never()).onRpcError(any(Throwable.class));
}
Aggregations