|
11 | 11 |
|
12 | 12 | import java.util.ArrayList; |
13 | 13 | import java.util.List; |
| 14 | +import java.util.concurrent.CountDownLatch; |
| 15 | +import java.util.concurrent.TimeUnit; |
| 16 | +import java.util.concurrent.atomic.AtomicReference; |
14 | 17 |
|
15 | 18 | import org.junit.jupiter.api.AfterEach; |
16 | 19 | import org.junit.jupiter.api.BeforeEach; |
17 | 20 | import org.junit.jupiter.api.Test; |
18 | 21 | import org.openmrs.api.APIException; |
| 22 | +import org.openmrs.api.PatientService; |
19 | 23 | import org.openmrs.test.jupiter.BaseContextSensitiveTest; |
20 | 24 | import org.openmrs.util.DatabaseUpdateException; |
21 | 25 | import org.openmrs.util.InputRequiredException; |
22 | 26 |
|
| 27 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
23 | 28 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
24 | 29 | import static org.junit.jupiter.api.Assertions.assertNull; |
25 | 30 | import static org.junit.jupiter.api.Assertions.assertThrows; |
@@ -102,4 +107,86 @@ public void getModuleOpenmrsServices_shouldRaiseApiExceptionWithNullClassInstanc |
102 | 107 | verify(spiedServiceContext, never()).getMessageService(); |
103 | 108 | verify(spiedServiceContext, never()).getMessageSourceService(); |
104 | 109 | } |
| 110 | + |
| 111 | + /** |
| 112 | + * @see ServiceContext#getService(Class) |
| 113 | + */ |
| 114 | + @Test |
| 115 | + public void getService_shouldReturnServiceOnTheFastPathWhenNoRefreshIsInProgress() { |
| 116 | + assertFalse(serviceContext.isRefreshingContext()); |
| 117 | + |
| 118 | + assertNotNull(serviceContext.getService(PatientService.class)); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @see ServiceContext#getService(Class) |
| 123 | + */ |
| 124 | + @Test |
| 125 | + public void getService_shouldBlockUntilAnInProgressRefreshCompletes() throws Exception { |
| 126 | + AtomicReference<Object> retrievedService = new AtomicReference<>(); |
| 127 | + AtomicReference<Throwable> thrown = new AtomicReference<>(); |
| 128 | + CountDownLatch lookupStarted = new CountDownLatch(1); |
| 129 | + |
| 130 | + serviceContext.startRefreshingContext(); |
| 131 | + |
| 132 | + Thread lookup = new Thread(() -> { |
| 133 | + lookupStarted.countDown(); |
| 134 | + try { |
| 135 | + retrievedService.set(serviceContext.getService(PatientService.class)); |
| 136 | + } catch (Throwable t) { |
| 137 | + thrown.set(t); |
| 138 | + } |
| 139 | + }); |
| 140 | + lookup.start(); |
| 141 | + |
| 142 | + try { |
| 143 | + // wait for the lookup thread to reach getService, then give it time to block |
| 144 | + assertTrue(lookupStarted.await(5, TimeUnit.SECONDS)); |
| 145 | + Thread.sleep(500); |
| 146 | + |
| 147 | + // the lookup must not have returned while the refresh is in progress |
| 148 | + assertNull(retrievedService.get()); |
| 149 | + assertTrue(lookup.isAlive()); |
| 150 | + } finally { |
| 151 | + serviceContext.doneRefreshingContext(); |
| 152 | + } |
| 153 | + |
| 154 | + // once the refresh finishes, the blocked lookup should complete |
| 155 | + lookup.join(5000); |
| 156 | + assertFalse(lookup.isAlive()); |
| 157 | + assertNull(thrown.get()); |
| 158 | + assertNotNull(retrievedService.get()); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * @see ServiceContext#setService(Class, Object) |
| 163 | + */ |
| 164 | + @Test |
| 165 | + public void setService_shouldMakeRegistrationsVisibleToOtherThreads() throws Exception { |
| 166 | + serviceContext.setService(RegistryVisibilityService.class, new RegistryVisibilityServiceImpl()); |
| 167 | + |
| 168 | + AtomicReference<Object> retrievedService = new AtomicReference<>(); |
| 169 | + AtomicReference<Throwable> thrown = new AtomicReference<>(); |
| 170 | + |
| 171 | + Thread reader = new Thread(() -> { |
| 172 | + try { |
| 173 | + retrievedService.set(serviceContext.getService(RegistryVisibilityService.class)); |
| 174 | + } catch (Throwable t) { |
| 175 | + thrown.set(t); |
| 176 | + } |
| 177 | + }); |
| 178 | + reader.start(); |
| 179 | + reader.join(5000); |
| 180 | + |
| 181 | + assertNull(thrown.get()); |
| 182 | + assertNotNull(retrievedService.get()); |
| 183 | + assertTrue(retrievedService.get() instanceof RegistryVisibilityService); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Simple service interface used to verify that registrations are safely published across threads. |
| 188 | + */ |
| 189 | + public interface RegistryVisibilityService {} |
| 190 | + |
| 191 | + public static class RegistryVisibilityServiceImpl implements RegistryVisibilityService {} |
105 | 192 | } |
0 commit comments