Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion core-api/src/main/java/com/optimizely/ab/Optimizely.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.optimizely.ab.event.*;
import com.optimizely.ab.event.internal.*;
import com.optimizely.ab.event.internal.payload.EventBatch;
import com.optimizely.ab.internal.NotificationRegistry;
import com.optimizely.ab.notification.*;
import com.optimizely.ab.odp.*;
import com.optimizely.ab.optimizelyconfig.OptimizelyConfig;
Expand Down Expand Up @@ -127,7 +128,12 @@ private Optimizely(@Nonnull EventHandler eventHandler,
if (getProjectConfig() != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like my previous comment on this line got lost :)
We discussed this blocking "getProjectConfig" while constructing Optimizely is not desirable, so we need to make it return null if not available. @msohailhussain may have a good idea on how to fix it.

updateODPSettings();
}
addUpdateConfigNotificationHandler(configNotification -> { updateODPSettings(); });
if (projectConfigManager != null) {
NotificationRegistry.getNotificationCenter(projectConfigManager.getSDKKey()).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be confusing with with public NotificationCenter. What about changing names to
NotificationRegistry.getInternalNotificationCenter or some other names to avoid confusion?
@msohailhussain What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jaeopt we are getting sdkKey from projectConfigManager instead of getProjectConfig() because of Asynchronous call of httpConfigManager it might not able to add this NotificationHandler when projectConfig is not fetched.

addNotificationHandler(UpdateConfigNotification.class,
configNotification -> { updateODPSettings(); });
}

}
}

