|
1 | 1 | package dev.openfeature.sdk; |
2 | 2 |
|
| 3 | +import static dev.openfeature.sdk.ProviderEvent.PROVIDER_CONFIGURATION_CHANGED; |
3 | 4 | import static org.assertj.core.api.Assertions.assertThat; |
4 | 5 | import static org.assertj.core.api.Assertions.assertThatCode; |
5 | 6 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
6 | 8 | import static org.mockito.ArgumentMatchers.any; |
7 | 9 | import static org.mockito.Mockito.mock; |
| 10 | +import static org.mockito.Mockito.spy; |
8 | 11 | import static org.mockito.Mockito.times; |
9 | 12 | import static org.mockito.Mockito.verify; |
10 | 13 |
|
11 | 14 | import dev.openfeature.sdk.testutils.testProvider.TestProvider; |
12 | 15 | import java.util.HashMap; |
| 16 | +import java.util.concurrent.CountDownLatch; |
| 17 | +import java.util.concurrent.TimeUnit; |
13 | 18 | import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.DisplayName; |
14 | 20 | import org.junit.jupiter.api.Test; |
15 | 21 |
|
16 | 22 | class OpenFeatureAPITest { |
@@ -115,4 +121,42 @@ void featureProviderTrackIsCalled() throws Exception { |
115 | 121 | verify(featureProvider, times(2)).getMetadata(); |
116 | 122 | verify(featureProvider).track(any(), any(), any()); |
117 | 123 | } |
| 124 | + |
| 125 | + @Test |
| 126 | + @DisplayName("Domainless provider initialized after domain provider - OnConfigurationChanged handlers are invoked for both providers after emitting PROVIDER_CONFIGURATION_CHANGED") |
| 127 | + void onConfigurationChangedAreCalledForDomainlessAndDomainProviders() throws InterruptedException { |
| 128 | + EventProvider domainlessFeatureProvider = spy(EventProvider.class); |
| 129 | + EventProvider featureProvider = spy(EventProvider.class); |
| 130 | + |
| 131 | + Client domainlessClient = api.getClient(); |
| 132 | + Client client = api.getClient("domain"); |
| 133 | + |
| 134 | + CountDownLatch domainlessLatch = new CountDownLatch(1); |
| 135 | + CountDownLatch latch = new CountDownLatch(1); |
| 136 | + |
| 137 | + domainlessClient.onProviderConfigurationChanged( |
| 138 | + eventDetails -> domainlessLatch.countDown() |
| 139 | + ); |
| 140 | + client.onProviderConfigurationChanged( |
| 141 | + eventDetails -> latch.countDown() |
| 142 | + ); |
| 143 | + |
| 144 | + api.setProviderAndWait("domain", featureProvider); |
| 145 | + api.setProviderAndWait(domainlessFeatureProvider); |
| 146 | + |
| 147 | + featureProvider.emit(PROVIDER_CONFIGURATION_CHANGED, mock(ProviderEventDetails.class)); |
| 148 | + domainlessFeatureProvider.emit(PROVIDER_CONFIGURATION_CHANGED, mock(ProviderEventDetails.class)); |
| 149 | + |
| 150 | + boolean domainlessCompleted = domainlessLatch.await(5, TimeUnit.SECONDS); |
| 151 | + assertTrue( |
| 152 | + domainlessCompleted, |
| 153 | + "onProviderConfigurationChanged was not be invoked for a client with default domain" |
| 154 | + ); |
| 155 | + |
| 156 | + boolean completed = latch.await(5, TimeUnit.SECONDS); |
| 157 | + assertTrue( |
| 158 | + completed, |
| 159 | + "onProviderConfigurationChanged was not be invoked for a client with domain 'domain'" |
| 160 | + ); |
| 161 | + } |
118 | 162 | } |
0 commit comments