3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.ce.analysis.cache.cleaning;
22 import java.io.ByteArrayInputStream;
23 import java.time.LocalDateTime;
24 import java.time.ZoneOffset;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.api.platform.Server;
28 import org.sonar.api.utils.System2;
29 import org.sonar.core.util.SequenceUuidFactory;
30 import org.sonar.core.util.UuidFactory;
31 import org.sonar.db.DbSession;
32 import org.sonar.db.DbTester;
33 import org.sonar.db.component.SnapshotDto;
34 import org.sonar.db.scannercache.ScannerAnalysisCacheDao;
36 import static java.util.concurrent.TimeUnit.DAYS;
37 import static java.util.concurrent.TimeUnit.SECONDS;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.mockito.ArgumentMatchers.any;
40 import static org.mockito.ArgumentMatchers.anyLong;
41 import static org.mockito.ArgumentMatchers.eq;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.times;
44 import static org.mockito.Mockito.verify;
46 public class AnalysisCacheCleaningSchedulerImplIT {
47 private System2 system2 = mock(System2.class);
48 private final static UuidFactory uuidFactory = new SequenceUuidFactory();
50 public DbTester dbTester = DbTester.create(system2);
51 private DbSession dbSession = dbTester.getSession();
52 private ScannerAnalysisCacheDao scannerAnalysisCacheDao = dbTester.getDbClient().scannerAnalysisCacheDao();
54 AnalysisCacheCleaningExecutorService executorService = mock(AnalysisCacheCleaningExecutorService.class);
56 AnalysisCacheCleaningSchedulerImpl underTest = new AnalysisCacheCleaningSchedulerImpl(executorService, dbTester.getDbClient());
59 public void startSchedulingOnServerStart() {
60 underTest.onServerStart(mock(Server.class));
61 verify(executorService, times(1)).scheduleAtFixedRate(any(Runnable.class), anyLong(), eq(DAYS.toSeconds(1)), eq(SECONDS));
65 public void clean_data_older_than_7_days() {
66 var snapshotDao = dbTester.getDbClient().snapshotDao();
67 var snapshot1 = createSnapshot(LocalDateTime.now().minusDays(1).toInstant(ZoneOffset.UTC).toEpochMilli());
68 snapshotDao.insert(dbSession, snapshot1);
69 scannerAnalysisCacheDao.insert(dbSession, snapshot1.getRootComponentUuid(), new ByteArrayInputStream("data".getBytes()));
70 var snapshot2 = createSnapshot(LocalDateTime.now().minusDays(6).toInstant(ZoneOffset.UTC).toEpochMilli());
71 snapshotDao.insert(dbSession, snapshot2);
72 scannerAnalysisCacheDao.insert(dbSession, snapshot2.getRootComponentUuid(), new ByteArrayInputStream("data".getBytes()));
73 var snapshot3 = createSnapshot(LocalDateTime.now().minusDays(8).toInstant(ZoneOffset.UTC).toEpochMilli());
74 snapshotDao.insert(dbSession, snapshot3);
75 scannerAnalysisCacheDao.insert(dbSession, snapshot3.getRootComponentUuid(), new ByteArrayInputStream("data".getBytes()));
76 var snapshot4 = createSnapshot(LocalDateTime.now().minusDays(30).toInstant(ZoneOffset.UTC).toEpochMilli());
77 snapshotDao.insert(dbSession, snapshot4);
78 scannerAnalysisCacheDao.insert(dbSession, snapshot4.getRootComponentUuid(), new ByteArrayInputStream("data".getBytes()));
80 assertThat(dbTester.countRowsOfTable("scanner_analysis_cache")).isEqualTo(4);
84 assertThat(dbTester.countRowsOfTable("scanner_analysis_cache")).isEqualTo(2);
85 assertThat(scannerAnalysisCacheDao.selectData(dbSession, snapshot1.getRootComponentUuid())).isNotNull();
86 assertThat(scannerAnalysisCacheDao.selectData(dbSession, snapshot2.getRootComponentUuid())).isNotNull();
87 assertThat(scannerAnalysisCacheDao.selectData(dbSession, snapshot3.getRootComponentUuid())).isNull();
88 assertThat(scannerAnalysisCacheDao.selectData(dbSession, snapshot4.getRootComponentUuid())).isNull();
91 private static SnapshotDto createSnapshot(long buildtime) {
92 return new SnapshotDto()
93 .setUuid(uuidFactory.create())
94 .setRootComponentUuid(uuidFactory.create())
97 .setProjectVersion("2.1-SNAPSHOT")
98 .setPeriodMode("days1")
100 .setPeriodDate(buildtime)
101 .setBuildDate(buildtime);