events: test refactored as Spring Boot test to verify random delay expression and default value in yaml

This commit is contained in:
Sergey Matvienko 2021-07-01 12:26:39 +03:00 committed by Andrew Shvayka
parent efe5677c80
commit 6a7a9ac1ec
2 changed files with 23 additions and 6 deletions

View File

@ -29,6 +29,9 @@ import org.thingsboard.server.service.ttl.AbstractCleanUpService;
@Service
public class EventsCleanUpService extends AbstractCleanUpService {
public static final String RANDOM_DELAY_INTERVAL_MS_EXPRESSION =
"#{T(org.apache.commons.lang3.RandomUtils).nextLong(0, ${sql.ttl.events.execution_interval_ms})}";
@Value("${sql.ttl.events.events_ttl}")
private long ttl;
@ -45,7 +48,7 @@ public class EventsCleanUpService extends AbstractCleanUpService {
this.eventService = eventService;
}
@Scheduled(initialDelayString = "#{T(org.apache.commons.lang3.RandomUtils).nextLong(0, ${sql.ttl.events.execution_interval_ms})}", fixedDelayString = "${sql.ttl.events.execution_interval_ms}")
@Scheduled(initialDelayString = RANDOM_DELAY_INTERVAL_MS_EXPRESSION, fixedDelayString = "${sql.ttl.events.execution_interval_ms}")
public void cleanUp() {
if (ttlTaskExecutionEnabled && isSystemTenantPartitionMine()) {
eventService.cleanupEvents(ttl, debugTtl);

View File

@ -17,20 +17,34 @@ package org.thingsboard.server.service.ttl;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.thingsboard.server.service.ttl.EventsCleanUpService.RANDOM_DELAY_INTERVAL_MS_EXPRESSION;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = EventsCleanUpServiceTest.class)
@Slf4j
public class EventsCleanUpServiceTest {
@Value(RANDOM_DELAY_INTERVAL_MS_EXPRESSION)
long randomDelayMs;
@Value("${sql.ttl.events.execution_interval_ms}")
long executionIntervalMs;
@Test
public void givenInterval_whenRandomDelay_ThenDelayInInterval() {
final long executionIntervalMs = 2220000; //37min
final long randomDelay = org.apache.commons.lang3.RandomUtils.nextLong(0, executionIntervalMs); //same as @Scheduled(initialDelayString = ...
log.info("randomDelay {}", randomDelay);
assertThat(randomDelay, greaterThanOrEqualTo(0L));
assertThat(randomDelay, lessThanOrEqualTo(executionIntervalMs));
log.info("randomDelay {}", randomDelayMs);
log.info("executionIntervalMs {}", executionIntervalMs);
assertThat(executionIntervalMs, is(2220000L));
assertThat(randomDelayMs, greaterThanOrEqualTo(0L));
assertThat(randomDelayMs, lessThanOrEqualTo(executionIntervalMs));
}
}