Expand All @@ -153,6 +159,7 @@ public void close() {
tryClose(eventProcessor);
tryClose(eventHandler);
tryClose(projectConfigManager);
NotificationRegistry.clearNotificationCenterRegistry();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this safe to clear all notificationCenters? We can clear one for the current sdkKey. @msohailhussain also shared concern about memory leak when we close a notificationCenter, which may be addressed together when we clean it up.

if (odpManager != null) {
tryClose(odpManager);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2019, Optimizely and contributors
* Copyright 2019, 2023, Optimizely and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -27,6 +27,11 @@ public ProjectConfig getConfig() {
return projectConfigReference.get();
}

@Override
public String getSDKKey() {
return "";
}

public void setConfig(ProjectConfig projectConfig) {
projectConfigReference.set(projectConfig);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2019-2020, Optimizely and contributors
* Copyright 2019-2020, 2023, Optimizely and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@
*/
package com.optimizely.ab.config;

import com.optimizely.ab.internal.NotificationRegistry;
import com.optimizely.ab.notification.NotificationCenter;
import com.optimizely.ab.notification.UpdateConfigNotification;
import com.optimizely.ab.optimizelyconfig.OptimizelyConfig;
Expand Down Expand Up @@ -109,6 +110,7 @@ void setConfig(ProjectConfig projectConfig) {
currentProjectConfig.set(projectConfig);
currentOptimizelyConfig.set(new OptimizelyConfigService(projectConfig).getConfig());
countDownLatch.countDown();
NotificationRegistry.getNotificationCenter(projectConfig.getSdkKey()).send(SIGNAL);
notificationCenter.send(SIGNAL);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2019, Optimizely and contributors
* Copyright 2019, 2023, Optimizely and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,5 +23,7 @@ public interface ProjectConfigManager {
* @return ProjectConfig
*/
ProjectConfig getConfig();

String getSDKKey();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get sdkKey via getConfig().getSDKKey() instead of adding the new inferface?
This will be convenient but we want to avoid any breaking changes with this new method here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see this comment. It will make sure that even when the config is not fetched, notificationCenter will contain the handler, which updates the ODPConfig.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mnoman09 Got it! Can we add a default implementation of this method (returning null), so no changes required for existing implementations. They'll all share the same internal notificationCenter for "null" sdkKey (until they fix it), which looks safe to me.
I believe the default interface method is supported in Java8 and also supported in android.

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
*
* Copyright 2023, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.optimizely.ab.internal;

import com.optimizely.ab.notification.NotificationCenter;

import javax.annotation.Nonnull;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class NotificationRegistry {
private final static Map<String, NotificationCenter> _notificationCenters = new ConcurrentHashMap<>();

private NotificationRegistry()
{
}

public static NotificationCenter getNotificationCenter(@Nonnull String sdkKey)
{
if (sdkKey == null) {
sdkKey = "";
}

NotificationCenter notificationCenter;
if (_notificationCenters.containsKey(sdkKey)) {
notificationCenter = _notificationCenters.get(sdkKey);
} else {
notificationCenter = new NotificationCenter();
_notificationCenters.put(sdkKey, notificationCenter);
}
return notificationCenter;
}

public static void clearNotificationCenterRegistry() {
_notificationCenters.clear();
}

}
44 changes: 20 additions & 24 deletions core-api/src/test/java/com/optimizely/ab/OptimizelyTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2016-2022, Optimizely, Inc. and contributors *
* Copyright 2016-2023, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand Down Expand Up @@ -4505,13 +4505,13 @@ public void isValidReturnsTrueWhenClientIsValid() throws Exception {

@Test
public void testGetNotificationCenter() {
Optimizely optimizely = optimizelyBuilder.withConfigManager(() -> null).build();
Optimizely optimizely = optimizelyBuilder.withConfigManager(new TestProjectConfigManager()).build();
assertEquals(optimizely.notificationCenter, optimizely.getNotificationCenter());
}

@Test
public void testAddTrackNotificationHandler() {
Optimizely optimizely = optimizelyBuilder.withConfigManager(() -> null).build();
Optimizely optimizely = optimizelyBuilder.withConfigManager(new TestProjectConfigManager()).build();
NotificationManager<TrackNotification> manager = optimizely.getNotificationCenter()
.getNotificationManager(TrackNotification.class);

Expand All @@ -4521,7 +4521,7 @@ public void testAddTrackNotificationHandler() {

@Test
public void testAddDecisionNotificationHandler() {
Optimizely optimizely = optimizelyBuilder.withConfigManager(() -> null).build();
Optimizely optimizely = optimizelyBuilder.withConfigManager(new TestProjectConfigManager()).build();
NotificationManager<DecisionNotification> manager = optimizely.getNotificationCenter()
.getNotificationManager(DecisionNotification.class);

Expand All @@ -4531,7 +4531,7 @@ public void testAddDecisionNotificationHandler() {

@Test
public void testAddUpdateConfigNotificationHandler() {
Optimizely optimizely = optimizelyBuilder.withConfigManager(() -> null).build();
Optimizely optimizely = optimizelyBuilder.withConfigManager(new TestProjectConfigManager()).build();
NotificationManager<UpdateConfigNotification> manager = optimizely.getNotificationCenter()
.getNotificationManager(UpdateConfigNotification.class);

Expand All @@ -4541,7 +4541,7 @@ public void testAddUpdateConfigNotificationHandler() {

@Test
public void testAddLogEventNotificationHandler() {
Optimizely optimizely = optimizelyBuilder.withConfigManager(() -> null).build();
Optimizely optimizely = optimizelyBuilder.withConfigManager(new TestProjectConfigManager()).build();
NotificationManager<LogEvent> manager = optimizely.getNotificationCenter()
.getNotificationManager(LogEvent.class);

Expand Down Expand Up @@ -4713,24 +4713,6 @@ public void initODPManagerWithProjectConfig() throws IOException {
verify(mockODPManager, times(1)).updateSettings(any(), any(), any());
}

@Test
public void updateODPManagerWhenConfigUpdates() throws IOException {
ODPEventManager mockODPEventManager = mock(ODPEventManager.class);
ODPManager mockODPManager = mock(ODPManager.class);
NotificationCenter mockNotificationCenter = mock(NotificationCenter.class);

Mockito.when(mockODPManager.getEventManager()).thenReturn(mockODPEventManager);
Optimizely.builder()
.withDatafile(validConfigJsonV4())
.withNotificationCenter(mockNotificationCenter)
.withODPManager(mockODPManager)
.build();

verify(mockODPManager, times(1)).updateSettings(any(), any(), any());

Mockito.verify(mockNotificationCenter, times(1)).addNotificationHandler(any(), any());
}

@Test
public void sendODPEvent() {
ProjectConfigManager mockProjectConfigManager = mock(ProjectConfigManager.class);
Expand Down Expand Up @@ -4798,4 +4780,18 @@ public void identifyUser() {
optimizely.identifyUser("the-user");
Mockito.verify(mockODPEventManager, times(1)).identifyUser("the-user");
}

public static class TestProjectConfigManager implements ProjectConfigManager {

@Override
public ProjectConfig getConfig() {
return null;
}

@Override
public String getSDKKey() {
return null;
}
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2019-2021, Optimizely and contributors
* Copyright 2019-2021, 2023, Optimizely and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@
*/
package com.optimizely.ab.config;

import com.optimizely.ab.internal.NotificationRegistry;
import com.optimizely.ab.notification.NotificationCenter;
import com.optimizely.ab.notification.UpdateConfigNotification;
import org.junit.After;
Expand Down Expand Up @@ -95,12 +96,14 @@ public void testBlockingGetConfig() throws Exception {
testProjectConfigManager.release();
TimeUnit.MILLISECONDS.sleep(PROJECT_CONFIG_DELAY);
assertEquals(projectConfig, testProjectConfigManager.getConfig());
assertEquals(projectConfig.getSdkKey(), testProjectConfigManager.getSDKKey());
}

@Test
public void testBlockingGetConfigWithDefault() throws Exception {
testProjectConfigManager.setConfig(projectConfig);
assertEquals(projectConfig, testProjectConfigManager.getConfig());
assertEquals(projectConfig.getSdkKey(), testProjectConfigManager.getSDKKey());
}

@Test
Expand All @@ -124,6 +127,7 @@ public void testGetConfigNotStartedDefault() throws Exception {
testProjectConfigManager.close();
assertFalse(testProjectConfigManager.isRunning());
assertEquals(projectConfig, testProjectConfigManager.getConfig());
assertEquals(projectConfig.getSdkKey(), testProjectConfigManager.getSDKKey());
}

@Test
Expand Down Expand Up @@ -210,11 +214,17 @@ public ProjectConfig poll() {

@Test
public void testUpdateConfigNotificationGetsTriggered() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(1);
CountDownLatch countDownLatch = new CountDownLatch(2);
NotificationCenter registryDefaultNotificationCenter = NotificationRegistry.getNotificationCenter("ValidProjectConfigV4");
NotificationCenter userNotificationCenter = testProjectConfigManager.getNotificationCenter();
assertNotEquals(registryDefaultNotificationCenter, userNotificationCenter);

testProjectConfigManager.getNotificationCenter()
.<UpdateConfigNotification>getNotificationManager(UpdateConfigNotification.class)
.addHandler(message -> {countDownLatch.countDown();});

NotificationRegistry.getNotificationCenter("ValidProjectConfigV4")
.<UpdateConfigNotification>getNotificationManager(UpdateConfigNotification.class)
.addHandler(message -> {countDownLatch.countDown();});
assertTrue(countDownLatch.await(500, TimeUnit.MILLISECONDS));
}

Expand Down Expand Up @@ -271,5 +281,13 @@ public int getCount() {
public void release() {
countDownLatch.countDown();
}

@Override
public String getSDKKey() {
if (projectConfig == null) {
return "";
}
return projectConfig.getSdkKey();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
*
* Copyright 2023, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.optimizely.ab.internal;

import com.optimizely.ab.notification.NotificationCenter;
import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.assertEquals;


public class NotificationRegistryTest {
@Test
public void getSameNotificationcenterWhenSDKkeyIsNull() {
String sdkKey = null;
NotificationCenter notificationCenter1 = NotificationRegistry.getNotificationCenter(sdkKey);
NotificationCenter notificationCenter2 = NotificationRegistry.getNotificationCenter(sdkKey);
assertEquals(notificationCenter1, notificationCenter2);
}

@Test
public void getSameNotificationcenterWhenSDKkeyIsSameButNotNull() {
String sdkKey = "testSDkKey";
NotificationCenter notificationCenter1 = NotificationRegistry.getNotificationCenter(sdkKey);
NotificationCenter notificationCenter2 = NotificationRegistry.getNotificationCenter(sdkKey);
assertEquals(notificationCenter1, notificationCenter2);
}

@Test
public void getSameNotificationcenterWhenSDKkeyIsNullAndAnotherIsEmpty() {
String sdkKey1 = "";
String sdkKey2 = null;
NotificationCenter notificationCenter1 = NotificationRegistry.getNotificationCenter(sdkKey1);
NotificationCenter notificationCenter2 = NotificationRegistry.getNotificationCenter(sdkKey2);
assertEquals(notificationCenter1, notificationCenter2);
}

@Test
public void getDifferentNotificationcenterWhenSDKkeyIsNotSame() {
String sdkKey1 = "testSDkKey1";
String sdkKey2 = "testSDkKey2";
NotificationCenter notificationCenter1 = NotificationRegistry.getNotificationCenter(sdkKey1);
NotificationCenter notificationCenter2 = NotificationRegistry.getNotificationCenter(sdkKey2);
Assert.assertNotEquals(notificationCenter1, notificationCenter2);
}

@Test
public void clearRegistryNotificationcenterClearsOldNotificationCenter() {
String sdkKey1 = "testSDkKey1";
NotificationCenter notificationCenter1 = NotificationRegistry.getNotificationCenter(sdkKey1);
NotificationRegistry.clearNotificationCenterRegistry();
NotificationCenter notificationCenter2 = NotificationRegistry.getNotificationCenter(sdkKey1);

Assert.assertNotEquals(notificationCenter1, notificationCenter2);
}
}
Loading