Skip to content

Commit 42da842

Browse files
(fix): Refactored Use of Event Processor (#299)
* Refactored Use of Event Processor * Refactored Use of Event Processor * Update Java-sdk version in gradle.build * Reverted the Java SDK Version * Updated java-sdk version * Updated java-sdk version * update unit tests * setting notificationcenter if sent from optimizelymanager * sending eventProcessor to optimizely * Added Close * hope you don't mind. updated java sdk to get build to pass. :)
1 parent 5dc0797 commit 42da842

File tree

5 files changed

+61
-39
lines changed

5 files changed

+61
-39
lines changed

android-sdk/src/androidTest/java/com/optimizely/ab/android/sdk/OptimizelyManagerBuilderTest.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,50 +16,19 @@
1616

1717
package com.optimizely.ab.android.sdk;
1818

19-
import android.app.UiModeManager;
20-
import android.content.Context;
21-
import android.content.res.Configuration;
22-
import android.os.Build;
23-
import android.support.annotation.RequiresApi;
2419
import android.support.test.InstrumentationRegistry;
25-
import android.support.test.espresso.core.deps.guava.util.concurrent.ListeningExecutorService;
2620
import android.support.test.runner.AndroidJUnit4;
27-
28-
import com.optimizely.ab.Optimizely;
2921
import com.optimizely.ab.OptimizelyRuntimeException;
3022
import com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler;
3123
import com.optimizely.ab.android.event_handler.DefaultEventHandler;
32-
import com.optimizely.ab.android.shared.DatafileConfig;
3324
import com.optimizely.ab.android.user_profile.DefaultUserProfileService;
34-
import com.optimizely.ab.config.Experiment;
35-
import com.optimizely.ab.config.ProjectConfig;
36-
import com.optimizely.ab.config.Variation;
3725
import com.optimizely.ab.error.ErrorHandler;
38-
import com.optimizely.ab.event.internal.payload.Event;
39-
import com.optimizely.ab.notification.NotificationListener;
40-
41-
import org.junit.Assert;
4226
import org.junit.Before;
4327
import org.junit.Test;
4428
import org.junit.runner.RunWith;
45-
import org.mockito.Mock;
46-
import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent;
4729
import org.slf4j.Logger;
48-
import org.slf4j.LoggerFactory;
49-
50-
import java.util.Collections;
51-
import java.util.HashMap;
52-
import java.util.Map;
53-
54-
import static junit.framework.Assert.assertEquals;
55-
import static junit.framework.Assert.assertFalse;
5630
import static junit.framework.Assert.assertNotNull;
57-
import static junit.framework.Assert.assertNull;
58-
import static junit.framework.Assert.assertTrue;
5931
import static org.mockito.Mockito.mock;
60-
import static org.mockito.Mockito.verify;
61-
import static org.mockito.Mockito.verifyZeroInteractions;
62-
import static org.mockito.Mockito.when;
6332

6433
@RunWith(AndroidJUnit4.class)
6534
public class OptimizelyManagerBuilderTest {

android-sdk/src/androidTest/java/com/optimizely/ab/android/sdk/OptimizelyManagerTest.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.optimizely.ab.config.ProjectConfig;
4242
import com.optimizely.ab.config.Variation;
4343
import com.optimizely.ab.event.EventHandler;
44+
import com.optimizely.ab.event.EventProcessor;
4445

4546
import org.junit.Before;
4647
import org.junit.Test;
@@ -96,8 +97,15 @@ public void setup() throws Exception {
9697
executor = MoreExecutors.newDirectExecutorService();
9798
defaultDatafileHandler = mock(DefaultDatafileHandler.class);
9899
EventHandler eventHandler = mock(DefaultEventHandler.class);
99-
optimizelyManager = new OptimizelyManager(testProjectId, null, null, logger, 3600L, defaultDatafileHandler, null, 3600L,
100-
eventHandler, null);
100+
EventProcessor eventProcessor = mock(EventProcessor.class);
101+
optimizelyManager = OptimizelyManager.builder(testProjectId)
102+
.withLogger(logger)
103+
.withDatafileDownloadInterval(3600L)
104+
.withDatafileHandler(defaultDatafileHandler)
105+
.withEventDispatchInterval(3600L)
106+
.withEventHandler(eventHandler)
107+
.withEventProcessor(eventProcessor)
108+
.build(InstrumentationRegistry.getTargetContext());
101109
String datafile = optimizelyManager.getDatafile(InstrumentationRegistry.getTargetContext(), R.raw.datafile);
102110
ProjectConfig config = new DatafileProjectConfig.Builder().withDatafile(datafile).build();
103111

@@ -152,8 +160,9 @@ public void initializeSyncWithEnvironment() {
152160
Logger logger = mock(Logger.class);
153161
DatafileHandler datafileHandler = mock(DefaultDatafileHandler.class);
154162
EventHandler eventHandler = mock(DefaultEventHandler.class);
163+
EventProcessor eventProcessor = mock(EventProcessor.class);
155164
OptimizelyManager optimizelyManager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, 3600L, datafileHandler, null, 3600L,
156-
eventHandler, null);
165+
eventHandler, eventProcessor, null, null);
157166
/*
158167
* Scenario#1: when datafile is not Empty
159168
* Scenario#2: when datafile is Empty
@@ -209,8 +218,9 @@ public void initializeAsyncWithEnvironment() {
209218
Logger logger = mock(Logger.class);
210219
DatafileHandler datafileHandler = mock(DefaultDatafileHandler.class);
211220
EventHandler eventHandler = mock(DefaultEventHandler.class);
221+
EventProcessor eventProcessor = mock(EventProcessor.class);
212222
final OptimizelyManager optimizelyManager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, 3600L, datafileHandler, null, 3600L,
213-
eventHandler, null);
223+
eventHandler, eventProcessor, null, null);
214224

215225
/*
216226
* Scenario#1: when datafile is not Empty

android-sdk/src/main/java/com/optimizely/ab/android/sdk/OptimizelyClient.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424
import com.optimizely.ab.UnknownEventTypeException;
2525
import com.optimizely.ab.config.ProjectConfig;
2626
import com.optimizely.ab.config.Variation;
27+
import com.optimizely.ab.event.EventHandler;
2728
import com.optimizely.ab.notification.DecisionNotification;
2829
import com.optimizely.ab.notification.NotificationCenter;
2930
import com.optimizely.ab.notification.NotificationHandler;
3031
import com.optimizely.ab.notification.TrackNotification;
3132

3233
import org.slf4j.Logger;
3334

35+
import java.io.Closeable;
3436
import java.util.HashMap;
3537
import java.util.List;
3638
import java.util.Map;
@@ -170,6 +172,16 @@ protected void setDefaultAttributes(@NonNull Map<String, ?> attrs) {
170172
}
171173
}
172174

175+
/**
176+
* Checks if eventHandler {@link EventHandler}
177+
* are Closeable {@link Closeable} and calls close on them.
178+
*
179+
* <b>NOTE:</b> There is a chance that this could be long running if the implementations of close are long running.
180+
*/
181+
public void close() {
182+
optimizely.close();
183+
}
184+
173185
/**
174186
* Check that this is a valid instance
175187
*

android-sdk/src/main/java/com/optimizely/ab/android/sdk/OptimizelyManager.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import com.optimizely.ab.config.parser.ConfigParseException;
4444
import com.optimizely.ab.error.ErrorHandler;
4545
import com.optimizely.ab.event.EventHandler;
46+
import com.optimizely.ab.event.EventProcessor;
47+
import com.optimizely.ab.event.ForwardingEventProcessor;
4648
import com.optimizely.ab.event.internal.payload.EventBatch;
4749
import com.optimizely.ab.notification.NotificationCenter;
4850
import com.optimizely.ab.notification.UpdateConfigNotification;
@@ -66,6 +68,8 @@ public class OptimizelyManager {
6668
private final long datafileDownloadInterval;
6769
private final long eventDispatchInterval;
6870
@Nullable private EventHandler eventHandler = null;
71+
@Nullable private EventProcessor eventProcessor = null;
72+
@Nullable private NotificationCenter notificationCenter = null;
6973
@Nullable private ErrorHandler errorHandler;
7074
@NonNull private Logger logger;
7175
@Nullable private final String projectId;
@@ -85,7 +89,9 @@ public class OptimizelyManager {
8589
@Nullable ErrorHandler errorHandler,
8690
long eventDispatchInterval,
8791
@NonNull EventHandler eventHandler,
88-
@NonNull UserProfileService userProfileService) {
92+
@Nullable EventProcessor eventProcessor,
93+
@NonNull UserProfileService userProfileService,
94+
@NonNull NotificationCenter notificationCenter) {
8995

9096
if (projectId == null && sdkKey == null) {
9197
logger.error("projectId and sdkKey are both null!");
@@ -103,8 +109,10 @@ public class OptimizelyManager {
103109
this.datafileHandler = datafileHandler;
104110
this.eventDispatchInterval = eventDispatchInterval;
105111
this.eventHandler = eventHandler;
112+
this.eventProcessor = eventProcessor;
106113
this.errorHandler = errorHandler;
107114
this.userProfileService = userProfileService;
115+
this.notificationCenter = notificationCenter;
108116
}
109117

110118
@VisibleForTesting
@@ -501,6 +509,7 @@ private OptimizelyClient buildOptimizely(@NonNull Context context, @NonNull Stri
501509
Optimizely.Builder builder = Optimizely.builder();
502510

503511
builder.withEventHandler(eventHandler);
512+
builder.withEventProcessor(eventProcessor);
504513

505514
if (datafileHandler instanceof DefaultDatafileHandler) {
506515
DefaultDatafileHandler handler = (DefaultDatafileHandler)datafileHandler;
@@ -519,7 +528,7 @@ private OptimizelyClient buildOptimizely(@NonNull Context context, @NonNull Stri
519528
}
520529

521530
builder.withUserProfileService(userProfileService);
522-
531+
builder.withNotificationCenter(notificationCenter);
523532
Optimizely optimizely = builder.build();
524533
return new OptimizelyClient(optimizely, LoggerFactory.getLogger(OptimizelyClient.class));
525534
}
@@ -644,6 +653,8 @@ public static class Builder {
644653
@Nullable private Logger logger = null;
645654
@Nullable private EventHandler eventHandler = null;
646655
@Nullable private ErrorHandler errorHandler = null;
656+
@Nullable private EventProcessor eventProcessor = null;
657+
@Nullable private NotificationCenter notificationCenter = null;
647658
@Nullable private UserProfileService userProfileService = null;
648659
@Nullable private String sdkKey = null;
649660
@Nullable private DatafileConfig datafileConfig = null;
@@ -748,6 +759,16 @@ public Builder withDatafileConfig(DatafileConfig datafileConfig) {
748759
return this;
749760
}
750761

762+
public Builder withEventProcessor(EventProcessor eventProcessor) {
763+
this.eventProcessor = eventProcessor;
764+
return this;
765+
}
766+
767+
public Builder withNotificationCenter(NotificationCenter notificationCenter) {
768+
this.notificationCenter = notificationCenter;
769+
return this;
770+
}
771+
751772
/**
752773
* Get a new {@link Builder} instance to create {@link OptimizelyManager} with.
753774
* @param context the application context used to create default service if not provided.
@@ -791,6 +812,14 @@ public OptimizelyManager build(Context context) {
791812
eventHandler = DefaultEventHandler.getInstance(context);
792813
}
793814

815+
if(notificationCenter == null) {
816+
notificationCenter = new NotificationCenter();
817+
}
818+
819+
if(eventProcessor == null) {
820+
eventProcessor = new ForwardingEventProcessor(eventHandler, notificationCenter);
821+
}
822+
794823
if (projectId == null && sdkKey == null) {
795824
logger.error("ProjectId and SDKKey cannot both be null");
796825
return null;
@@ -804,7 +833,9 @@ public OptimizelyManager build(Context context) {
804833
errorHandler,
805834
eventDispatchInterval,
806835
eventHandler,
807-
userProfileService);
836+
eventProcessor,
837+
userProfileService,
838+
notificationCenter);
808839
}
809840
}
810841
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ ext {
5353
build_tools_version = "28.0.3"
5454
min_sdk_version = 14
5555
target_sdk_version = 28
56-
java_core_ver = "3.2.1"
56+
java_core_ver = "3.3.1"
5757
android_logger_ver = "1.3.6"
5858
jacksonversion= "2.9.9.1"
5959
support_annotations_ver = "24.2.1"

0 commit comments

Comments
 (0)