Commit 1037e5d28ed67e8e4eecbeb06cbf5024ea08afe4
1 parent
6cf63eba
events: test refactored as Spring Boot test to verify random delay expression an…
…d default value in yaml
Showing
2 changed files
with
23 additions
and
6 deletions
... | ... | @@ -29,6 +29,9 @@ import org.thingsboard.server.service.ttl.AbstractCleanUpService; |
29 | 29 | @Service |
30 | 30 | public class EventsCleanUpService extends AbstractCleanUpService { |
31 | 31 | |
32 | + public static final String RANDOM_DELAY_INTERVAL_MS_EXPRESSION = | |
33 | + "#{T(org.apache.commons.lang3.RandomUtils).nextLong(0, ${sql.ttl.events.execution_interval_ms})}"; | |
34 | + | |
32 | 35 | @Value("${sql.ttl.events.events_ttl}") |
33 | 36 | private long ttl; |
34 | 37 | |
... | ... | @@ -45,7 +48,7 @@ public class EventsCleanUpService extends AbstractCleanUpService { |
45 | 48 | this.eventService = eventService; |
46 | 49 | } |
47 | 50 | |
48 | - @Scheduled(initialDelayString = "#{T(org.apache.commons.lang3.RandomUtils).nextLong(0, ${sql.ttl.events.execution_interval_ms})}", fixedDelayString = "${sql.ttl.events.execution_interval_ms}") | |
51 | + @Scheduled(initialDelayString = RANDOM_DELAY_INTERVAL_MS_EXPRESSION, fixedDelayString = "${sql.ttl.events.execution_interval_ms}") | |
49 | 52 | public void cleanUp() { |
50 | 53 | if (ttlTaskExecutionEnabled && isSystemTenantPartitionMine()) { |
51 | 54 | eventService.cleanupEvents(ttl, debugTtl); | ... | ... |
... | ... | @@ -17,20 +17,34 @@ package org.thingsboard.server.service.ttl; |
17 | 17 | |
18 | 18 | import lombok.extern.slf4j.Slf4j; |
19 | 19 | import org.junit.Test; |
20 | +import org.junit.runner.RunWith; | |
21 | +import org.springframework.beans.factory.annotation.Value; | |
22 | +import org.springframework.boot.test.context.SpringBootTest; | |
23 | +import org.springframework.test.context.junit4.SpringRunner; | |
20 | 24 | |
21 | 25 | import static org.hamcrest.MatcherAssert.assertThat; |
22 | 26 | import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
27 | +import static org.hamcrest.Matchers.is; | |
23 | 28 | import static org.hamcrest.Matchers.lessThanOrEqualTo; |
29 | +import static org.thingsboard.server.service.ttl.EventsCleanUpService.RANDOM_DELAY_INTERVAL_MS_EXPRESSION; | |
24 | 30 | |
31 | +@RunWith(SpringRunner.class) | |
32 | +@SpringBootTest(classes = EventsCleanUpServiceTest.class) | |
25 | 33 | @Slf4j |
26 | 34 | public class EventsCleanUpServiceTest { |
27 | 35 | |
36 | + @Value(RANDOM_DELAY_INTERVAL_MS_EXPRESSION) | |
37 | + long randomDelayMs; | |
38 | + @Value("${sql.ttl.events.execution_interval_ms}") | |
39 | + long executionIntervalMs; | |
40 | + | |
28 | 41 | @Test |
29 | 42 | public void givenInterval_whenRandomDelay_ThenDelayInInterval() { |
30 | - final long executionIntervalMs = 2220000; //37min | |
31 | - final long randomDelay = org.apache.commons.lang3.RandomUtils.nextLong(0, executionIntervalMs); //same as @Scheduled(initialDelayString = ... | |
32 | - log.info("randomDelay {}", randomDelay); | |
33 | - assertThat(randomDelay, greaterThanOrEqualTo(0L)); | |
34 | - assertThat(randomDelay, lessThanOrEqualTo(executionIntervalMs)); | |
43 | + log.info("randomDelay {}", randomDelayMs); | |
44 | + log.info("executionIntervalMs {}", executionIntervalMs); | |
45 | + assertThat(executionIntervalMs, is(2220000L)); | |
46 | + assertThat(randomDelayMs, greaterThanOrEqualTo(0L)); | |
47 | + assertThat(randomDelayMs, lessThanOrEqualTo(executionIntervalMs)); | |
35 | 48 | } |
49 | + | |
36 | 50 | } | ... | ... |