Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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.getInternalNotificationCenter(projectConfigManager.getSDKKey()).
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-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.getInternalNotificationCenter(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,9 @@ public interface ProjectConfigManager {
* @return ProjectConfig
*/
ProjectConfig getConfig();

default String getSDKKey() {
return null;
}
}

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 getInternalNotificationCenter(@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();
}

}
20 changes: 1 addition & 19 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 @@ -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
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.getInternalNotificationCenter("ValidProjectConfigV4");
NotificationCenter userNotificationCenter = testProjectConfigManager.getNotificationCenter();
assertNotEquals(registryDefaultNotificationCenter, userNotificationCenter);

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

NotificationRegistry.getInternalNotificationCenter("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 null;
}
return projectConfig.getSdkKey();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
*
* 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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.assertEquals;


public class NotificationRegistryTest {

@SuppressFBWarnings("NP_NONNULL_PARAM_VIOLATION")
@Test
public void getSameNotificationcenterWhenSDKkeyIsNull() {
String sdkKey = null;
NotificationCenter notificationCenter1 = NotificationRegistry.getInternalNotificationCenter(sdkKey);
NotificationCenter notificationCenter2 = NotificationRegistry.getInternalNotificationCenter(sdkKey);
assertEquals(notificationCenter1, notificationCenter2);
}

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

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

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

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

Assert.assertNotEquals(notificationCenter1, notificationCenter2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class HttpProjectConfigManager extends PollingProjectConfigManager {
private final URI uri;
private final String datafileAccessToken;
private String datafileLastModified;
private String sdkKey;

private HttpProjectConfigManager(long period,
TimeUnit timeUnit,
Expand All @@ -73,11 +74,13 @@ private HttpProjectConfigManager(long period,
String datafileAccessToken,
long blockingTimeoutPeriod,
TimeUnit blockingTimeoutUnit,
NotificationCenter notificationCenter) {
NotificationCenter notificationCenter,
String sdkKey) {
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 also avoid this change as well if we can get sdkKey via ProjectConfig?

Copy link
Contributor Author

@mnoman09 mnoman09 Jan 12, 2023

Choose a reason for hiding this comment

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

Same as above. We need to have sdkKey pass in here, to make sure that the when we call getSDKKey on projectConfigManager then we won't get it null in case if it is http config Manager.

super(period, timeUnit, blockingTimeoutPeriod, blockingTimeoutUnit, notificationCenter);
this.httpClient = httpClient;
this.uri = URI.create(url);
this.datafileAccessToken = datafileAccessToken;
this.sdkKey = sdkKey;
}

public URI getUri() {
Expand Down Expand Up @@ -167,6 +170,11 @@ public static Builder builder() {
return new Builder();
}

@Override
public String getSDKKey() {
return this.sdkKey;
}

public static class Builder {
private String datafile;
private String url;
Expand Down Expand Up @@ -357,7 +365,8 @@ public HttpProjectConfigManager build(boolean defer) {
datafileAccessToken,
blockingTimeoutPeriod,
blockingTimeoutUnit,
notificationCenter);
notificationCenter,
sdkKey);

if (datafile != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2019, Optimizely
* Copyright 2019, 2023, Optimizely
*
* 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 @@ -207,6 +207,7 @@ public void testBuildDefer() throws Exception {
.withOptimizelyHttpClient(mockHttpClient)
.withSdkKey("sdk-key")
.build(true);
assertEquals("sdk-key", projectConfigManager.getSDKKey());
}

@Test
Expand Down