|
17 | 17 | package org.glassfish.jersey.tests.e2e.common.process.internal; |
18 | 18 |
|
19 | 19 | import java.lang.reflect.Type; |
| 20 | +import java.util.concurrent.atomic.AtomicBoolean; |
20 | 21 | import java.util.concurrent.atomic.AtomicInteger; |
21 | 22 | import java.util.function.Consumer; |
22 | 23 |
|
@@ -171,12 +172,67 @@ public void accept(Object o) { |
171 | 172 | Assertions.assertEquals(987654321, instanceRelease.get()); |
172 | 173 | } |
173 | 174 |
|
| 175 | + @Test |
| 176 | + public void testMultipleReleases() throws InterruptedException { |
| 177 | + final RequestScope requestScope = new Hk2RequestScope(); |
| 178 | + final AtomicBoolean passed = new AtomicBoolean(true); |
| 179 | + final int CNT = 200; |
| 180 | + Thread[] thread = new Thread[CNT]; |
| 181 | + Hk2RequestScope.Instance instance = requestScope.runInScope(() -> { |
| 182 | + final Hk2RequestScope.Instance internalInstance = (Hk2RequestScope.Instance) requestScope.current(); |
| 183 | + for (int index = 1; index != CNT; index++) { |
| 184 | + TestProvider testProvider = new TestProvider(String.valueOf(index)) { |
| 185 | + @Override |
| 186 | + public int hashCode() { |
| 187 | + return super.hashCode() + Integer.parseInt(id); |
| 188 | + } |
| 189 | + }; |
| 190 | + final ForeignDescriptor fd = ForeignDescriptor.wrap(testProvider, new Consumer<Object>() { |
| 191 | + @Override |
| 192 | + public void accept(Object o) { |
| 193 | + // noop |
| 194 | + } |
| 195 | + }); |
| 196 | + internalInstance.put(fd, String.valueOf(index)); |
| 197 | + |
| 198 | + for (int i = 0; i != CNT; i++) { |
| 199 | + thread[i] = new Thread(new Runnable() { |
| 200 | + @Override |
| 201 | + public void run() { |
| 202 | + final long waitTime = (int) (Math.random() * 5 + 1) * 10L; |
| 203 | + try { |
| 204 | + Thread.sleep(waitTime); |
| 205 | + } catch (InterruptedException e) { |
| 206 | + throw new RuntimeException(e); |
| 207 | + } |
| 208 | + try { |
| 209 | + internalInstance.release(); |
| 210 | + } catch (Throwable throwable) { |
| 211 | + passed.set(false); |
| 212 | + } |
| 213 | + } |
| 214 | + }); |
| 215 | + } |
| 216 | + for (int i = 0; i != CNT; i++) { |
| 217 | + thread[i].start(); |
| 218 | + } |
| 219 | + } |
| 220 | + return internalInstance; |
| 221 | + }); |
| 222 | + |
| 223 | + for (int i = 0; i != CNT; i++) { |
| 224 | + thread[i].join(); |
| 225 | + } |
| 226 | + |
| 227 | + Assertions.assertTrue(passed.get()); |
| 228 | + } |
| 229 | + |
174 | 230 | /** |
175 | 231 | * Test request scope inhabitant. |
176 | 232 | */ |
177 | 233 | public static class TestProvider extends AbstractActiveDescriptor<String> { |
178 | 234 |
|
179 | | - private final String id; |
| 235 | + public final String id; |
180 | 236 |
|
181 | 237 | public TestProvider(final String id) { |
182 | 238 | super(); |
|
0 commit comments