Skip to content

Commit d07caef

Browse files
Shawyeokclaude
andcommitted
test: fix flaky SpringAnnotationCompatibilityTest
The test asserted that the first ApolloConfigChangeEvent received by the ApplicationListener probe is for namespace 'application'. However, config change listeners are notified asynchronously on a shared thread pool (AbstractConfig#notifyAsync), so events from different namespaces may arrive in any order, occasionally failing CI with: expected:<application[]> but was:<application[.yaml]> Replace the order-sensitive FIFO assertion with an order-independent check that all expected namespaces are eventually observed, matching the approach already used in ApolloSpringBootCompatibilityTest. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent a9a6edc commit d07caef

2 files changed

Lines changed: 14 additions & 9 deletions

File tree

apollo-compat-tests/apollo-spring-compat-it/src/test/java/com/ctrip/framework/apollo/compat/spring/SpringAnnotationCompatibilityTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,13 @@ public void shouldSupportAnnotationAndMultipleConfig() throws Exception {
122122
assertNotNull(anotherAppChange);
123123
assertNotNull(anotherAppChange.getChange("compat.origin"));
124124

125-
String namespace = apolloEventListenerProbe.pollNamespace(10, TimeUnit.SECONDS);
126-
assertEquals("application", namespace);
125+
// config change listeners are notified asynchronously, so events from different
126+
// namespaces may arrive in any order
127+
SpringCompatibilityTestSupport.waitForCondition(
128+
"ApplicationListener should receive namespace updates",
129+
() -> apolloEventListenerProbe.hasNamespace("application")
130+
&& apolloEventListenerProbe.hasNamespace("TEST1.apollo")
131+
&& apolloEventListenerProbe.hasNamespace("application.yaml"));
127132

128133
SpringCompatibilityTestSupport.waitForCondition("public value should be updated",
129134
() -> "from-public-updated".equals(

apollo-compat-tests/apollo-spring-compat-it/src/test/java/com/ctrip/framework/apollo/compat/spring/SpringApolloEventListenerProbe.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,24 @@
1717
package com.ctrip.framework.apollo.compat.spring;
1818

1919
import com.ctrip.framework.apollo.spring.events.ApolloConfigChangeEvent;
20-
import java.util.concurrent.BlockingQueue;
21-
import java.util.concurrent.LinkedBlockingQueue;
22-
import java.util.concurrent.TimeUnit;
20+
import java.util.Collections;
21+
import java.util.HashSet;
22+
import java.util.Set;
2323
import org.springframework.context.ApplicationEvent;
2424
import org.springframework.context.ApplicationListener;
2525

2626
public class SpringApolloEventListenerProbe implements ApplicationListener {
2727

28-
private final BlockingQueue<String> namespaces = new LinkedBlockingQueue<String>();
28+
private final Set<String> namespaces = Collections.synchronizedSet(new HashSet<String>());
2929

3030
@Override
3131
public void onApplicationEvent(ApplicationEvent event) {
3232
if (event instanceof ApolloConfigChangeEvent) {
33-
namespaces.offer(((ApolloConfigChangeEvent) event).getConfigChangeEvent().getNamespace());
33+
namespaces.add(((ApolloConfigChangeEvent) event).getConfigChangeEvent().getNamespace());
3434
}
3535
}
3636

37-
public String pollNamespace(long timeout, TimeUnit unit) throws InterruptedException {
38-
return namespaces.poll(timeout, unit);
37+
public boolean hasNamespace(String namespace) {
38+
return namespaces.contains(namespace);
3939
}
4040
}

0 commit comments

Comments
 (0)