|
16 | 16 |
|
17 | 17 | package org.springframework.boot.security.servlet;
|
18 | 18 |
|
| 19 | +import java.lang.Thread.UncaughtExceptionHandler; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | +import java.util.concurrent.atomic.AtomicBoolean; |
19 | 23 | import java.util.function.Supplier;
|
20 | 24 |
|
21 | 25 | import javax.servlet.http.HttpServletRequest;
|
|
26 | 30 | import org.springframework.context.ApplicationContext;
|
27 | 31 | import org.springframework.mock.web.MockHttpServletRequest;
|
28 | 32 | import org.springframework.mock.web.MockServletContext;
|
| 33 | +import org.springframework.util.ReflectionUtils; |
29 | 34 | import org.springframework.web.context.WebApplicationContext;
|
30 | 35 | import org.springframework.web.context.support.StaticWebApplicationContext;
|
31 | 36 |
|
@@ -105,6 +110,31 @@ protected boolean matches(HttpServletRequest request, Supplier<ApplicationContex
|
105 | 110 | assertThat(matcher.matches(request)).isFalse();
|
106 | 111 | }
|
107 | 112 |
|
| 113 | + @Test // gh-18211 |
| 114 | + public void matchesWhenConcurrentlyCalledWaitsForInitialize() { |
| 115 | + ConcurrentApplicationContextRequestMatcher matcher = new ConcurrentApplicationContextRequestMatcher(); |
| 116 | + StaticWebApplicationContext context = createWebApplicationContext(); |
| 117 | + Runnable target = () -> matcher.matches(new MockHttpServletRequest(context.getServletContext())); |
| 118 | + List<Thread> threads = new ArrayList<>(); |
| 119 | + AssertingUncaughtExceptionHandler exceptionHandler = new AssertingUncaughtExceptionHandler(); |
| 120 | + for (int i = 0; i < 2; i++) { |
| 121 | + Thread thread = new Thread(target); |
| 122 | + thread.setUncaughtExceptionHandler(exceptionHandler); |
| 123 | + threads.add(thread); |
| 124 | + } |
| 125 | + threads.forEach(Thread::start); |
| 126 | + threads.forEach(this::join); |
| 127 | + exceptionHandler.assertNoExceptions(); |
| 128 | + } |
| 129 | + |
| 130 | + private void join(Thread thread) { |
| 131 | + try { |
| 132 | + thread.join(1000); |
| 133 | + } |
| 134 | + catch (InterruptedException ex) { |
| 135 | + } |
| 136 | + } |
| 137 | + |
108 | 138 | private StaticWebApplicationContext createWebApplicationContext() {
|
109 | 139 | StaticWebApplicationContext context = new StaticWebApplicationContext();
|
110 | 140 | MockServletContext servletContext = new MockServletContext();
|
@@ -160,4 +190,47 @@ public Supplier<C> getProvidedContext() {
|
160 | 190 |
|
161 | 191 | }
|
162 | 192 |
|
| 193 | + static class ConcurrentApplicationContextRequestMatcher extends ApplicationContextRequestMatcher<Object> { |
| 194 | + |
| 195 | + ConcurrentApplicationContextRequestMatcher() { |
| 196 | + super(Object.class); |
| 197 | + } |
| 198 | + |
| 199 | + private AtomicBoolean initialized = new AtomicBoolean(); |
| 200 | + |
| 201 | + @Override |
| 202 | + protected void initialized(Supplier<Object> context) { |
| 203 | + try { |
| 204 | + Thread.sleep(200); |
| 205 | + } |
| 206 | + catch (InterruptedException ex) { |
| 207 | + } |
| 208 | + this.initialized.set(true); |
| 209 | + } |
| 210 | + |
| 211 | + @Override |
| 212 | + protected boolean matches(HttpServletRequest request, Supplier<Object> context) { |
| 213 | + assertThat(this.initialized.get()).isTrue(); |
| 214 | + return true; |
| 215 | + } |
| 216 | + |
| 217 | + } |
| 218 | + |
| 219 | + private static class AssertingUncaughtExceptionHandler implements UncaughtExceptionHandler { |
| 220 | + |
| 221 | + private volatile Throwable ex; |
| 222 | + |
| 223 | + @Override |
| 224 | + public void uncaughtException(Thread thead, Throwable ex) { |
| 225 | + this.ex = ex; |
| 226 | + } |
| 227 | + |
| 228 | + public void assertNoExceptions() { |
| 229 | + if (this.ex != null) { |
| 230 | + ReflectionUtils.rethrowRuntimeException(this.ex); |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + } |
| 235 | + |
163 | 236 | }
|
0 commit comments