Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
apollo
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
apollo
Commits
38e63c6a
Unverified
Commit
38e63c6a
authored
May 31, 2020
by
Jason Song
Committed by
GitHub
May 31, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use awaitility to stabilize the async tests (#3099)
parent
552bdfda
Changes
6
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
137 additions
and
97 deletions
+137
-97
apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/controller/NotificationControllerV2Test.java
...onfigservice/controller/NotificationControllerV2Test.java
+3
-3
apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/integration/AbstractBaseIntegrationTest.java
...onfigservice/integration/AbstractBaseIntegrationTest.java
+1
-1
apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/service/AccessKeyServiceWithCacheTest.java
.../configservice/service/AccessKeyServiceWithCacheTest.java
+15
-10
apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/service/AppNamespaceServiceWithCacheTest.java
...nfigservice/service/AppNamespaceServiceWithCacheTest.java
+94
-72
apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/service/ReleaseMessageServiceWithCacheTest.java
...igservice/service/ReleaseMessageServiceWithCacheTest.java
+12
-11
pom.xml
pom.xml
+12
-0
No files found.
apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/controller/NotificationControllerV2Test.java
View file @
38e63c6a
...
...
@@ -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
...
...
apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/integration/AbstractBaseIntegrationTest.java
View file @
38e63c6a
...
...
@@ -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
{
...
...
apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/service/AccessKeyServiceWithCacheTest.java
View file @
38e63c6a
...
...
@@ -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
,
...
...
apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/service/AppNamespaceServiceWithCacheTest.java
View file @
38e63c6a
This diff is collapsed.
Click to expand it.
apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/service/ReleaseMessageServiceWithCacheTest.java
View file @
38e63c6a
...
...
@@ -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
...
...
pom.xml
View file @
38e63c6a
...
...
@@ -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>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment