voonhous commented on code in PR #19876:
URL: https://github.com/apache/hudi/pull/19876#discussion_r3975338765


##########
hudi-aws/src/test/java/org/apache/hudi/aws/utils/TestDynamoTableUtils.java:
##########
@@ -0,0 +1,179 @@
+/*
+ * 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.hudi.aws.utils;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import software.amazon.awssdk.core.exception.SdkClientException;
+import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
+import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest;
+import software.amazon.awssdk.services.dynamodb.model.CreateTableResponse;
+import software.amazon.awssdk.services.dynamodb.model.DeleteTableRequest;
+import software.amazon.awssdk.services.dynamodb.model.DeleteTableResponse;
+import software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest;
+import software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse;
+import software.amazon.awssdk.services.dynamodb.model.ResourceInUseException;
+import 
software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException;
+import software.amazon.awssdk.services.dynamodb.model.TableDescription;
+import software.amazon.awssdk.services.dynamodb.model.TableStatus;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.atLeast;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * Tests {@link DynamoTableUtils} against a mocked DynamoDB client. The 
polling helpers are always
+ * given an explicit, short timeout so the tests stay fast and deterministic.
+ */
+@ExtendWith(MockitoExtension.class)
+class TestDynamoTableUtils {
+
+  private static final String TABLE_NAME = "lock_table";
+  private static final int TIMEOUT_MS = 300;
+  private static final int INTERVAL_MS = 50;
+
+  @Mock
+  private DynamoDbClient dynamoDb;
+
+  @Test
+  void testWaitUntilExists_returnsOnTheFirstDescription() throws Exception {
+    
when(dynamoDb.describeTable(any(DescribeTableRequest.class))).thenReturn(describeResponse(TableStatus.CREATING));
+
+    DynamoTableUtils.waitUntilExists(dynamoDb, TABLE_NAME, TIMEOUT_MS, 
INTERVAL_MS);
+
+    ArgumentCaptor<DescribeTableRequest> captor = 
ArgumentCaptor.forClass(DescribeTableRequest.class);
+    verify(dynamoDb, times(1)).describeTable(captor.capture());
+    assertEquals(TABLE_NAME, captor.getValue().tableName(),
+        "any table status is enough to prove the table exists");
+  }
+
+  @Test
+  void testWaitUntilExists_pollsUntilTheTableShowsUp() throws Exception {
+    when(dynamoDb.describeTable(any(DescribeTableRequest.class)))
+        .thenThrow(ResourceNotFoundException.builder().message("not there 
yet").build())
+        .thenReturn(describeResponse(TableStatus.ACTIVE));
+
+    DynamoTableUtils.waitUntilExists(dynamoDb, TABLE_NAME, TIMEOUT_MS, 
INTERVAL_MS);
+
+    verify(dynamoDb, times(2)).describeTable(any(DescribeTableRequest.class));
+  }
+
+  @Test
+  void testWaitUntilExists_throwsWhenTheTableNeverShowsUp() {
+    when(dynamoDb.describeTable(any(DescribeTableRequest.class)))
+        .thenThrow(ResourceNotFoundException.builder().message("not there 
yet").build());
+
+    SdkClientException ex = assertThrows(SdkClientException.class,
+        () -> DynamoTableUtils.waitUntilExists(dynamoDb, TABLE_NAME, 
TIMEOUT_MS, INTERVAL_MS));
+
+    assertTrue(ex.getMessage().contains(TABLE_NAME + " never returned a 
result"));
+    verify(dynamoDb, 
atLeast(2)).describeTable(any(DescribeTableRequest.class));
+  }
+
+  @Test
+  void testWaitUntilActive_returnsWhenTheTableIsActive() throws Exception {
+    when(dynamoDb.describeTable(any(DescribeTableRequest.class)))
+        .thenReturn(describeResponse(TableStatus.CREATING))
+        .thenReturn(describeResponse(TableStatus.ACTIVE));
+
+    DynamoTableUtils.waitUntilActive(dynamoDb, TABLE_NAME, TIMEOUT_MS, 
INTERVAL_MS);
+
+    verify(dynamoDb, times(2)).describeTable(any(DescribeTableRequest.class));
+  }
+
+  @Test
+  void testWaitUntilActive_throwsWhenTheTableStaysInAnotherState() {
+    
when(dynamoDb.describeTable(any(DescribeTableRequest.class))).thenReturn(describeResponse(TableStatus.CREATING));
+
+    DynamoTableUtils.TableNeverTransitionedToStateException ex =
+        
assertThrows(DynamoTableUtils.TableNeverTransitionedToStateException.class,
+            () -> DynamoTableUtils.waitUntilActive(dynamoDb, TABLE_NAME, 
TIMEOUT_MS, INTERVAL_MS));
+
+    assertTrue(ex.getMessage().contains(TABLE_NAME + " never transitioned to 
desired state of ACTIVE"));
+  }
+
+  @Test
+  void testWaitUntilActive_throwsWhenTheTableNeverAppears() {
+    when(dynamoDb.describeTable(any(DescribeTableRequest.class)))
+        .thenThrow(ResourceNotFoundException.builder().message("not there 
yet").build());
+
+    assertThrows(DynamoTableUtils.TableNeverTransitionedToStateException.class,
+        () -> DynamoTableUtils.waitUntilActive(dynamoDb, TABLE_NAME, 
TIMEOUT_MS, INTERVAL_MS),
+        "a table that never gets described is reported the same way as one 
stuck in another state");
+  }
+
+  @Test
+  void testDefaultTimeoutOverloadsReturnAsSoonAsTheTableIsReady() throws 
Exception {
+    
when(dynamoDb.describeTable(any(DescribeTableRequest.class))).thenReturn(describeResponse(TableStatus.ACTIVE));
+
+    // both overloads poll before sleeping, so an already-ready table returns 
without waiting
+    DynamoTableUtils.waitUntilExists(dynamoDb, TABLE_NAME);

Review Comment:
   Added `@Timeout(value = 10, unit = SECONDS)` to that test and reworded the 
class javadoc: the explicit-timeout tests bound their own runtime, the 
default-overload one is bounded by `@Timeout`. Done in bb7fccea3715.



-- 
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