nastra commented on code in PR #10140:
URL: https://github.com/apache/iceberg/pull/10140#discussion_r1581127958


##########
core/src/test/java/org/apache/iceberg/TestClientPoolImpl.java:
##########
@@ -0,0 +1,149 @@
+/*
+ * 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.iceberg;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.assertj.core.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class TestClientPoolImpl {
+
+  class RetryableException extends RuntimeException {}
+
+  class NonRetryableException extends RuntimeException {}
+
+  class MockClient {
+    boolean closed = false;
+
+    int actions = 0;
+
+    int retryableFailures = 0;
+
+    public void close() {
+      closed = true;
+    }
+
+    public int successfulAction() {
+      actions++;
+      return actions;
+    }
+
+    int succeedAfter(int succeedAfterAttempts) {
+      if (retryableFailures == succeedAfterAttempts - 1) {
+        return successfulAction();
+      }
+
+      retryableFailures++;
+      throw new RetryableException();
+    }
+
+    int failWithNonRetryable() {
+      throw new NonRetryableException();
+    }
+  }
+
+  class MockClientPoolImpl extends ClientPoolImpl<MockClient, Exception> {
+
+    private int reconnectionAttempts;
+
+    MockClientPoolImpl(
+        int poolSize,
+        Class<? extends Exception> reconnectExc,
+        boolean retryByDefault,
+        int numRetries) {
+      super(poolSize, reconnectExc, retryByDefault, numRetries);
+    }
+
+    @Override
+    protected MockClient newClient() {
+      return new MockClient();
+    }
+
+    @Override
+    protected MockClient reconnect(MockClient client) {
+      reconnectionAttempts++;
+      return client;
+    }
+
+    @Override
+    protected void close(MockClient client) {
+      client.close();
+    }
+
+    int reconnectionAttempts() {
+      return reconnectionAttempts;
+    }
+  }
+
+  @Test
+  public void testRetrySucceedsWithinMaxAttempts() {
+    int maxRetries = 5;
+    int succeedAfterAttempts = 3;
+    try (MockClientPoolImpl mockClientPool =
+        new MockClientPoolImpl(2, RetryableException.class, true, maxRetries)) 
{
+      int actions = mockClientPool.run(client -> 
client.succeedAfter(succeedAfterAttempts));
+      Assertions.assertThat(actions)
+          .as("There should be exactly one successful action invocation")
+          .isEqualTo(1);
+      Assertions.assertThat(mockClientPool.reconnectionAttempts())
+          .isEqualTo(succeedAfterAttempts - 1);
+    } catch (Exception e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  @Test
+  public void testRetriesExhaustedAndSurfacesFailure() {
+    int maxRetries = 3;
+    int succeedAfterAttempts = 5;
+    MockClientPoolImpl mockClientPool =
+        new MockClientPoolImpl(2, RetryableException.class, true, maxRetries);
+    assertThrows(

Review Comment:
   we typically use `assertThatThrownBy(..)` from AssertJ rather than the stuff 
from JUnit



-- 
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-unsubscr...@iceberg.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to