Commit 38e63c6a authored by Jason Song's avatar Jason Song Committed by GitHub

Use awaitility to stabilize the async tests (#3099)

parent 552bdfda
......@@ -32,6 +32,7 @@ import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import static org.awaitility.Awaitility.await;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
......@@ -334,10 +335,9 @@ public class NotificationControllerV2Test {
//in batch mode, at most one of them should have result
assertFalse(deferredResult.hasResult() && anotherDeferredResult.hasResult());
TimeUnit.MILLISECONDS.sleep(someBatchInterval * 10);
//now both of them should have result
assertTrue(deferredResult.hasResult() && anotherDeferredResult.hasResult());
await().atMost(someBatchInterval * 500, TimeUnit.MILLISECONDS).untilAsserted(
() -> assertTrue(deferredResult.hasResult() && anotherDeferredResult.hasResult()));
}
@Test
......
......@@ -93,7 +93,7 @@ public abstract class AbstractBaseIntegrationTest {
}
protected void periodicSendMessage(ExecutorService executorService, String message, AtomicBoolean stop) {
executorService.submit((Runnable) () -> {
executorService.submit(() -> {
//wait for the request connected to server
while (!stop.get() && !Thread.currentThread().isInterrupted()) {
try {
......
......@@ -3,6 +3,7 @@ package com.ctrip.framework.apollo.configservice.service;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.when;
import static org.awaitility.Awaitility.*;
import com.ctrip.framework.apollo.biz.config.BizConfig;
import com.ctrip.framework.apollo.biz.entity.AccessKey;
......@@ -10,6 +11,7 @@ import com.ctrip.framework.apollo.biz.repository.AccessKeyRepository;
import com.google.common.collect.Lists;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.awaitility.Awaitility;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -40,6 +42,10 @@ public class AccessKeyServiceWithCacheTest {
when(bizConfig.accessKeyCacheScanIntervalTimeUnit()).thenReturn(scanIntervalTimeUnit);
when(bizConfig.accessKeyCacheRebuildInterval()).thenReturn(scanInterval);
when(bizConfig.accessKeyCacheRebuildIntervalTimeUnit()).thenReturn(scanIntervalTimeUnit);
Awaitility.reset();
Awaitility.setDefaultTimeout(scanInterval * 100, scanIntervalTimeUnit);
Awaitility.setDefaultPollInterval(scanInterval, scanIntervalTimeUnit);
}
@Test
......@@ -63,8 +69,7 @@ public class AccessKeyServiceWithCacheTest {
when(accessKeyRepository.findAllById(anyList()))
.thenReturn(Lists.newArrayList(firstAccessKey, secondAccessKey));
scanIntervalTimeUnit.sleep(scanInterval * 10);
assertThat(accessKeyServiceWithCache.getAvailableSecrets(appId)).isEmpty();
await().untilAsserted(() -> assertThat(accessKeyServiceWithCache.getAvailableSecrets(appId)).isEmpty());
// Update access key, enable both of them
firstAccessKey = assembleAccessKey(1L, appId, "secret-1", true, false, 1577808002000L);
......@@ -74,8 +79,8 @@ public class AccessKeyServiceWithCacheTest {
when(accessKeyRepository.findAllById(anyList()))
.thenReturn(Lists.newArrayList(firstAccessKey, secondAccessKey));
scanIntervalTimeUnit.sleep(scanInterval * 10);
assertThat(accessKeyServiceWithCache.getAvailableSecrets(appId)).containsExactly("secret-1", "secret-2");
await().untilAsserted(() -> assertThat(accessKeyServiceWithCache.getAvailableSecrets(appId))
.containsExactly("secret-1", "secret-2"));
// Update access key, disable the first one
firstAccessKey = assembleAccessKey(1L, appId, "secret-1", false, false, 1577808004000L);
......@@ -84,15 +89,15 @@ public class AccessKeyServiceWithCacheTest {
when(accessKeyRepository.findAllById(anyList()))
.thenReturn(Lists.newArrayList(firstAccessKey, secondAccessKey));
scanIntervalTimeUnit.sleep(scanInterval * 10);
assertThat(accessKeyServiceWithCache.getAvailableSecrets(appId)).containsExactly("secret-2");
await().untilAsserted(() -> assertThat(accessKeyServiceWithCache.getAvailableSecrets(appId))
.containsExactly("secret-2"));
// Delete access key, delete the second one
when(accessKeyRepository.findAllById(anyList()))
.thenReturn(Lists.newArrayList(firstAccessKey));
scanIntervalTimeUnit.sleep(scanInterval * 10);
assertThat(accessKeyServiceWithCache.getAvailableSecrets(appId)).isEmpty();
await().untilAsserted(
() -> assertThat(accessKeyServiceWithCache.getAvailableSecrets(appId)).isEmpty());
// Add new access key in runtime, enable by default
when(accessKeyRepository.findFirst500ByDataChangeLastModifiedTimeGreaterThanOrderByDataChangeLastModifiedTimeAsc(new Date(1577808004000L)))
......@@ -100,8 +105,8 @@ public class AccessKeyServiceWithCacheTest {
when(accessKeyRepository.findAllById(anyList()))
.thenReturn(Lists.newArrayList(firstAccessKey, thirdAccessKey));
scanIntervalTimeUnit.sleep(scanInterval * 10);
assertThat(accessKeyServiceWithCache.getAvailableSecrets(appId)).containsExactly("secret-3");
await().untilAsserted(() -> assertThat(accessKeyServiceWithCache.getAvailableSecrets(appId))
.containsExactly("secret-3"));
}
public AccessKey assembleAccessKey(Long id, String appId, String secret, boolean enabled,
......
......@@ -19,6 +19,7 @@ import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import static org.awaitility.Awaitility.await;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
......@@ -172,19 +173,19 @@ public class ReleaseMessageServiceWithCacheTest {
when(releaseMessageRepository.findFirst500ByIdGreaterThanOrderByIdAsc(someMessageId)).thenReturn(Lists
.newArrayList(newMessage));
scanIntervalTimeUnit.sleep(scanInterval * 10);
await().atMost(scanInterval * 500, scanIntervalTimeUnit).untilAsserted(() -> {
ReleaseMessage newLatestReleaseMsg =
releaseMessageServiceWithCache
.findLatestReleaseMessageForMessages(Sets.newHashSet(someMessageContent));
ReleaseMessage newLatestReleaseMsg =
releaseMessageServiceWithCache
.findLatestReleaseMessageForMessages(Sets.newHashSet(someMessageContent));
List<ReleaseMessage> newLatestReleaseMsgGroupByMsgContent =
releaseMessageServiceWithCache
.findLatestReleaseMessagesGroupByMessages(Sets.newHashSet(someMessageContent));
List<ReleaseMessage> newLatestReleaseMsgGroupByMsgContent =
releaseMessageServiceWithCache
.findLatestReleaseMessagesGroupByMessages(Sets.newHashSet(someMessageContent));
assertEquals(newMessageId, newLatestReleaseMsg.getId());
assertEquals(someMessageContent, newLatestReleaseMsg.getMessage());
assertEquals(newLatestReleaseMsg, newLatestReleaseMsgGroupByMsgContent.get(0));
assertEquals(newMessageId, newLatestReleaseMsg.getId());
assertEquals(someMessageContent, newLatestReleaseMsg.getMessage());
assertEquals(newLatestReleaseMsg, newLatestReleaseMsgGroupByMsgContent.get(0));
});
}
@Test
......
......@@ -290,6 +290,13 @@
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.191</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>4.0.3</version>
<scope>test</scope>
</dependency>
<!-- declare Spring BOMs in order -->
<dependency>
......@@ -362,6 +369,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment