|
45 | 45 | import org.mockito.junit.MockitoRule;
|
46 | 46 |
|
47 | 47 | import javax.annotation.Nonnull;
|
| 48 | +import java.io.Closeable; |
48 | 49 | import java.io.IOException;
|
49 | 50 | import java.util.ArrayList;
|
50 | 51 | import java.util.Arrays;
|
|
53 | 54 | import java.util.HashMap;
|
54 | 55 | import java.util.List;
|
55 | 56 | import java.util.Map;
|
56 |
| -import java.util.concurrent.CountDownLatch; |
57 |
| -import java.util.concurrent.TimeUnit; |
58 |
| -import java.util.concurrent.TimeoutException; |
59 | 57 |
|
60 | 58 | import static com.optimizely.ab.config.DatafileProjectConfigTestUtils.*;
|
61 | 59 | import static com.optimizely.ab.config.ValidProjectConfigV4.*;
|
62 | 60 | import static com.optimizely.ab.event.LogEvent.RequestMethod;
|
63 | 61 | import static com.optimizely.ab.notification.DecisionNotification.ExperimentDecisionNotificationBuilder.EXPERIMENT_KEY;
|
64 | 62 | import static com.optimizely.ab.notification.DecisionNotification.ExperimentDecisionNotificationBuilder.VARIATION_KEY;
|
65 | 63 | import static com.optimizely.ab.notification.DecisionNotification.FeatureVariableDecisionNotificationBuilder.*;
|
66 |
| -import static com.optimizely.ab.notification.DecisionNotification.ExperimentDecisionNotificationBuilder.EXPERIMENT_KEY; |
67 |
| -import static com.optimizely.ab.notification.DecisionNotification.ExperimentDecisionNotificationBuilder.VARIATION_KEY; |
68 | 64 | import static java.util.Arrays.asList;
|
69 | 65 | import static junit.framework.TestCase.assertTrue;
|
70 | 66 | import static org.hamcrest.CoreMatchers.is;
|
@@ -148,6 +144,70 @@ public OptimizelyTest(String validDatafile, String noAudienceDatafile) throws Co
|
148 | 144 | this.datafileVersion = Integer.parseInt(validProjectConfig.getVersion());
|
149 | 145 | }
|
150 | 146 |
|
| 147 | + @Test |
| 148 | + public void testClose() throws IOException { |
| 149 | + EventHandler mockCloseableEventHandler = mock( |
| 150 | + EventHandler.class, |
| 151 | + withSettings().extraInterfaces(Closeable.class) |
| 152 | + ); |
| 153 | + ProjectConfigManager mockCloseableProjectConfigManager = mock( |
| 154 | + ProjectConfigManager.class, |
| 155 | + withSettings().extraInterfaces(Closeable.class) |
| 156 | + ); |
| 157 | + |
| 158 | + Optimizely optimizely = Optimizely.builder() |
| 159 | + .withEventHandler(mockCloseableEventHandler) |
| 160 | + .withConfigManager(mockCloseableProjectConfigManager) |
| 161 | + .build(); |
| 162 | + |
| 163 | + optimizely.close(); |
| 164 | + |
| 165 | + verify((Closeable) mockCloseableEventHandler).close(); |
| 166 | + verify((Closeable) mockCloseableProjectConfigManager).close(); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + public void testCloseConfigManagerThrowsException() throws IOException { |
| 171 | + EventHandler mockCloseableEventHandler = mock( |
| 172 | + EventHandler.class, |
| 173 | + withSettings().extraInterfaces(Closeable.class) |
| 174 | + ); |
| 175 | + ProjectConfigManager mockCloseableProjectConfigManager = mock( |
| 176 | + ProjectConfigManager.class, |
| 177 | + withSettings().extraInterfaces(Closeable.class) |
| 178 | + ); |
| 179 | + |
| 180 | + Optimizely optimizely = Optimizely.builder() |
| 181 | + .withEventHandler(mockCloseableEventHandler) |
| 182 | + .withConfigManager(mockCloseableProjectConfigManager) |
| 183 | + .build(); |
| 184 | + |
| 185 | + doThrow(new IOException()).when((Closeable) mockCloseableProjectConfigManager).close(); |
| 186 | + logbackVerifier.expectMessage(Level.WARN, "Unexpected exception on trying to close " + mockCloseableProjectConfigManager + "."); |
| 187 | + optimizely.close(); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + public void testCloseEventHandlerThrowsException() throws IOException { |
| 192 | + EventHandler mockCloseableEventHandler = mock( |
| 193 | + EventHandler.class, |
| 194 | + withSettings().extraInterfaces(Closeable.class) |
| 195 | + ); |
| 196 | + ProjectConfigManager mockCloseableProjectConfigManager = mock( |
| 197 | + ProjectConfigManager.class, |
| 198 | + withSettings().extraInterfaces(Closeable.class) |
| 199 | + ); |
| 200 | + |
| 201 | + Optimizely optimizely = Optimizely.builder() |
| 202 | + .withEventHandler(mockCloseableEventHandler) |
| 203 | + .withConfigManager(mockCloseableProjectConfigManager) |
| 204 | + .build(); |
| 205 | + |
| 206 | + doThrow(new IOException()).when((Closeable) mockCloseableEventHandler).close(); |
| 207 | + logbackVerifier.expectMessage(Level.WARN, "Unexpected exception on trying to close " + mockCloseableEventHandler + "."); |
| 208 | + optimizely.close(); |
| 209 | + } |
| 210 | + |
151 | 211 | //======== activate tests ========//
|
152 | 212 |
|
153 | 213 | /**
|
|
0 commit comments