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