Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
12 changes: 10 additions & 2 deletions 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 @@ -124,10 +125,15 @@ private Optimizely(@Nonnull EventHandler eventHandler,

if (odpManager != null) {
odpManager.getEventManager().start();
if (getProjectConfig() != null) {
if (!(projectConfigManager instanceof PollingProjectConfigManager)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

PollingProjectConfigManager can also have a preset projectConfig from a fallback datafile. In that case, we have to call updateODPSettings for the projectConfig immediately (we may not get notification later if the fallback is updated already).
I think we can keep the getProjectConfig() != null condition as before, with the change that `getProjectConfig() returns null immediately if config is not available.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

getProjectConfig() != null condition is not possible without wait, with our current implementation. However, we can use the similar approach as we are using in Csharp, adding additional function of getCachedConfig() in ProjectConfigManager. Now the only concern I have, related to this approach, is that do we want to enforce its implementation or return null by default as we are doing for getSDKKey() function?
In my opinion we should enforce it because if anyone uses custom ProjectConfigManager without giving getCachedConfig implementation then they won't be able use ODP as it only gets config using this new function.

Copy link
Contributor

Choose a reason for hiding this comment

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

@mnoman09 Agreed. Let's make them (getSdkKey and getCachedConfig) mandatory changes and keep them in the list of breaking changes for release.

updateODPSettings();
}
addUpdateConfigNotificationHandler(configNotification -> { updateODPSettings(); });
if (projectConfigManager != null && projectConfigManager.getSDKKey() != null) {
NotificationRegistry.getInternalNotificationCenter(projectConfigManager.getSDKKey()).
addNotificationHandler(UpdateConfigNotification.class,
configNotification -> { updateODPSettings(); });
}

}
}

Expand All @@ -153,6 +159,8 @@ public void close() {
tryClose(eventProcessor);
tryClose(eventHandler);
tryClose(projectConfigManager);
notificationCenter.clearAllNotificationListeners();
NotificationRegistry.clearNotificationCenterRegistry(projectConfigManager.getSDKKey());
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,9 @@ void setConfig(ProjectConfig projectConfig) {
currentProjectConfig.set(projectConfig);
currentOptimizelyConfig.set(new OptimizelyConfigService(projectConfig).getConfig());
countDownLatch.countDown();
if (projectConfig.getSdkKey() != null) {
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)
{
NotificationCenter notificationCenter = null;
if (sdkKey != null) {
if (_notificationCenters.containsKey(sdkKey)) {
notificationCenter = _notificationCenters.get(sdkKey);
} else {
notificationCenter = new NotificationCenter();
_notificationCenters.put(sdkKey, notificationCenter);
}
}
return notificationCenter;
}

public static void clearNotificationCenterRegistry(@Nonnull String sdkKey) {
if (sdkKey != null) {
_notificationCenters.remove(sdkKey);
}
}

}
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,84 @@
/**
*
* 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;
import static org.junit.Assert.assertNull;


public class NotificationRegistryTest {

@SuppressFBWarnings("NP_NONNULL_PARAM_VIOLATION")
@Test
public void getNullNotificationCenterWhenSDKeyIsNull() {
String sdkKey = null;
NotificationCenter notificationCenter = NotificationRegistry.getInternalNotificationCenter(sdkKey);
assertNull(notificationCenter);
}

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

@Test
public void getSameNotificationCenterWhenSDKKeyIsEmpty() {
String sdkKey1 = "";
String sdkKey2 = "";
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(sdkKey1);
NotificationCenter notificationCenter2 = NotificationRegistry.getInternalNotificationCenter(sdkKey1);

Assert.assertNotEquals(notificationCenter1, notificationCenter2);
}

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

Assert.assertEquals(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