uros-b commented on code in PR #17767: URL: https://github.com/apache/iceberg/pull/17767#discussion_r3839620990
########## core/src/test/java/org/apache/iceberg/rest/TestDuplicatePageToken.java: ########## @@ -0,0 +1,175 @@ +/* + * 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.rest; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyMap; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.when; + +import java.util.function.Consumer; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.catalog.SessionCatalog; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.rest.responses.ConfigResponse; +import org.apache.iceberg.rest.responses.ListNamespacesResponse; +import org.apache.iceberg.rest.responses.ListTablesResponse; +import org.apache.iceberg.rest.responses.ErrorResponse; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +public class TestDuplicatePageToken { + + @Test + @SuppressWarnings("unchecked") + public void testListNamespacesThrowsOnDuplicatePageToken() { + RESTClient mockClient = Mockito.mock(RESTClient.class); + + // withAuthSession is a default method that returns this; stub it since Mockito + // does not execute default methods by default + when(mockClient.withAuthSession(any())).thenReturn(mockClient); + + // Config response with namespace listing endpoint enabled + ConfigResponse configResponse = + ConfigResponse.builder() + .withEndpoints(ImmutableList.of(Endpoint.V1_LIST_NAMESPACES)) + .build(); + when(mockClient.get( + anyString(), + anyMap(), + Mockito.eq(ConfigResponse.class), + anyMap(), + any(Consumer.class))) + .thenReturn(configResponse); + + // Namespace listing: always return the same page token "stuck" + ListNamespacesResponse dupResponse = + ListNamespacesResponse.builder() + .add(Namespace.of("demo")) + .nextPageToken("stuck") + .build(); + when(mockClient.get( + anyString(), + anyMap(), + Mockito.eq(ListNamespacesResponse.class), + anyMap(), + any(Consumer.class))) + .thenReturn(dupResponse); + + RESTCatalog catalog = + new RESTCatalog( + SessionCatalog.SessionContext.createEmpty(), (config) -> mockClient); + catalog.initialize("test", ImmutableMap.of()); + + assertThatThrownBy(() -> catalog.listNamespaces()) + .isInstanceOf(IllegalStateException.class) + .hasMessageContaining("duplicate page token"); + } + + @Test + @SuppressWarnings("unchecked") + public void testListTablesThrowsOnDuplicatePageToken() { + RESTClient mockClient = Mockito.mock(RESTClient.class); + + when(mockClient.withAuthSession(any())).thenReturn(mockClient); + + // Config response with table listing endpoint enabled + ConfigResponse configResponse = + ConfigResponse.builder() + .withEndpoints(ImmutableList.of(Endpoint.V1_LIST_TABLES)) + .build(); + when(mockClient.get( + anyString(), + anyMap(), + Mockito.eq(ConfigResponse.class), + anyMap(), + any(Consumer.class))) + .thenReturn(configResponse); + + // Table listing: always return the same page token "stuck" + ListTablesResponse dupResponse = + ListTablesResponse.builder() + .add(TableIdentifier.of(Namespace.of("demo"), "t1")) + .nextPageToken("stuck") + .build(); + when(mockClient.get( + anyString(), + anyMap(), + Mockito.eq(ListTablesResponse.class), + anyMap(), + any(Consumer.class))) + .thenReturn(dupResponse); + + RESTCatalog catalog = + new RESTCatalog( + SessionCatalog.SessionContext.createEmpty(), (config) -> mockClient); + catalog.initialize("test", ImmutableMap.of()); + + assertThatThrownBy(() -> catalog.listTables(Namespace.of("demo"))) + .isInstanceOf(IllegalStateException.class) + .hasMessageContaining("duplicate page token"); + } + + @Test + @SuppressWarnings("unchecked") + public void testListViewsThrowsOnDuplicatePageToken() { + RESTClient mockClient = Mockito.mock(RESTClient.class); + + when(mockClient.withAuthSession(any())).thenReturn(mockClient); + + // Config response with view listing endpoint enabled + ConfigResponse configResponse = + ConfigResponse.builder() + .withEndpoints(ImmutableList.of(Endpoint.V1_LIST_VIEWS)) + .build(); + when(mockClient.get( + anyString(), + anyMap(), + Mockito.eq(ConfigResponse.class), + anyMap(), + any(Consumer.class))) + .thenReturn(configResponse); + + // View listing: always return the same page token "stuck" + ListTablesResponse dupResponse = + ListTablesResponse.builder() + .add(TableIdentifier.of(Namespace.of("demo"), "v1")) + .nextPageToken("stuck") + .build(); + when(mockClient.get( + anyString(), + anyMap(), + Mockito.eq(ListTablesResponse.class), + anyMap(), + any(Consumer.class))) + .thenReturn(dupResponse); + + RESTCatalog catalog = + new RESTCatalog( + SessionCatalog.SessionContext.createEmpty(), (config) -> mockClient); + catalog.initialize("test", ImmutableMap.of()); + + assertThatThrownBy(() -> catalog.listViews(Namespace.of("demo"))) + .isInstanceOf(IllegalStateException.class) + .hasMessageContaining("duplicate page token"); + } Review Comment: Coverage gap on the new guard code path. All three tests assert only the throw, driven by an always-"stuck" server that repeats one token immediately. There is no positive test proving the guard leaves a valid multi-page listing intact (distinct tokens across N pages, null terminator, all results returned), and none covering the alternating-token loop (A to B to A to B) that the full-HashSet design specifically exists to catch. A no-throw happy-path test plus an alternating-token test should be added so a future regression in the guard (for example a stray double-add of the "" sentinel) that silently truncates real listings would be caught. Live regression risk is bounded because the existing CatalogTests/RESTCatalog pagination suites exercise normal multi-page listing, which is why this is minor rather than blocking, but the strengthening belongs in this PR since it introduces the new code path. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
