|
35 | 35 | import static org.junit.jupiter.api.Assertions.fail; |
36 | 36 |
|
37 | 37 | import com.google.api.gax.core.NoCredentialsProvider; |
38 | | -import com.google.api.gax.rpc.ApiException; |
39 | | -import com.google.api.gax.rpc.StatusCode; |
40 | | -import com.google.cloud.NoCredentials; |
41 | | -import com.google.cloud.bigquery.*; |
42 | | -import com.google.cloud.translate.v3.*; |
43 | 38 | import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder; |
44 | 39 | import io.grpc.netty.shaded.io.netty.handler.ssl.ApplicationProtocolConfig; |
45 | 40 | import io.grpc.netty.shaded.io.netty.handler.ssl.SslContext; |
@@ -272,51 +267,38 @@ public static void teardown() { |
272 | 267 | } |
273 | 268 |
|
274 | 269 | private void runGrpcTest(int port, boolean shouldSucceed) throws Exception { |
275 | | - TranslationServiceSettings.Builder settingsBuilder = |
276 | | - TranslationServiceSettings.newBuilder() |
| 270 | + com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.Builder channelProviderBuilder = |
| 271 | + com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.newBuilder() |
277 | 272 | .setEndpoint("localhost:" + port) |
278 | | - .setCredentialsProvider(NoCredentialsProvider.create()); |
| 273 | + .setHeaderProvider(() -> java.util.Collections.emptyMap()); |
279 | 274 |
|
280 | | - com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.Builder channelProviderBuilder = |
281 | | - ((com.google.api.gax.grpc.InstantiatingGrpcChannelProvider) |
282 | | - settingsBuilder.getTransportChannelProvider()) |
283 | | - .toBuilder(); |
284 | | - channelProviderBuilder.setChannelConfigurator( |
285 | | - new com.google.api.core.ApiFunction< |
286 | | - io.grpc.ManagedChannelBuilder, io.grpc.ManagedChannelBuilder>() { |
287 | | - @Override |
288 | | - public io.grpc.ManagedChannelBuilder apply(io.grpc.ManagedChannelBuilder builder) { |
289 | | - if (clientSupportsPqc()) { |
290 | | - configureGrpcChannelForPqc(builder); |
291 | | - } else { |
292 | | - configureGrpcChannelForClassical(builder); |
293 | | - } |
294 | | - return builder; |
295 | | - } |
296 | | - }); |
297 | | - settingsBuilder.setTransportChannelProvider(channelProviderBuilder.build()); |
298 | | - |
299 | | - TranslationServiceSettings settings = settingsBuilder.build(); |
300 | | - |
301 | | - try (TranslationServiceClient client = TranslationServiceClient.create(settings)) { |
302 | | - List<String> contents = new ArrayList<>(); |
303 | | - contents.add("house"); |
304 | | - TranslateTextRequest request = |
305 | | - TranslateTextRequest.newBuilder() |
306 | | - .setParent("projects/test-project") |
307 | | - .addAllContents(contents) |
| 275 | + |
| 276 | + com.google.api.gax.grpc.InstantiatingGrpcChannelProvider channelProvider = channelProviderBuilder.build(); |
| 277 | + com.google.api.gax.rpc.TransportChannel transportChannel = channelProvider.getTransportChannel(); |
| 278 | + io.grpc.Channel channel = ((com.google.api.gax.grpc.GrpcTransportChannel) transportChannel).getChannel(); |
| 279 | + |
| 280 | + try { |
| 281 | + io.grpc.MethodDescriptor<byte[], byte[]> method = |
| 282 | + io.grpc.MethodDescriptor.<byte[], byte[]>newBuilder() |
| 283 | + .setType(io.grpc.MethodDescriptor.MethodType.UNARY) |
| 284 | + .setFullMethodName("Greeter/SayHello") |
| 285 | + .setRequestMarshaller(new ByteMarshaller()) |
| 286 | + .setResponseMarshaller(new ByteMarshaller()) |
308 | 287 | .build(); |
309 | 288 |
|
310 | | - try { |
311 | | - TranslateTextResponse response = client.translateText(request); |
312 | | - if (!shouldSucceed) { |
313 | | - fail("Expected gRPC call to fail!"); |
314 | | - } |
315 | | - assertNotNull(response); |
316 | | - } catch (ApiException e) { |
317 | | - if (shouldSucceed) { |
318 | | - fail("Expected gRPC call to succeed, but failed: " + e.getMessage(), e); |
319 | | - } |
| 289 | + byte[] response = io.grpc.stub.ClientCalls.blockingUnaryCall(channel, method, io.grpc.CallOptions.DEFAULT, "request".getBytes()); |
| 290 | + if (!shouldSucceed) { |
| 291 | + fail("Expected gRPC call to fail!"); |
| 292 | + } |
| 293 | + assertNotNull(response); |
| 294 | + assertEquals("PQC gRPC OK", new String(response)); |
| 295 | + } catch (Exception e) { |
| 296 | + if (shouldSucceed) { |
| 297 | + fail("Expected gRPC call to succeed, but failed: " + e.getMessage(), e); |
| 298 | + } |
| 299 | + } finally { |
| 300 | + if (channel instanceof io.grpc.ManagedChannel) { |
| 301 | + ((io.grpc.ManagedChannel) channel).shutdownNow(); |
320 | 302 | } |
321 | 303 | } |
322 | 304 | } |
@@ -395,4 +377,20 @@ private static void configureGrpcChannelForClassical(io.grpc.ManagedChannelBuild |
395 | 377 | throw new RuntimeException("Failed to configure shaded gRPC Netty channel for Classical", e); |
396 | 378 | } |
397 | 379 | } |
| 380 | + |
| 381 | + private static class ByteMarshaller implements io.grpc.MethodDescriptor.Marshaller<byte[]> { |
| 382 | + @Override |
| 383 | + public InputStream stream(byte[] value) { |
| 384 | + return new java.io.ByteArrayInputStream(value); |
| 385 | + } |
| 386 | + |
| 387 | + @Override |
| 388 | + public byte[] parse(InputStream stream) { |
| 389 | + try { |
| 390 | + return com.google.common.io.ByteStreams.toByteArray(stream); |
| 391 | + } catch (java.io.IOException e) { |
| 392 | + throw new RuntimeException(e); |
| 393 | + } |
| 394 | + } |
| 395 | + } |
398 | 396 | } |
0 commit comments