Commit 6a7a9ac1ec63b2ab879f49fce2ee07b08428fa4b
Committed by
Andrew Shvayka
1 parent
efe5677c
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,6 +29,9 @@ import org.thingsboard.server.service.ttl.AbstractCleanUpService; | ||
29 | @Service | 29 | @Service |
30 | public class EventsCleanUpService extends AbstractCleanUpService { | 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 | @Value("${sql.ttl.events.events_ttl}") | 35 | @Value("${sql.ttl.events.events_ttl}") |
33 | private long ttl; | 36 | private long ttl; |
34 | 37 | ||
@@ -45,7 +48,7 @@ public class EventsCleanUpService extends AbstractCleanUpService { | @@ -45,7 +48,7 @@ public class EventsCleanUpService extends AbstractCleanUpService { | ||
45 | this.eventService = eventService; | 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 | public void cleanUp() { | 52 | public void cleanUp() { |
50 | if (ttlTaskExecutionEnabled && isSystemTenantPartitionMine()) { | 53 | if (ttlTaskExecutionEnabled && isSystemTenantPartitionMine()) { |
51 | eventService.cleanupEvents(ttl, debugTtl); | 54 | eventService.cleanupEvents(ttl, debugTtl); |
@@ -17,20 +17,34 @@ package org.thingsboard.server.service.ttl; | @@ -17,20 +17,34 @@ package org.thingsboard.server.service.ttl; | ||
17 | 17 | ||
18 | import lombok.extern.slf4j.Slf4j; | 18 | import lombok.extern.slf4j.Slf4j; |
19 | import org.junit.Test; | 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 | import static org.hamcrest.MatcherAssert.assertThat; | 25 | import static org.hamcrest.MatcherAssert.assertThat; |
22 | import static org.hamcrest.Matchers.greaterThanOrEqualTo; | 26 | import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
27 | +import static org.hamcrest.Matchers.is; | ||
23 | import static org.hamcrest.Matchers.lessThanOrEqualTo; | 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 | @Slf4j | 33 | @Slf4j |
26 | public class EventsCleanUpServiceTest { | 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 | @Test | 41 | @Test |
29 | public void givenInterval_whenRandomDelay_ThenDelayInInterval() { | 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 | } |