Skip to content

Commit 6616aaa

Browse files
committed
GH-116 - PublishedEvents now sees events from asynchronous event listeners as well.
The ApplicationListener we deploy to capture events published during a test method execution now uses an InheritableThreadLocal so that events published on threads spawned from the main test execution thread also end up in the PublishedEvents instance prepared for the test method. Also, the registration of that particular event listener avoids duplicate registrations by inspecting the application context in use for the test method execution for an already registered listener, falling back to registering one.
1 parent c4321c5 commit 6616aaa

File tree

5 files changed

+152
-4
lines changed

5 files changed

+152
-4
lines changed

spring-modulith-test/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@
5555
<artifactId>spring-boot-starter-test</artifactId>
5656
<optional>true</optional>
5757
</dependency>
58+
59+
<dependency>
60+
<groupId>org.awaitility</groupId>
61+
<artifactId>awaitility</artifactId>
62+
<scope>test</scope>
63+
</dependency>
5864

5965
</dependencies>
6066
</project>

spring-modulith-test/src/main/java/org/springframework/modulith/test/PublishedEventsParameterResolver.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.springframework.context.ApplicationContext;
2626
import org.springframework.context.ApplicationEvent;
2727
import org.springframework.context.ApplicationListener;
28-
import org.springframework.context.ConfigurableApplicationContext;
28+
import org.springframework.context.support.AbstractApplicationContext;
2929
import org.springframework.test.context.junit.jupiter.SpringExtension;
3030
import org.springframework.util.Assert;
3131
import org.springframework.util.ClassUtils;
@@ -40,7 +40,7 @@ class PublishedEventsParameterResolver implements ParameterResolver, BeforeAllCa
4040
private static final boolean ASSERT_J_PRESENT = ClassUtils.isPresent("org.assertj.core.api.Assert",
4141
PublishedEventsParameterResolver.class.getClassLoader());
4242

43-
private ThreadBoundApplicationListenerAdapter listener = new ThreadBoundApplicationListenerAdapter();
43+
private ThreadBoundApplicationListenerAdapter listener;
4444
private final Function<ExtensionContext, ApplicationContext> lookup;
4545

4646
PublishedEventsParameterResolver() {
@@ -59,7 +59,22 @@ class PublishedEventsParameterResolver implements ParameterResolver, BeforeAllCa
5959
public void beforeAll(ExtensionContext extensionContext) {
6060

6161
ApplicationContext context = lookup.apply(extensionContext);
62-
((ConfigurableApplicationContext) context).addApplicationListener(listener);
62+
63+
if (!(context instanceof AbstractApplicationContext aac)) {
64+
throw new IllegalStateException();
65+
}
66+
67+
listener = aac.getApplicationListeners().stream()
68+
.filter(ThreadBoundApplicationListenerAdapter.class::isInstance)
69+
.map(ThreadBoundApplicationListenerAdapter.class::cast)
70+
.findFirst()
71+
.orElseGet(() -> {
72+
73+
var adapter = new ThreadBoundApplicationListenerAdapter();
74+
aac.addApplicationListener(adapter);
75+
76+
return adapter;
77+
});
6378
}
6479

6580
/*
@@ -114,7 +129,7 @@ public void afterEach(ExtensionContext context) {
114129
*/
115130
private static class ThreadBoundApplicationListenerAdapter implements ApplicationListener<ApplicationEvent> {
116131

117-
private final ThreadLocal<ApplicationListener<ApplicationEvent>> delegate = new ThreadLocal<>();
132+
private final ThreadLocal<ApplicationListener<ApplicationEvent>> delegate = new InheritableThreadLocal<>();
118133

119134
/**
120135
* Registers the given {@link ApplicationListener} to be used for the current thread.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example;
17+
18+
import lombok.RequiredArgsConstructor;
19+
20+
import org.springframework.context.ApplicationEventPublisher;
21+
import org.springframework.context.event.EventListener;
22+
import org.springframework.scheduling.annotation.Async;
23+
import org.springframework.stereotype.Component;
24+
25+
@Component
26+
@RequiredArgsConstructor
27+
public class AsyncEventListener {
28+
29+
private final ApplicationEventPublisher publisher;
30+
31+
@Async
32+
@EventListener
33+
void on(FirstEvent event) {
34+
publisher.publishEvent(new SecondEvent());
35+
}
36+
37+
@Async
38+
@EventListener
39+
void on(SecondEvent event) {
40+
publisher.publishEvent(new ThirdEvent());
41+
}
42+
43+
public static record FirstEvent() {}
44+
45+
static record SecondEvent() {}
46+
47+
public static record ThirdEvent() {}
48+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example;
17+
18+
import org.springframework.boot.autoconfigure.SpringBootApplication;
19+
import org.springframework.scheduling.annotation.EnableAsync;
20+
21+
@EnableAsync(proxyTargetClass = true)
22+
@SpringBootApplication
23+
public class TestConfiguration {}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.modulith.test;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import example.AsyncEventListener;
21+
import example.AsyncEventListener.FirstEvent;
22+
import example.AsyncEventListener.ThirdEvent;
23+
import example.TestConfiguration;
24+
import lombok.RequiredArgsConstructor;
25+
26+
import org.awaitility.Awaitility;
27+
import org.junit.jupiter.api.Test;
28+
import org.junit.jupiter.api.extension.ExtendWith;
29+
import org.springframework.beans.factory.annotation.Autowired;
30+
import org.springframework.boot.test.context.SpringBootTest;
31+
import org.springframework.context.ApplicationEventPublisher;
32+
33+
/**
34+
* Integration tests for {@link PublishedEvents}.
35+
*
36+
* @author Oliver Drotbohm
37+
*/
38+
@RequiredArgsConstructor
39+
@ExtendWith(PublishedEventsExtension.class)
40+
@SpringBootTest(classes = TestConfiguration.class)
41+
class PublishedEventsIntegrationTests {
42+
43+
@Autowired ApplicationEventPublisher publisher;
44+
@Autowired AsyncEventListener listener;
45+
46+
@Test // #116
47+
void capturesEventsTriggeredByAsyncEventListeners(PublishedEvents events) {
48+
49+
assertThatNoException().isThrownBy(() -> {
50+
51+
publisher.publishEvent(new FirstEvent());
52+
53+
Awaitility.await().until(() -> events.eventOfTypeWasPublished(ThirdEvent.class));
54+
});
55+
}
56+
}

0 commit comments

Comments
 (0)