|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | + * |
| 6 | + * Modifications copyright (C) 2017 Uber Technologies, Inc. |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this material except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +package io.temporal.serviceclient; |
| 22 | + |
| 23 | +import static org.junit.Assert.assertEquals; |
| 24 | + |
| 25 | +import io.grpc.ClientInterceptor; |
| 26 | +import io.grpc.ManagedChannel; |
| 27 | +import io.grpc.Status; |
| 28 | +import io.grpc.StatusRuntimeException; |
| 29 | +import io.grpc.inprocess.InProcessChannelBuilder; |
| 30 | +import io.grpc.inprocess.InProcessServerBuilder; |
| 31 | +import io.grpc.stub.StreamObserver; |
| 32 | +import io.grpc.testing.GrpcCleanupRule; |
| 33 | +import io.temporal.api.workflowservice.v1.GetSystemInfoRequest; |
| 34 | +import io.temporal.api.workflowservice.v1.GetSystemInfoResponse; |
| 35 | +import io.temporal.api.workflowservice.v1.WorkflowServiceGrpc; |
| 36 | +import java.time.Duration; |
| 37 | +import java.util.*; |
| 38 | +import java.util.concurrent.ArrayBlockingQueue; |
| 39 | +import java.util.concurrent.atomic.AtomicInteger; |
| 40 | +import org.junit.*; |
| 41 | + |
| 42 | +public class SystemInfoTimeoutTest { |
| 43 | + |
| 44 | + private static final GetSystemInfoResponse.Capabilities CAPABILITIES = |
| 45 | + GetSystemInfoResponse.Capabilities.newBuilder().setInternalErrorDifferentiation(true).build(); |
| 46 | + |
| 47 | + private static final GetSystemInfoResponse GET_SYSTEM_INFO_RESPONSE = |
| 48 | + GetSystemInfoResponse.newBuilder().setCapabilities(CAPABILITIES).build(); |
| 49 | + |
| 50 | + private static final RpcRetryOptions RPC_RETRY_OPTIONS = |
| 51 | + RpcRetryOptions.newBuilder() |
| 52 | + .setInitialInterval(Duration.ofMillis(10)) |
| 53 | + .setBackoffCoefficient(1.0) |
| 54 | + .setMaximumAttempts(3) |
| 55 | + .setExpiration(Duration.ofMillis(100)) |
| 56 | + .validateBuildWithDefaults(); |
| 57 | + |
| 58 | + @Rule public final GrpcCleanupRule grpcCleanupRule = new GrpcCleanupRule(); |
| 59 | + private final AtomicInteger getSystemInfoCount = new AtomicInteger(0); |
| 60 | + private final AbstractQueue<Duration> getSystemInfoTimeout = new ArrayBlockingQueue<Duration>(10); |
| 61 | + |
| 62 | + private final WorkflowServiceGrpc.WorkflowServiceImplBase workflowImpl = |
| 63 | + new WorkflowServiceGrpc.WorkflowServiceImplBase() { |
| 64 | + @Override |
| 65 | + public void getSystemInfo( |
| 66 | + GetSystemInfoRequest request, StreamObserver<GetSystemInfoResponse> responseObserver) { |
| 67 | + Duration timeout = getSystemInfoTimeout.poll(); |
| 68 | + if (timeout != null) { |
| 69 | + try { |
| 70 | + Thread.sleep(timeout.toMillis()); |
| 71 | + } catch (InterruptedException e) { |
| 72 | + throw new RuntimeException(e); |
| 73 | + } |
| 74 | + } |
| 75 | + getSystemInfoCount.getAndIncrement(); |
| 76 | + responseObserver.onNext(GET_SYSTEM_INFO_RESPONSE); |
| 77 | + responseObserver.onCompleted(); |
| 78 | + } |
| 79 | + }; |
| 80 | + |
| 81 | + private ManagedChannel managedChannel; |
| 82 | + |
| 83 | + @Before |
| 84 | + public void setUp() throws Exception { |
| 85 | + getSystemInfoCount.set(0); |
| 86 | + String serverName = InProcessServerBuilder.generateName(); |
| 87 | + grpcCleanupRule.register( |
| 88 | + InProcessServerBuilder.forName(serverName) |
| 89 | + .directExecutor() |
| 90 | + .addService(workflowImpl) |
| 91 | + .build() |
| 92 | + .start()); |
| 93 | + managedChannel = |
| 94 | + grpcCleanupRule.register( |
| 95 | + InProcessChannelBuilder.forName(serverName).directExecutor().build()); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testGetServerCapabilitiesTimeoutExceeded() { |
| 100 | + WorkflowServiceStubsOptions serviceStubsOptions = |
| 101 | + WorkflowServiceStubsOptions.newBuilder() |
| 102 | + .setChannel(managedChannel) |
| 103 | + .setRpcRetryOptions(RPC_RETRY_OPTIONS) |
| 104 | + .setSystemInfoTimeout(Duration.ofSeconds(1)) |
| 105 | + .validateAndBuildWithDefaults(); |
| 106 | + |
| 107 | + ClientInterceptor deadlineInterceptor = |
| 108 | + new GrpcDeadlineInterceptor( |
| 109 | + serviceStubsOptions.getRpcTimeout(), |
| 110 | + serviceStubsOptions.getRpcLongPollTimeout(), |
| 111 | + serviceStubsOptions.getRpcQueryTimeout()); |
| 112 | + |
| 113 | + ChannelManager channelManager = |
| 114 | + new ChannelManager(serviceStubsOptions, Collections.singletonList(deadlineInterceptor)); |
| 115 | + |
| 116 | + getSystemInfoTimeout.add(Duration.ofSeconds(2)); |
| 117 | + |
| 118 | + StatusRuntimeException sre = |
| 119 | + Assert.assertThrows( |
| 120 | + StatusRuntimeException.class, () -> channelManager.getServerCapabilities().get()); |
| 121 | + assertEquals(Status.Code.DEADLINE_EXCEEDED, sre.getStatus().getCode()); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void testGetServerCapabilitiesRetry() { |
| 126 | + WorkflowServiceStubsOptions serviceStubsOptions = |
| 127 | + WorkflowServiceStubsOptions.newBuilder() |
| 128 | + .setChannel(managedChannel) |
| 129 | + .setRpcRetryOptions(RPC_RETRY_OPTIONS) |
| 130 | + .setRpcTimeout(Duration.ofMillis(500)) |
| 131 | + .setSystemInfoTimeout(Duration.ofSeconds(5)) |
| 132 | + .validateAndBuildWithDefaults(); |
| 133 | + |
| 134 | + ClientInterceptor deadlineInterceptor = |
| 135 | + new GrpcDeadlineInterceptor( |
| 136 | + serviceStubsOptions.getRpcTimeout(), |
| 137 | + serviceStubsOptions.getRpcLongPollTimeout(), |
| 138 | + serviceStubsOptions.getRpcQueryTimeout()); |
| 139 | + |
| 140 | + ChannelManager channelManager = |
| 141 | + new ChannelManager(serviceStubsOptions, Collections.singletonList(deadlineInterceptor)); |
| 142 | + |
| 143 | + getSystemInfoTimeout.add(Duration.ofSeconds(1)); |
| 144 | + getSystemInfoTimeout.add(Duration.ofSeconds(1)); |
| 145 | + |
| 146 | + GetSystemInfoResponse.Capabilities capabilities = channelManager.getServerCapabilities().get(); |
| 147 | + assertEquals(CAPABILITIES, capabilities); |
| 148 | + assertEquals(3, getSystemInfoCount.get()); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testGetServerCapabilitiesTimeout() { |
| 153 | + WorkflowServiceStubsOptions serviceStubsOptions = |
| 154 | + WorkflowServiceStubsOptions.newBuilder() |
| 155 | + .setChannel(managedChannel) |
| 156 | + .setRpcRetryOptions(RPC_RETRY_OPTIONS) |
| 157 | + .setSystemInfoTimeout(Duration.ofSeconds(10)) |
| 158 | + .validateAndBuildWithDefaults(); |
| 159 | + |
| 160 | + ClientInterceptor deadlineInterceptor = |
| 161 | + new GrpcDeadlineInterceptor( |
| 162 | + serviceStubsOptions.getRpcTimeout(), |
| 163 | + serviceStubsOptions.getRpcLongPollTimeout(), |
| 164 | + serviceStubsOptions.getRpcQueryTimeout()); |
| 165 | + |
| 166 | + ChannelManager channelManager = |
| 167 | + new ChannelManager(serviceStubsOptions, Collections.singletonList(deadlineInterceptor)); |
| 168 | + |
| 169 | + getSystemInfoTimeout.add(Duration.ofSeconds(6)); |
| 170 | + |
| 171 | + GetSystemInfoResponse.Capabilities capabilities = channelManager.getServerCapabilities().get(); |
| 172 | + assertEquals(CAPABILITIES, capabilities); |
| 173 | + assertEquals(1, getSystemInfoCount.get()); |
| 174 | + } |
| 175 | +} |
0 commit comments