|
| 1 | +package com.microsoft.durabletask.azuremanaged; |
| 2 | + |
| 3 | +import com.microsoft.durabletask.DurableTaskClient; |
| 4 | +import com.microsoft.durabletask.DurableTaskGrpcClientBuilder; |
| 5 | +import com.microsoft.durabletask.NewOrchestrationInstanceOptions; |
| 6 | +import io.grpc.*; |
| 7 | +import io.grpc.stub.ServerCalls; |
| 8 | +import io.grpc.stub.StreamObserver; |
| 9 | +import io.grpc.netty.NettyServerBuilder; |
| 10 | +import org.junit.jupiter.api.AfterEach; |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.util.concurrent.atomic.AtomicReference; |
| 16 | + |
| 17 | +import static org.junit.jupiter.api.Assertions.*; |
| 18 | + |
| 19 | +public class DurableTaskSchedulerClientExtensionsUserAgentTest { |
| 20 | + private Server server; |
| 21 | + private Channel channel; |
| 22 | + private final AtomicReference<String> capturedUserAgent = new AtomicReference<>(); |
| 23 | + private static final String EXPECTED_USER_AGENT_PREFIX = "durabletask-java/"; |
| 24 | + |
| 25 | + // Dummy gRPC service definition |
| 26 | + public static class DummyService implements io.grpc.BindableService { |
| 27 | + @Override |
| 28 | + public ServerServiceDefinition bindService() { |
| 29 | + return ServerServiceDefinition.builder("TaskHubSidecarService") |
| 30 | + .addMethod( |
| 31 | + MethodDescriptor.<com.google.protobuf.Empty, com.google.protobuf.Empty>newBuilder() |
| 32 | + .setType(MethodDescriptor.MethodType.UNARY) |
| 33 | + .setFullMethodName(MethodDescriptor.generateFullMethodName("TaskHubSidecarService", "StartInstance")) |
| 34 | + .setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(com.google.protobuf.Empty.getDefaultInstance())) |
| 35 | + .setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(com.google.protobuf.Empty.getDefaultInstance())) |
| 36 | + .build(), |
| 37 | + ServerCalls.asyncUnaryCall( |
| 38 | + new ServerCalls.UnaryMethod<com.google.protobuf.Empty, com.google.protobuf.Empty>() { |
| 39 | + @Override |
| 40 | + public void invoke(com.google.protobuf.Empty request, StreamObserver<com.google.protobuf.Empty> responseObserver) { |
| 41 | + // Mock response for StartInstance |
| 42 | + responseObserver.onNext(com.google.protobuf.Empty.getDefaultInstance()); |
| 43 | + responseObserver.onCompleted(); |
| 44 | + } |
| 45 | + } |
| 46 | + ) |
| 47 | + ) |
| 48 | + .build(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @BeforeEach |
| 53 | + public void setUp() throws IOException { |
| 54 | + // Use NettyServerBuilder to expose the server via HTTP |
| 55 | + server = NettyServerBuilder.forPort(0) |
| 56 | + .addService(new DummyService()) // Register DummyService |
| 57 | + .intercept(new ServerInterceptor() { |
| 58 | + @Override |
| 59 | + public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall( |
| 60 | + ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) { |
| 61 | + String userAgent = headers.get(Metadata.Key.of("x-user-agent", Metadata.ASCII_STRING_MARSHALLER)); |
| 62 | + capturedUserAgent.set(userAgent); |
| 63 | + return next.startCall(call, headers); |
| 64 | + } |
| 65 | + }) |
| 66 | + .directExecutor() |
| 67 | + .build() |
| 68 | + .start(); |
| 69 | + int port = server.getPort(); |
| 70 | + String endpoint = "localhost:" + port; |
| 71 | + DurableTaskSchedulerClientOptions options = new DurableTaskSchedulerClientOptions(); |
| 72 | + options.setEndpointAddress(endpoint); |
| 73 | + options.setTaskHubName("testHub"); |
| 74 | + options.setAllowInsecureCredentials(true); // Netty is insecure for localhost |
| 75 | + channel = options.createGrpcChannel(); |
| 76 | + } |
| 77 | + |
| 78 | + @AfterEach |
| 79 | + public void tearDown() { |
| 80 | + if (server != null) server.shutdownNow(); |
| 81 | + if (channel != null && channel instanceof ManagedChannel) { |
| 82 | + ((ManagedChannel) channel).shutdownNow(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testUserAgentHeaderIsSet() { |
| 88 | + DurableTaskGrpcClientBuilder builder = new DurableTaskGrpcClientBuilder(); |
| 89 | + builder.grpcChannel(channel); |
| 90 | + DurableTaskClient client = builder.build(); |
| 91 | + |
| 92 | + // Schedule a new orchestration instance |
| 93 | + String instanceId = client.scheduleNewOrchestrationInstance( |
| 94 | + "TestOrchestration", |
| 95 | + new NewOrchestrationInstanceOptions().setInput("TestInput")); |
| 96 | + |
| 97 | + // Make a dummy call to trigger the request |
| 98 | + String userAgent = capturedUserAgent.get(); |
| 99 | + assertNotNull(userAgent, "X-User-Agent header should be set"); |
| 100 | + assertTrue(userAgent.startsWith(EXPECTED_USER_AGENT_PREFIX), "X-User-Agent should start with durabletask-java/"); |
| 101 | + } |
| 102 | +} |
| 103 | + |
0 commit comments