parveensania commented on code in PR #37840:
URL: https://github.com/apache/beam/pull/37840#discussion_r2992472175


##########
runners/google-cloud-dataflow-java/worker/src/test/java/org/apache/beam/runners/dataflow/worker/windmill/client/grpc/stubs/FailoverChannelTest.java:
##########
@@ -0,0 +1,501 @@
+/*
+ * 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
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.beam.runners.dataflow.worker.windmill.client.grpc.stubs;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.same;
+import static org.mockito.Mockito.atLeast;
+import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.AtomicReference;
+import org.apache.beam.vendor.grpc.v1p69p0.io.grpc.CallCredentials;
+import org.apache.beam.vendor.grpc.v1p69p0.io.grpc.CallOptions;
+import org.apache.beam.vendor.grpc.v1p69p0.io.grpc.ClientCall;
+import org.apache.beam.vendor.grpc.v1p69p0.io.grpc.ClientCall.Listener;
+import org.apache.beam.vendor.grpc.v1p69p0.io.grpc.ConnectivityState;
+import org.apache.beam.vendor.grpc.v1p69p0.io.grpc.ManagedChannel;
+import org.apache.beam.vendor.grpc.v1p69p0.io.grpc.Metadata;
+import org.apache.beam.vendor.grpc.v1p69p0.io.grpc.MethodDescriptor;
+import org.apache.beam.vendor.grpc.v1p69p0.io.grpc.Status;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+import org.mockito.ArgumentCaptor;
+
+@RunWith(JUnit4.class)
+public class FailoverChannelTest {
+
+  private MethodDescriptor<Object, Object> methodDescriptor =
+      MethodDescriptor.newBuilder()
+          .setType(MethodDescriptor.MethodType.UNARY)
+          .setFullMethodName(MethodDescriptor.generateFullMethodName("test", 
"test"))
+          .setRequestMarshaller(new IsolationChannelTest.NoopMarshaller())
+          .setResponseMarshaller(new IsolationChannelTest.NoopMarshaller())
+          .build();
+
+  private static FailoverChannel createForTest(ManagedChannel primary, 
ManagedChannel fallback) {
+    return FailoverChannel.forTest(primary, fallback, null, System::nanoTime);
+  }
+
+  /**
+   * Starts a call on the primary channel, captures the injected listener, and 
fires onClose with
+   * the given status. Use this to trigger RPC-based failover in tests.
+   */
+  private void triggerRPCFailure(
+      FailoverChannel channel, ClientCall<Object, Object> underlying, Status 
status)
+      throws Exception {
+    channel
+        .newCall(methodDescriptor, CallOptions.DEFAULT)
+        .start(new NoopClientCall.NoopClientCallListener<>(), new Metadata());
+    ArgumentCaptor<Listener<Object>> captor = 
ArgumentCaptor.forClass(ClientCall.Listener.class);
+    verify(underlying).start(captor.capture(), any());

Review Comment:
   Done



##########
runners/google-cloud-dataflow-java/worker/src/test/java/org/apache/beam/runners/dataflow/worker/windmill/client/grpc/stubs/FailoverChannelTest.java:
##########
@@ -0,0 +1,501 @@
+/*
+ * 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
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.beam.runners.dataflow.worker.windmill.client.grpc.stubs;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.same;
+import static org.mockito.Mockito.atLeast;
+import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.AtomicReference;
+import org.apache.beam.vendor.grpc.v1p69p0.io.grpc.CallCredentials;
+import org.apache.beam.vendor.grpc.v1p69p0.io.grpc.CallOptions;
+import org.apache.beam.vendor.grpc.v1p69p0.io.grpc.ClientCall;
+import org.apache.beam.vendor.grpc.v1p69p0.io.grpc.ClientCall.Listener;
+import org.apache.beam.vendor.grpc.v1p69p0.io.grpc.ConnectivityState;
+import org.apache.beam.vendor.grpc.v1p69p0.io.grpc.ManagedChannel;
+import org.apache.beam.vendor.grpc.v1p69p0.io.grpc.Metadata;
+import org.apache.beam.vendor.grpc.v1p69p0.io.grpc.MethodDescriptor;
+import org.apache.beam.vendor.grpc.v1p69p0.io.grpc.Status;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+import org.mockito.ArgumentCaptor;
+
+@RunWith(JUnit4.class)
+public class FailoverChannelTest {
+
+  private MethodDescriptor<Object, Object> methodDescriptor =
+      MethodDescriptor.newBuilder()
+          .setType(MethodDescriptor.MethodType.UNARY)
+          .setFullMethodName(MethodDescriptor.generateFullMethodName("test", 
"test"))
+          .setRequestMarshaller(new IsolationChannelTest.NoopMarshaller())
+          .setResponseMarshaller(new IsolationChannelTest.NoopMarshaller())
+          .build();
+
+  private static FailoverChannel createForTest(ManagedChannel primary, 
ManagedChannel fallback) {
+    return FailoverChannel.forTest(primary, fallback, null, System::nanoTime);
+  }
+
+  /**
+   * Starts a call on the primary channel, captures the injected listener, and 
fires onClose with
+   * the given status. Use this to trigger RPC-based failover in tests.
+   */
+  private void triggerRPCFailure(
+      FailoverChannel channel, ClientCall<Object, Object> underlying, Status 
status)
+      throws Exception {
+    channel
+        .newCall(methodDescriptor, CallOptions.DEFAULT)
+        .start(new NoopClientCall.NoopClientCallListener<>(), new Metadata());
+    ArgumentCaptor<Listener<Object>> captor = 
ArgumentCaptor.forClass(ClientCall.Listener.class);
+    verify(underlying).start(captor.capture(), any());
+    captor.getValue().onClose(status, new Metadata());
+  }
+
+
+  public void testRPCFailureTriggersFallback() throws Exception {
+    // RPC failure with UNAVAILABLE should switch to fallback channel.
+    ManagedChannel mockChannel = mock(ManagedChannel.class);
+    ManagedChannel mockFallbackChannel = mock(ManagedChannel.class);
+    ClientCall<Object, Object> underlyingCall = mock(ClientCall.class);
+    ClientCall<Object, Object> fallbackCall = mock(ClientCall.class);
+    when(mockChannel.newCall(any(), any())).thenReturn(underlyingCall);
+    when(mockFallbackChannel.newCall(any(), any())).thenReturn(fallbackCall);
+
+    FailoverChannel failoverChannel = createForTest(mockChannel, 
mockFallbackChannel);
+
+    ClientCall<Object, Object> call1 =
+        failoverChannel.newCall(methodDescriptor, CallOptions.DEFAULT);
+    Metadata metadata1 = new Metadata();
+    call1.start(new NoopClientCall.NoopClientCallListener<>(), metadata1);
+
+    ArgumentCaptor<Listener<Object>> captor = 
ArgumentCaptor.forClass(ClientCall.Listener.class);
+    verify(underlyingCall).start(captor.capture(), same(metadata1));
+    captor.getValue().onClose(Status.UNAVAILABLE, new Metadata());
+
+    ClientCall<Object, Object> call2 =
+        failoverChannel.newCall(methodDescriptor, CallOptions.DEFAULT);
+    call2.start(new NoopClientCall.NoopClientCallListener<>(), new Metadata());
+    verify(mockFallbackChannel, atLeastOnce()).newCall(any(), any());
+  }
+
+  @Test
+  public void testRPCFailureRecoveryAfterCoolingPeriod() throws Exception {
+    // After RPC failure, channel stays on fallback during cooling period, 
then returns to primary.
+    ManagedChannel mockChannel = mock(ManagedChannel.class);
+    ManagedChannel mockFallbackChannel = mock(ManagedChannel.class);
+    ClientCall<Object, Object> underlyingCall = mock(ClientCall.class);
+    ClientCall<Object, Object> fallbackCall = mock(ClientCall.class);
+    when(mockChannel.newCall(any(), any())).thenReturn(underlyingCall, 
mock(ClientCall.class));
+    when(mockFallbackChannel.newCall(any(), any())).thenReturn(fallbackCall);
+
+    when(mockChannel.getState(false)).thenReturn(ConnectivityState.READY);
+
+    AtomicLong time = new AtomicLong(0);
+    FailoverChannel failoverChannel =
+        FailoverChannel.forTest(mockChannel, mockFallbackChannel, null, 
time::get);
+
+    // Trigger RPC failure fallback

Review Comment:
   Done



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to