Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new tests
Browse files Browse the repository at this point in the history
jaeopt committed Apr 2, 2024
1 parent ca66b36 commit bf66394
Showing 13 changed files with 52 additions and 37 deletions.
4 changes: 2 additions & 2 deletions core-api/src/main/java/com/optimizely/ab/Optimizely.java
Original file line number Diff line number Diff line change
@@ -77,7 +77,6 @@ public class Optimizely implements AutoCloseable {
private static final Logger logger = LoggerFactory.getLogger(Optimizely.class);

final DecisionService decisionService;
@VisibleForTesting
@Deprecated
final EventHandler eventHandler;
@VisibleForTesting
@@ -87,7 +86,8 @@ public class Optimizely implements AutoCloseable {

public final List<OptimizelyDecideOption> defaultDecideOptions;

private final ProjectConfigManager projectConfigManager;
@VisibleForTesting
final ProjectConfigManager projectConfigManager;

@Nullable
private final OptimizelyConfigManager optimizelyConfigManager;
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@
*/
package com.optimizely.ab.event;

import com.optimizely.ab.annotations.VisibleForTesting;
import com.optimizely.ab.config.ProjectConfig;
import com.optimizely.ab.event.internal.EventFactory;
import com.optimizely.ab.event.internal.UserEvent;
@@ -57,7 +58,8 @@ public class BatchEventProcessor implements EventProcessor, AutoCloseable {
private static final Object FLUSH_SIGNAL = new Object();

private final BlockingQueue<Object> eventQueue;
private final EventHandler eventHandler;
@VisibleForTesting
public final EventHandler eventHandler;

final int batchSize;
final long flushInterval;
Original file line number Diff line number Diff line change
@@ -53,8 +53,8 @@ public class ODPEventManager {
// needs to see the change immediately.
private volatile ODPConfig odpConfig;
private EventDispatcherThread eventDispatcherThread;

private final ODPApiManager apiManager;
@VisibleForTesting
public final ODPApiManager apiManager;

// The eventQueue needs to be thread safe. We are not doing anything extra for thread safety here
// because `LinkedBlockingQueue` itself is thread safe.
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@
*/
package com.optimizely.ab.odp;

import com.optimizely.ab.annotations.VisibleForTesting;
import com.optimizely.ab.internal.Cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@
*/
package com.optimizely.ab.odp;

import com.optimizely.ab.annotations.VisibleForTesting;
import com.optimizely.ab.internal.Cache;
import com.optimizely.ab.internal.DefaultLRUCache;
import com.optimizely.ab.odp.parser.ResponseJsonParser;
@@ -31,8 +32,8 @@ public class ODPSegmentManager {
private static final Logger logger = LoggerFactory.getLogger(ODPSegmentManager.class);

private static final String SEGMENT_URL_PATH = "/v3/graphql";

private final ODPApiManager apiManager;
@VisibleForTesting
public final ODPApiManager apiManager;

private volatile ODPConfig odpConfig;

Original file line number Diff line number Diff line change
@@ -41,7 +41,6 @@
public class OptimizelyHttpClient implements Closeable {

private static final Logger logger = LoggerFactory.getLogger(OptimizelyHttpClient.class);

private final CloseableHttpClient httpClient;

OptimizelyHttpClient(CloseableHttpClient httpClient) {
Original file line number Diff line number Diff line change
@@ -58,7 +58,8 @@ public class HttpProjectConfigManager extends PollingProjectConfigManager {

private static final Logger logger = LoggerFactory.getLogger(HttpProjectConfigManager.class);

private final OptimizelyHttpClient httpClient;
@VisibleForTesting
public final OptimizelyHttpClient httpClient;
private final URI uri;
private final String datafileAccessToken;
private String datafileLastModified;
Original file line number Diff line number Diff line change
@@ -30,7 +30,6 @@
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@@ -45,6 +44,7 @@
import java.util.concurrent.TimeUnit;

import javax.annotation.CheckForNull;
import javax.annotation.Nullable;

/**
* {@link EventHandler} implementation that queues events and has a separate pool of threads responsible
@@ -67,7 +67,8 @@ public class AsyncEventHandler implements EventHandler, AutoCloseable {
private static final Logger logger = LoggerFactory.getLogger(AsyncEventHandler.class);
private static final ProjectConfigResponseHandler EVENT_RESPONSE_HANDLER = new ProjectConfigResponseHandler();

private final OptimizelyHttpClient httpClient;
@VisibleForTesting
public final OptimizelyHttpClient httpClient;
private final ExecutorService workerExecutor;

private final long closeTimeout;
Original file line number Diff line number Diff line change
@@ -42,8 +42,10 @@
public class DefaultODPApiManager implements ODPApiManager {
private static final Logger logger = LoggerFactory.getLogger(DefaultODPApiManager.class);

private final OptimizelyHttpClient httpClientSegments;
private final OptimizelyHttpClient httpClientEvents;
@VisibleForTesting
public final OptimizelyHttpClient httpClientSegments;
@VisibleForTesting
public final OptimizelyHttpClient httpClientEvents;

public DefaultODPApiManager() {
this(null);
Original file line number Diff line number Diff line change
@@ -25,12 +25,11 @@
import com.optimizely.ab.event.BatchEventProcessor;
import com.optimizely.ab.internal.PropertyUtils;
import com.optimizely.ab.notification.NotificationCenter;
import org.apache.http.HttpHost;
import org.apache.http.conn.routing.HttpRoutePlanner;
import com.optimizely.ab.odp.DefaultODPApiManager;
import com.optimizely.ab.odp.ODPManager;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -247,21 +246,39 @@ public void newDefaultInstanceWithDatafileAccessToken() throws Exception {

@Test
public void newDefaultInstanceWithDatafileAccessTokenAndCustomHttpClient() throws Exception {
// Add custom Proxy and Port here
int port = 443;
String proxyHostName = "someProxy.com";
HttpHost proxyHost = new HttpHost(proxyHostName, port);
CloseableHttpClient customHttpClient = HttpClients.custom().build();

HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxyHost);

HttpClientBuilder clientBuilder = HttpClients.custom();
clientBuilder = clientBuilder.setRoutePlanner(routePlanner);

CloseableHttpClient httpClient = clientBuilder.build();
String datafileString = Resources.toString(Resources.getResource("valid-project-config-v4.json"), Charsets.UTF_8);
optimizely = OptimizelyFactory.newDefaultInstance("sdk-key", datafileString, "auth-token", httpClient);
optimizely = OptimizelyFactory.newDefaultInstance("sdk-key", datafileString, "auth-token", customHttpClient);
assertTrue(optimizely.isValid());

// HttpProjectConfigManager should be using the customHttpClient

HttpProjectConfigManager projectConfigManager = (HttpProjectConfigManager) optimizely.projectConfigManager;
assert(doesUseCustomHttpClient(projectConfigManager.httpClient, customHttpClient));

// AsyncEventHandler should be using the customHttpClient

BatchEventProcessor eventProcessor = (BatchEventProcessor) optimizely.eventProcessor;
AsyncEventHandler eventHandler = (AsyncEventHandler)eventProcessor.eventHandler;
assert(doesUseCustomHttpClient(eventHandler.httpClient, customHttpClient));

// ODPManager should be using the customHttpClient

ODPManager odpManager = optimizely.getODPManager();
assert odpManager != null;
DefaultODPApiManager odpApiManager = (DefaultODPApiManager) odpManager.getEventManager().apiManager;
assert(doesUseCustomHttpClient(odpApiManager.httpClientSegments, customHttpClient));
assert(doesUseCustomHttpClient(odpApiManager.httpClientEvents, customHttpClient));
}

boolean doesUseCustomHttpClient(OptimizelyHttpClient optimizelyHttpClient, HttpClient customHttpClient) {
if (optimizelyHttpClient == null) {
return false;
}
return optimizelyHttpClient.getHttpClient() == customHttpClient;
}

public ProjectConfigManager projectConfigManagerReturningNull = new ProjectConfigManager() {
@Override
public ProjectConfig getConfig() {
Original file line number Diff line number Diff line change
@@ -20,7 +20,6 @@
import com.google.common.io.Resources;
import com.optimizely.ab.OptimizelyHttpClient;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
@@ -44,7 +43,6 @@
import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.concurrent.TimeUnit.MINUTES;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
Original file line number Diff line number Diff line change
@@ -22,14 +22,9 @@
import com.optimizely.ab.event.internal.payload.EventBatch;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

import java.io.IOException;
import java.util.HashMap;
@@ -38,7 +33,6 @@
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.mockito.runners.MockitoJUnitRunner;

import static com.optimizely.ab.event.AsyncEventHandler.builder;
Original file line number Diff line number Diff line change
@@ -33,7 +33,6 @@
import java.util.List;

import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;

public class DefaultODPApiManagerTest {

0 comments on commit bf66394

Please sign in to comment.