Browse Source

Fix flaky AuditLogControllerTest: use Awaitility for async audit log assertions

Audit logs are saved asynchronously via executor.submit() in AuditLogServiceImpl.
Three tests were asserting log counts immediately after HTTP API calls, creating
a race condition where the last audit log write may not have completed yet.

Replace bare assertions with Awaitility.await().atMost(TIMEOUT, ...).untilAsserted()
in testAuditLogs, testAuditLogs_byTenantIdAndEntityId, and
testAuditLogs_byTenantIdAndEntityId_Sysadmin (confirmed broken: expected 2, got 1).
Also replace the hardcoded 10s timeout in testAuditLogsSysAdmin with the TIMEOUT constant.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
pull/15200/head
Sergey Matvienko 3 months ago
parent
commit
da12ee6dc0
  1. 28
      application/src/test/java/org/thingsboard/server/controller/AuditLogControllerTest.java

28
application/src/test/java/org/thingsboard/server/controller/AuditLogControllerTest.java

@ -113,17 +113,14 @@ public class AuditLogControllerTest extends AbstractControllerTest {
doPost("/api/device", device, Device.class);
}
List<AuditLog> loadedAuditLogs = getAuditLogs(5, "/api/audit/logs?");
Awaitility.await().atMost(TIMEOUT, TimeUnit.SECONDS).untilAsserted(() ->
assertThat(getAuditLogs(5, "/api/audit/logs?")).hasSize(11 + 1));
Assert.assertEquals(11 + 1, loadedAuditLogs.size());
Awaitility.await().atMost(TIMEOUT, TimeUnit.SECONDS).untilAsserted(() ->
assertThat(getAuditLogs(5, "/api/audit/logs/customer/" + ModelConstants.NULL_UUID + "?")).hasSize(11 + 1));
loadedAuditLogs = getAuditLogs(5, "/api/audit/logs/customer/" + ModelConstants.NULL_UUID + "?");
Assert.assertEquals(11 + 1, loadedAuditLogs.size());
loadedAuditLogs = getAuditLogs(5, "/api/audit/logs/user/" + tenantAdmin.getId().getId().toString() + "?");
Assert.assertEquals(11 + 1, loadedAuditLogs.size());
Awaitility.await().atMost(TIMEOUT, TimeUnit.SECONDS).untilAsserted(() ->
assertThat(getAuditLogs(5, "/api/audit/logs/user/" + tenantAdmin.getId().getId().toString() + "?")).hasSize(11 + 1));
}
@Test
@ -138,7 +135,7 @@ public class AuditLogControllerTest extends AbstractControllerTest {
}
int expectedSize = loadedAuditLogsBefore.size() + 3;
Awaitility.await().atMost(10, TimeUnit.SECONDS).untilAsserted(() ->
Awaitility.await().atMost(TIMEOUT, TimeUnit.SECONDS).untilAsserted(() ->
Assert.assertEquals("Have X audit log before this test + New tenant profiles in the test",
expectedSize, getAuditLogs(100, "/api/audit/logs?").size()));
}
@ -170,9 +167,8 @@ public class AuditLogControllerTest extends AbstractControllerTest {
savedDevice = doPost("/api/device", savedDevice, Device.class);
}
List<AuditLog> loadedAuditLogs = getAuditLogs(5, "/api/audit/logs/entity/DEVICE/" + savedDevice.getId().getId() + "?");
Assert.assertEquals(11 + 1, loadedAuditLogs.size());
Awaitility.await().atMost(TIMEOUT, TimeUnit.SECONDS).untilAsserted(() ->
assertThat(getAuditLogs(5, "/api/audit/logs/entity/DEVICE/" + savedDevice.getId().getId() + "?")).hasSize(11 + 1));
}
@Test
@ -188,9 +184,9 @@ public class AuditLogControllerTest extends AbstractControllerTest {
tenantProfile.setName(tenantProfile.getName() + "(old)");
tenantProfile = doPost("/api/tenantProfile", tenantProfile, TenantProfile.class);
List<AuditLog> loadedAuditLogs = getAuditLogs(5, "/api/audit/logs/entity/" +tenantProfile.getId().getEntityType()+ "/" + tenantProfile.getId().getId() + "?");
Assert.assertEquals("Audit logs count by Tenant Profile entity", 2, loadedAuditLogs.size());
Awaitility.await().atMost(TIMEOUT, TimeUnit.SECONDS).untilAsserted(() ->
assertThat(getAuditLogs(5, "/api/audit/logs/entity/" + tenantProfile.getId().getEntityType() + "/" + tenantProfile.getId().getId() + "?"))
.as("Audit logs count by Tenant Profile entity").hasSize(2));
//cleanup
doDelete("/api/tenantProfile/" + tenantProfile.getId().getId().toString());

Loading…
Cancel
Save