From 1886a1601a494ae2e2802aaf7842d08d6a92369b Mon Sep 17 00:00:00 2001 From: =?utf8?q?L=C3=A9o=20Geoffroy?= Date: Mon, 13 Feb 2023 17:42:48 +0100 Subject: [PATCH] SONAR-18386 Add ANALYZE to test to avoid timeout on purge audit --- .../java/org/sonar/db/audit/AuditDaoTest.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/audit/AuditDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/audit/AuditDaoTest.java index 64f9de0d3a2..25d42633dc0 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/audit/AuditDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/audit/AuditDaoTest.java @@ -19,6 +19,8 @@ */ package org.sonar.db.audit; +import java.sql.SQLException; +import java.sql.Statement; import java.util.List; import org.junit.Rule; import org.junit.Test; @@ -26,6 +28,7 @@ import org.sonar.api.impl.utils.TestSystem2; import org.sonar.core.util.UuidFactoryImpl; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.dialect.PostgreSql; import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; @@ -62,6 +65,14 @@ public class AuditDaoTest { @Test public void purge_has_limit() { prepareRowsWithDeterministicCreatedAt(100_001); + //SONAR-18386 In order to prevent database to hang forever, we let it digest the inserted data + if (db.getDbClient().getDatabase().getDialect().getId().equals(PostgreSql.ID)) { + try (Statement statement = db.getSession().getSqlSession().getConnection().createStatement()) { + statement.execute("ANALYZE audits"); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } long purged = testAuditDao.deleteBefore(dbSession, 200_000); assertThat(purged).isEqualTo(100_000); assertThat(db.countRowsOfTable(dbSession, "audits")).isOne(); @@ -73,6 +84,14 @@ public class AuditDaoTest { @Test public void purge_with_threshold() { prepareRowsWithDeterministicCreatedAt(100_000); + //SONAR-18386 In order to prevent database to hang forever, we let it digest the inserted data + if (db.getDbClient().getDatabase().getDialect().getId().equals(PostgreSql.ID)) { + try (Statement statement = db.getSession().getSqlSession().getConnection().createStatement()) { + statement.execute("ANALYZE audits"); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } long purged = testAuditDao.deleteBefore(dbSession, 50_000); assertThat(purged).isEqualTo(49_999); assertThat(db.countRowsOfTable(dbSession, "audits")).isEqualTo(50_001); @@ -141,5 +160,6 @@ public class AuditDaoTest { AuditDto auditDto = AuditTesting.newAuditDto(i); testAuditDao.insert(dbSession, auditDto); } + dbSession.commit(); } } -- 2.39.5