You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ratis.apache.org by "kaijchen (via GitHub)" <gi...@apache.org> on 2023/04/14 03:19:08 UTC

[GitHub] [ratis] kaijchen commented on a diff in pull request #842: RATIS-1807. Support timeout in gRPC.

kaijchen commented on code in PR #842:
URL: https://github.com/apache/ratis/pull/842#discussion_r1166238748


##########
ratis-test/src/test/java/org/apache/ratis/grpc/util/TestStreamObserverWithTimeout.java:
##########
@@ -0,0 +1,122 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ratis.grpc.util;
+
+import org.apache.ratis.BaseTest;
+import org.apache.ratis.grpc.util.GrpcTestClient.StreamObserverFactory;
+import org.apache.ratis.thirdparty.io.grpc.StatusRuntimeException;
+import org.apache.ratis.util.NetUtils;
+import org.apache.ratis.util.Slf4jUtils;
+import org.apache.ratis.util.StringUtils;
+import org.apache.ratis.util.TimeDuration;
+import org.junit.Assert;
+import org.junit.Test;
+import org.slf4j.event.Level;
+
+import java.net.InetSocketAddress;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.function.Function;
+
+public class TestStreamObserverWithTimeout extends BaseTest {
+  {
+    Slf4jUtils.setLogLevel(ResponseNotifyClientInterceptor.LOG, Level.TRACE);
+  }
+
+  enum Type {
+    WithDeadline(GrpcTestClient::withDeadline),
+    WithTimeout(GrpcTestClient::withTimeout);
+
+    private final Function<TimeDuration, StreamObserverFactory> factory;
+
+    Type(Function<TimeDuration, StreamObserverFactory> function) {
+      this.factory = function;
+    }
+
+    StreamObserverFactory createFunction(TimeDuration timeout) {
+      return factory.apply(timeout);
+    }
+  }
+
+  @Test
+  public void testWithDeadline() throws Exception {
+    //the total sleep time is within the deadline
+    runTestTimeout(2, Type.WithDeadline);
+  }
+
+  @Test
+  public void testWithDeadlineFailure() {
+    //Expected to have DEADLINE_EXCEEDED
+    testFailureCase("total sleep time is longer than the deadline",
+        () -> runTestTimeout(5, Type.WithDeadline),
+        ExecutionException.class, StatusRuntimeException.class);
+  }
+
+  @Test
+  public void testWithTimeout() throws Exception {
+    //Each sleep time is within the timeout,
+    //Note that the total sleep time is longer than the timeout, but it does not matter.
+    runTestTimeout(5, Type.WithTimeout);
+  }
+
+  void runTestTimeout(int slow, Type type) throws Exception {
+    LOG.info("slow = {}, {}", slow, type);
+    final TimeDuration timeout = ONE_SECOND.multiply(0.5);
+    final StreamObserverFactory function = type.createFunction(timeout);
+    final InetSocketAddress address = NetUtils.createLocalServerAddress();
+
+    final List<String> messages = new ArrayList<>();
+    for (int i = 0; i < 2 * slow; i++) {
+      messages.add("m" + i);
+    }
+    try (GrpcTestServer server = new GrpcTestServer(address.getPort(), slow, timeout)) {
+      final int port = server.start();
+      try (GrpcTestClient client = new GrpcTestClient(address.getHostName(), port, function)) {
+
+        final List<CompletableFuture<String>> futures = new ArrayList<>();
+        for (String m : messages) {
+          if (type == Type.WithTimeout) {
+            timeout.sleep();
+          }

Review Comment:
   These lines were removed in #850, and `testWithTimeout()` became flaky.
   But I guess the root cause might be race condition on `responseCount` between
   `ResponseNotifyClientInterceptor#notifier` and `StreamObserverWithTimeout#handleTimeout()`.
   
   Cc @szetszwo.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@ratis.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org