test: stabilize live DNS pagination test
Build and Analyse / build-and-analyse (push) Successful in 1m21s

Co-authored-by: Junie <junie@jetbrains.com>
This commit is contained in:
thischwaandJunie committed 2026-11-07 19:27:44 +01:00
1 parent a7292153e2
commit 1a37d308e3
1 file changed
+90 -48
@@ -44,6 +44,9 @@ public class CfClientIntegrationTest {
private static final String IP_PREFIX = "130.0.0."; private static final String IP_PREFIX = "130.0.0.";
private static final String UPDATED_IP_PREFIX = "130.1.0."; private static final String UPDATED_IP_PREFIX = "130.1.0.";
private record PagedResult(int total, Set<String> allIds, List<List<RecordEntity>> pages) {
}
@BeforeAll @BeforeAll
static void checkEnv() { static void checkEnv() {
assumeTrue(API_TOKEN != null && !API_TOKEN.isBlank(), "API_TOKEN not set; skipping client tests"); assumeTrue(API_TOKEN != null && !API_TOKEN.isBlank(), "API_TOKEN not set; skipping client tests");
@@ -471,78 +474,73 @@ public class CfClientIntegrationTest {
assertEquals(2, groupedRecords.get("example.com.").size(), "The key 'example.com.' should have 2 records."); assertEquals(2, groupedRecords.get("example.com.").size(), "The key 'example.com.' should have 2 records.");
} }
@Test @Test
void testPaging() throws Exception { void testPaging() throws Exception {
ZoneEntity zone = client.zoneGet(ZONE_STR); ZoneEntity zone = client.zoneGet(ZONE_STR);
String pagingSld = "paging-" + System.currentTimeMillis(); String pagingSld = "paging-" + System.currentTimeMillis();
List<RecordEntity> createdRecords = new ArrayList<>(); List<RecordEntity> createdRecords = new ArrayList<>();
Set<String> createdIds = new HashSet<>();
try { try {
int existingCount = 0; List<RecordEntity> staleRecords = List.of();
try { try {
List<RecordEntity> allRecords = client.recordList(zone); staleRecords = client.recordList(zone).stream()
existingCount = allRecords.size(); .filter(record -> record.getName() != null && record.getName().startsWith("paging-"))
} catch (CloudflareApiException e) { .toList();
// ignore } catch (CloudflareNotFoundException ignored) {
// The zone is empty.
} }
deleteQuietly(zone, staleRecords);
// Calculate how many records we need to create to reach at least 12 total A records // Each record gets its own name, so the order by name is unambiguous.
// (to test paging with pageSize 5: page 1 = 5, page 2 = 5, page 3 = 2+) for (int i = 1; i <= 12; i++) {
int targetCount = 12;
int recordsToCreate = Math.max(0, targetCount - existingCount);
// Create additional A records if needed. Each record gets its own name, so the order by name
// is unambiguous (Cloudflare doesn't guarantee a stable order of records with the same name).
for (int i = 1; i <= recordsToCreate; i++) {
String name = String.format("%s-%02d.%s", pagingSld, i, ZONE_STR); String name = String.format("%s-%02d.%s", pagingSld, i, ZONE_STR);
RecordEntity record = RecordEntity.build(name, RecordType.A, TTL, "127.0.0." + i); RecordEntity record = RecordEntity.build(name, RecordType.A, TTL, "127.0.0." + i);
RecordEntity created = client.recordCreate(zone, record); RecordEntity created = client.recordCreate(zone, record);
createdRecords.add(created); createdRecords.add(created);
assertNotNull(created.getId()); assertNotNull(created.getId());
createdIds.add(created.getId());
} }
// Test paging with page size of 5 PagedResult pagedResult = pageThrough(zone, createdIds, 5);
PagingRequest page1Request = PagingRequest.forRecords(1, 5); List<String> pagedIds = pagedResult.pages().stream()
List<RecordEntity> page1Records = client.recordList(zone, page1Request); .flatMap(List::stream)
assertEquals(5, page1Records.size(), "First page should contain 5 records"); .map(RecordEntity::getId)
.toList();
Set<String> pagedIdSet = new HashSet<>(pagedIds);
// 2nd page should also contain 5 records (if we have at least 12 total) for (int page = 1; page <= pagedResult.pages().size(); page++) {
PagingRequest page2Request = PagingRequest.forRecords(2, 5); int expected = Math.max(0, Math.min(5,
List<RecordEntity> page2Records = client.recordList(zone, page2Request); pagedResult.total() - 5 * (page - 1)));
assertEquals(5, page2Records.size(), "Second page should contain at least 5 records"); assertEquals(expected, pagedResult.pages().get(page - 1).size(),
"Page " + page + " should contain " + expected + " records");
}
assertTrue(pagedResult.pages().get(pagedResult.pages().size() - 1).isEmpty(),
"The page after the last result page should be empty");
assertEquals(pagedIds.size(), pagedIdSet.size(), "Pages should not overlap");
assertEquals(pagedResult.allIds(), pagedIdSet,
"Pages should contain every record in the zone");
assertTrue(pagedResult.allIds().containsAll(createdIds),
"The paged records should contain all records created by this test");
// 3rd page should contain 2 records List<String> createdNamesInPageOrder = pagedResult.pages().stream()
PagingRequest page3Request = PagingRequest.forRecords(3, 5); .flatMap(List::stream)
List<RecordEntity> page3Records = client.recordList(zone, page3Request); .filter(record -> createdIds.contains(record.getId()))
assertEquals(2, page3Records.size(), "Third page should contain 2 records"); .map(RecordEntity::getName)
.toList();
// Verify no overlap between pages assertEquals(createdRecords.stream().map(RecordEntity::getName).toList(),
List<String> page1Ids = page1Records.stream().map(RecordEntity::getId).toList(); createdNamesInPageOrder, "The test records should be sorted by name");
List<String> page2Ids = page2Records.stream().map(RecordEntity::getId).toList();
List<String> page3Ids = page3Records.stream().map(RecordEntity::getId).toList();
Set<String> generatedRecordIds = new HashSet<>(page1Ids);
generatedRecordIds.addAll(page2Ids);
generatedRecordIds.addAll(page3Ids);
assertEquals(createdRecords.size(), generatedRecordIds.size());
// Verify our created records are in the zone
List<RecordEntity> allRecords = client.recordList(zone);
Set<String> allRecordIds = allRecords.stream().map(RecordEntity::getId).collect(Collectors.toSet());
assertEquals(createdRecords.size(), allRecordIds.size());
assertTrue(allRecordIds.containsAll(generatedRecordIds));
// Without PagingRequest all pages must be fetched, also if the page size is small // Without PagingRequest all pages must be fetched, also if the page size is small
try (CfDnsClient smallPageClient = new CfDnsClient(true, CfDnsClientBuilder.DEFAULT_BASEURL, try (CfDnsClient smallPageClient = new CfDnsClient(true, CfDnsClientBuilder.DEFAULT_BASEURL,
new CfDnsClientBuilder.ApiTokenAuth(API_TOKEN), 5)) { new CfDnsClientBuilder.ApiTokenAuth(API_TOKEN), 5)) {
// a new client (connection) may see a lagging state of the record list for a short time List<RecordEntity> allRecords = awaitRecords(() -> smallPageClient.recordList(zone),
Set<String> smallPageIds = awaitRecords(() -> smallPageClient.recordList(zone), recs -> ids(recs).containsAll(createdIds));
recs -> recs.size() >= allRecordIds.size()).stream() assertTrue(ids(allRecords).containsAll(createdIds));
.map(RecordEntity::getId).collect(Collectors.toSet()); List<RecordEntity> aRecords = awaitRecords(
assertEquals(allRecordIds, smallPageIds);
assertEquals(createdRecords.size(), awaitRecords(
() -> smallPageClient.recordListByType(zone, RecordType.A), () -> smallPageClient.recordListByType(zone, RecordType.A),
recs -> recs.size() >= createdRecords.size()).size()); recs -> ids(recs).containsAll(createdIds));
assertTrue(ids(aRecords).containsAll(createdIds));
assertEquals(1, smallPageClient.recordListBySubdomain(zone, pagingSld + "-01").size()); assertEquals(1, smallPageClient.recordListBySubdomain(zone, pagingSld + "-01").size());
} }
} finally { } finally {
@@ -550,6 +548,47 @@ public class CfClientIntegrationTest {
} }
} }
private static Set<String> ids(List<RecordEntity> records) {
return records.stream().map(RecordEntity::getId).collect(Collectors.toSet());
}
private PagedResult pageThrough(ZoneEntity zone, Set<String> createdIds, int pageSize)
throws CloudflareApiException, InterruptedException {
PagedResult result = null;
for (int attempt = 1; attempt <= 3; attempt++) {
List<RecordEntity> allRecords = awaitRecords(() -> client.recordList(zone),
records -> ids(records).containsAll(createdIds));
int total = allRecords.size();
Set<String> allIds = ids(allRecords);
List<List<RecordEntity>> pages = new ArrayList<>();
List<String> pagedIds = new ArrayList<>();
int pageCount = (total + pageSize - 1) / pageSize + 1;
boolean pageSizesMatch = true;
for (int page = 1; page <= pageCount; page++) {
int currentPage = page;
int expected = Math.max(0, Math.min(pageSize, total - pageSize * (page - 1)));
List<RecordEntity> pageRecords = awaitRecords(
() -> client.recordList(zone, PagingRequest.forRecords(currentPage, pageSize)),
records -> records.size() == expected);
pages.add(pageRecords);
pagedIds.addAll(pageRecords.stream().map(RecordEntity::getId).toList());
if (pageRecords.size() != expected) {
pageSizesMatch = false;
}
}
result = new PagedResult(total, allIds, pages);
if (pageSizesMatch && new HashSet<>(pagedIds).equals(allIds)) {
return result;
}
if (attempt < 3) {
Thread.sleep(1000);
}
}
return result;
}
@Test @Test
void testApiErrorDetails() throws Exception { void testApiErrorDetails() throws Exception {
ZoneEntity zone = client.zoneGet(ZONE_STR); ZoneEntity zone = client.zoneGet(ZONE_STR);
@@ -787,6 +826,9 @@ public class CfClientIntegrationTest {
} }
} catch (CloudflareNotFoundException e) { } catch (CloudflareNotFoundException e) {
recs = List.of(); recs = List.of();
if (condition.test(recs)) {
return recs;
}
} }
log.info("Records not yet listed (attempt {}), waiting ...", attempt); log.info("Records not yet listed (attempt {}), waiting ...", attempt);
Thread.sleep(1000); Thread.sleep(1000);