From d8c0599e225bae10067f1f2e85b9f0870b806750 Mon Sep 17 00:00:00 2001 From: Arif Burak Demiray Date: Tue, 10 Nov 2026 15:14:23 +0300 Subject: [PATCH 1/3] feat: change the server URL and app key after init --- CHANGELOG.md | 3 + .../main/java/ly/count/sdk/java/Config.java | 4 +- .../main/java/ly/count/sdk/java/Countly.java | 83 ++++ .../sdk/java/internal/InternalConfig.java | 19 + .../sdk/java/internal/ModuleBackendMode.java | 2 +- .../java/internal/ModuleConfiguration.java | 14 +- .../count/sdk/java/internal/ModuleEvents.java | 8 + .../sdk/java/internal/ModuleRemoteConfig.java | 20 +- .../sdk/java/internal/ModuleRequests.java | 54 +++ .../ly/count/sdk/java/internal/SDKCore.java | 206 +++++++++ .../ly/count/sdk/java/internal/Storage.java | 23 + .../ly/count/sdk/java/internal/Transport.java | 5 +- .../sdk/java/internal/AppKeyChangeTests.java | 395 +++++++++++++++++ .../java/internal/ServerUrlChangeTests.java | 410 ++++++++++++++++++ 14 files changed, 1237 insertions(+), 9 deletions(-) create mode 100644 sdk-java/src/test/java/ly/count/sdk/java/internal/AppKeyChangeTests.java create mode 100644 sdk-java/src/test/java/ly/count/sdk/java/internal/ServerUrlChangeTests.java diff --git a/CHANGELOG.md b/CHANGELOG.md index e5d92d36..4dae135a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,9 @@ * Added support for the SDK connection test. When the server asks for it, the SDK checks that it can reach every endpoint it depends on and reports the results. * Added "setMaxKeyLength(int)" init config method to limit the length of event keys, view names, segmentation keys and custom user property keys. Longer keys are truncated. There is no limit by default, and the key length limit of the SDK behavior settings overrides it. * Added "disableHealthCheck()" init config method to turn off the health check request. +* Added "changeServerUrl(String)" to "Countly.instance()" for changing the server URL while the SDK runs. Queued requests are sent to the new URL too, and the next initialization uses the URL of the config again. +* Added "changeAppKey(String)" to "Countly.instance()" for reporting to another app while the SDK runs. The running views, queued events, pending user profile changes and the running session are closed under the old app key, then a session begins under the new one if one was running. Queued requests keep the app key they were created with, and the next initialization uses the app key of the config again. +* Added "flushQueues()", "replaceAllAppKeysInQueueWithCurrentAppKey()", "removeDifferentAppKeysFromQueue()" and "attemptToSendStoredRequests()" to "Countly.instance()" for managing the request queue. * The deprecated "event", "timedEvent", "view" and "addCrashReport" calls of the session interface, and "record" and "endAndRecord" of a legacy event, now log a warning that names the call to use instead. * Failed requests are now logged at debug level, and wrong uses of the session, event, view and crash methods at warning level instead of error level. diff --git a/sdk-java/src/main/java/ly/count/sdk/java/Config.java b/sdk-java/src/main/java/ly/count/sdk/java/Config.java index 91603dab..f49dac67 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/Config.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/Config.java @@ -29,12 +29,12 @@ public class Config { /** * URL of Countly server */ - protected URL serverURL; + protected volatile URL serverURL; /** * Application key of Countly server */ - protected String serverAppKey; + protected volatile String serverAppKey; /** * Set of Countly SDK features enabled diff --git a/sdk-java/src/main/java/ly/count/sdk/java/Countly.java b/sdk-java/src/main/java/ly/count/sdk/java/Countly.java index 6a5a40a3..0a0e1dba 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/Countly.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/Countly.java @@ -3,6 +3,7 @@ import java.io.File; import java.util.Arrays; import java.util.Map; +import java.util.function.Consumer; import ly.count.sdk.java.internal.Device; import ly.count.sdk.java.internal.DeviceIdType; import ly.count.sdk.java.internal.InternalConfig; @@ -411,6 +412,88 @@ public static void onConsentRemoval(Config.Feature... features) { } } + /** + * Changes the server URL while the SDK runs. Every later request goes to the new URL, queued + * ones included; a request being sent finishes at the old one. The app key, salt and custom + * request headers stay. Then the SDK requests its behavior settings from the new server, and + * remote config when it downloads automatically. Kept in memory only: the next initialization + * uses the URL of the configuration again. An invalid URL is ignored with an error log. + * + * @param serverUrl an absolute http or https URL with a host, https only while certificate or + * public key pins are set; trailing "/" characters are removed + */ + public void changeServerUrl(String serverUrl) { + callIfInitialized("changeServerUrl, serverUrl:[" + serverUrl + "]", sdk -> sdk.changeServerUrl(serverUrl)); + } + + /** + * Changes the app key while the SDK runs, so the SDK reports to another app. First, under the + * old key, the running views are stopped, the queued events and pending user profile changes + * are stored as requests, and a running session is ended, then begun again under the new key. + * Queued requests keep their key: see {@link #replaceAllAppKeysInQueueWithCurrentAppKey()} and + * {@link #removeDifferentAppKeysFromQueue()}. Then the SDK requests the behavior settings of + * the new app, and its remote config when remote config downloads automatically; the remote + * config values of the old app are cleared, or marked as cached with value caching. Kept in + * memory only: the next initialization uses the key of the configuration again. An invalid key + * is ignored with an error log. The key in use is ignored, and so is any call in backend mode. + * + * @param appKey the new app key, neither empty nor "YOUR_APP_KEY" + */ + public void changeAppKey(String appKey) { + callIfInitialized("changeAppKey, appKey:[" + appKey + "]", sdk -> sdk.changeAppKey(appKey)); + } + + /** + * Clears the request queue and the event queue: all pending data is discarded. Started timed + * events, running views, pending user profile changes and the health check counters are kept, + * and a request that is being sent is not stopped. Ignored in backend mode. + */ + public void flushQueues() { + callIfInitialized("flushQueues", SDKCore::flushQueues); + } + + /** + * Replaces the app key of every queued request with the current app key, so requests queued + * under another key are sent to the current app. Ignored in backend mode. + */ + public void replaceAllAppKeysInQueueWithCurrentAppKey() { + callIfInitialized("replaceAllAppKeysInQueueWithCurrentAppKey", SDKCore::replaceAllAppKeysInQueueWithCurrentAppKey); + } + + /** + * Removes every queued request whose app key differs from the current app key, so it is never + * sent. Ignored in backend mode. + */ + public void removeDifferentAppKeysFromQueue() { + callIfInitialized("removeDifferentAppKeysFromQueue", SDKCore::removeDifferentAppKeysFromQueue); + } + + /** + * Packs the queued events into a request and starts sending the request queue right away, + * without waiting for the next timer tick. During a backoff pause, or while the SDK behavior + * settings turn networking off, sending waits as usual. + */ + public void attemptToSendStoredRequests() { + callIfInitialized("attemptToSendStoredRequests", SDKCore::attemptToSendStoredRequests); + } + + /** + * Logs a call at info level and runs it on the SDK, or only records log code w101 before init. + * + * @param logLine the call and its arguments, as logged + * @param call the call on the SDK + */ + private static void callIfInitialized(String logLine, Consumer call) { + if (!isInitialized()) { + Log.recordLogCodeWhileNotInitialized("w101"); + return; + } + if (cly.L != null) { + cly.L.i("[Countly] " + logLine); + } + call.accept(cly.sdk); + } + /** * Feedback interface to use feedback widgets nps,rating and forms. * And do manual reporting of feedbacks. diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/InternalConfig.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/InternalConfig.java index 12ce391f..004afb9a 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/InternalConfig.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/InternalConfig.java @@ -2,6 +2,7 @@ import java.io.File; import java.lang.reflect.Field; +import java.net.URL; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -238,6 +239,24 @@ public void setLogger(Log logger) { this.configLog = logger; } + /** + * Replaces the server URL every later request is sent to, in this configuration only. + * + * @param serverURL the new server URL + */ + void setServerURL(@Nonnull URL serverURL) { + this.serverURL = serverURL; + } + + /** + * Replaces the app key every later request carries, in this configuration only. + * + * @param serverAppKey the new app key + */ + void setServerAppKey(@Nonnull String serverAppKey) { + this.serverAppKey = serverAppKey; + } + File getSdkStorageRootDirectory() { return sdkStorageRootDirectory; } diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleBackendMode.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleBackendMode.java index 3e089eee..6404a73b 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleBackendMode.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleBackendMode.java @@ -315,7 +315,7 @@ private synchronized void addEventsAgainstDeviceIdToRequestQ(String deviceID) { addRequestToRequestQ(request); } - private synchronized void addEventsToRequestQ() { + synchronized void addEventsToRequestQ() { L.d("[BackendMode] addEventsToRequestQ"); for (String s : eventQueues.keySet()) { diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleConfiguration.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleConfiguration.java index 1f3d881f..a5a212fc 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleConfiguration.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleConfiguration.java @@ -903,9 +903,11 @@ void fetchConfigFromServer(@Nonnull InternalConfig config) { Transport transport = config.sdk.networking.getTransport(); final long fetchStartNs = System.nanoTime(); final int requestedAfterDeviceIdChanges = deviceIdChangesWithoutMerge; + final String requestedAppKey = config.getServerAppKey(); + final String requestedServerUrl = config.getServerURL().toString(); config.immediateRequestGenerator.createImmediateRequestMaker().doWork(requestData, "/o/sdk?", transport, false, true, - response -> onServerConfigResponse(config, response, fetchStartNs, requestedAfterDeviceIdChanges), L); + response -> onServerConfigResponse(config, response, fetchStartNs, requestedAfterDeviceIdChanges, requestedAppKey, requestedServerUrl), L); } catch (Exception e) { L.e("[ModuleConfiguration] fetchConfigFromServer, failed to request the configuration, [" + e + "]"); decideLogGatheringOffIfUndecided("the server config request failed"); @@ -920,8 +922,11 @@ void fetchConfigFromServer(@Nonnull InternalConfig config) { * @param response the response, {@code null} when the request failed * @param fetchStartNs {@link System#nanoTime()} just before the request was made * @param requestedAfterDeviceIdChanges how many device ID changes without merge happened before the request was made + * @param requestedAppKey the app key the request was made with + * @param requestedServerUrl the server URL the request was made to */ - private void onServerConfigResponse(@Nonnull InternalConfig config, @Nullable JSONObject response, long fetchStartNs, int requestedAfterDeviceIdChanges) { + private void onServerConfigResponse(@Nonnull InternalConfig config, @Nullable JSONObject response, long fetchStartNs, int requestedAfterDeviceIdChanges, + @Nullable String requestedAppKey, @Nonnull String requestedServerUrl) { if (!running) { L.d("[ModuleConfiguration] onServerConfigResponse, the SDK was stopped before the response arrived, ignoring it"); return; @@ -933,6 +938,11 @@ private void onServerConfigResponse(@Nonnull InternalConfig config, @Nullable JS return; } + if (Utils.isNotEqual(requestedAppKey, config.getServerAppKey()) || !requestedServerUrl.equals(config.getServerURL().toString())) { + L.d("[ModuleConfiguration] onServerConfigResponse, the app key or the server URL changed after the request was made, ignoring its response"); + return; + } + L.d("[ModuleConfiguration] onServerConfigResponse, retrieved configuration response: [" + response + "]"); long fetchLatencyMs = (System.nanoTime() - fetchStartNs) / 1_000_000L; boolean connectionTestRequested = extractConnectionTestFlag(response); diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleEvents.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleEvents.java index f1f36574..00604a3d 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleEvents.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleEvents.java @@ -194,6 +194,14 @@ private synchronized void addEventsToRequestQ(@Nullable String deviceId, boolean }); } + /** + * Drops the queued events, unsent; timed events are kept. Takes the lock of moving the queued + * events into a request, so a request they were already moved into is queued first. + */ + synchronized void clearEventQueue() { + eventQueue.clear(); + } + protected void recordEventInternal(String key, int count, Double sum, Double dur, Map segmentation, String eventIdOverride) { ConfigurationProvider configProvider = internalConfig.getConfigurationProvider(); if (!configProvider.getTrackingEnabled()) { diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleRemoteConfig.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleRemoteConfig.java index 8810618a..32a27e68 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleRemoteConfig.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleRemoteConfig.java @@ -74,6 +74,8 @@ void updateRemoteConfigValues(@Nullable final String[] keysOnly, @Nullable final Transport transport = internalConfig.sdk.networking.getTransport(); final boolean networkingIsEnabled = internalConfig.getNetworkingEnabled(); + final String requestedAppKey = internalConfig.getServerAppKey(); + final String requestedServerUrl = internalConfig.getServerURL().toString(); internalConfig.immediateRequestGenerator.createImmediateRequestMaker().doWork(requestData, "/o/sdk?", transport, false, networkingIsEnabled, checkResponse -> { L.d("[ModuleRemoteConfig] updateRemoteConfigValues, Processing remote config received response, received response is null:[" + (checkResponse == null) + "]"); @@ -82,6 +84,12 @@ void updateRemoteConfigValues(@Nullable final String[] keysOnly, @Nullable final return; } + if (Utils.isNotEqual(requestedAppKey, internalConfig.getServerAppKey()) || !requestedServerUrl.equals(internalConfig.getServerURL().toString())) { + L.d("[ModuleRemoteConfig] updateRemoteConfigValues, the app key or the server URL changed after the request was made, ignoring its response"); + notifyDownloadCallbacks(devProvidedCallback, RequestResult.Error, "The app key or the server URL changed before the response arrived", fullUpdate, null); + return; + } + String error = null; Map newRC = downloadedValuesIntoMap(checkResponse); @@ -238,7 +246,10 @@ private void saveRCValues(@Nonnull final RemoteConfigValueStore rcvs) { internalConfig.storageProvider.setRemoteConfigValues(rcvs.values); } - private void cacheOrClearRCValuesIfNeeded() { + /** + * Clears the stored remote config values, or marks them as cached with value caching. + */ + void cacheOrClearRCValuesIfNeeded() { L.v("[ModuleRemoteConfig] cacheOrClearRCValuesIfNeeded, cache-clearing values"); RemoteConfigValueStore rcvs = getRemoteConfigValueStoreInternal(); @@ -254,7 +265,12 @@ private void notifyDownloadCallbacks(final RCDownloadCallback devProvidedCallbac } } - private void rcAutomaticDownloadTrigger(final boolean cacheClearOldValues) { + /** + * Downloads every remote config value when automatic download triggers are enabled. + * + * @param cacheClearOldValues whether the stored values are first cleared, or marked as cached with value caching + */ + void rcAutomaticDownloadTrigger(final boolean cacheClearOldValues) { if (cacheClearOldValues) { cacheOrClearRCValuesIfNeeded(); } diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleRequests.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleRequests.java index f16107db..a6255704 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleRequests.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleRequests.java @@ -8,6 +8,8 @@ import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Predicate; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -387,6 +389,58 @@ static int dropOldestRequestsOverQueueLimit(@Nonnull InternalConfig config) { return droppedIds.size(); } + /** + * Sets the current app key on every queued request that carries another one, or none, as one + * storage task. The checksum is computed when a request is sent, so it matches the new key. + * + * @param config configuration of the running SDK + * @return how many requests were changed + */ + static int replaceAppKeysInQueue(@Nonnull InternalConfig config) { + final String appKey = config.getServerAppKey(); + final AtomicInteger replaced = new AtomicInteger(); + Storage.transform(config, Request.getStoragePrefix(), (id, data) -> { + Request request = new Request(id); + if (!request.restore(data, config.getLogger()) || appKey.equals(request.params.get("app_key"))) { + return null; + } + request.params.remove("app_key"); + request.params.add("app_key", appKey); + replaced.incrementAndGet(); + return request.store(config.getLogger()); + }); + return replaced.get(); + } + + /** + * Removes the queued requests a filter selects, unsent, in one storage task, so no other storage + * work interleaves. A request being sent finishes, then the network removes it by its storage + * ID, so no other request is lost. Settles the journey triggers the removed requests carried. + * + * @param config configuration of the running SDK + * @param filter selects a request to remove, {@code null} to remove every request without reading it + * @return how many requests were removed + */ + static int removeQueuedRequests(@Nonnull InternalConfig config, @Nullable Predicate filter) { + List removedIds = Storage.runAndWait(config, () -> { + List removed = new ArrayList<>(); + for (Long id : config.sdk.sdkStorage.storableList(config, Request.getStoragePrefix(), 0)) { + Request request = new Request(id); + boolean selected = filter == null || (Boolean.TRUE.equals(config.sdk.sdkStorage.storableRead(config, request)) && filter.test(request)); + if (selected && Boolean.TRUE.equals(config.sdk.sdkStorage.storableRemove(config, request))) { + removed.add(id); + } + } + return removed; + }); + if (removedIds == null) { + return 0; + } + + settleJourneyTriggersOf(config, removedIds); + return removedIds.size(); + } + /** * Tells the events module which requests were dropped unread, so a journey trigger one of them * carried stops waiting for a response. Only a set of IDs is touched, which is safe on the diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/SDKCore.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/SDKCore.java index a532399b..68fbbea3 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/SDKCore.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/SDKCore.java @@ -1,11 +1,15 @@ package ly.count.sdk.java.internal; import java.io.File; +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.net.URL; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Queue; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentSkipListMap; import java.util.concurrent.CopyOnWriteArrayList; @@ -13,9 +17,15 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import ly.count.sdk.java.Config; +import ly.count.sdk.java.Countly; public class SDKCore { + /** + * The app key placeholder of the integration samples, never a key of a real app. + */ + private static final String PLACEHOLDER_APP_KEY = "YOUR_APP_KEY"; + protected static SDKCore instance; protected final SDKStorage sdkStorage; private UserImpl user; @@ -779,6 +789,202 @@ public InternalConfig config() { return config; } + /** + * Switches to a valid server URL in memory, then sends the request queue and catches up the new + * server, see {@link Countly#changeServerUrl(String)}. + * + * @param serverUrl the new server URL, ignored with log code e102 when it is not valid + */ + public void changeServerUrl(@Nullable String serverUrl) { + URL url = validServerUrl(serverUrl); + if (url == null) { + L.recordLogCode("e102"); + return; + } + + config.setServerURL(url); + L.i("[SDKCore] changeServerUrl, requests are sent to [" + url + "] from now on"); + checkNetworking(config); + catchUpAfterChange(false); + } + + /** + * Parses a server URL the SDK can send to, logging an error when it cannot. + * + * @param serverUrl the URL as given + * @return the URL without trailing "/" characters, {@code null} when it is not valid + */ + @Nullable + private URL validServerUrl(@Nullable String serverUrl) { + URL url; + try { + url = new URL(serverUrl == null ? "" : serverUrl.replaceAll("/+$", "")); + url.toURI(); + } catch (MalformedURLException | URISyntaxException e) { + url = null; + } + + if (url == null || !("http".equals(url.getProtocol()) || "https".equals(url.getProtocol())) || Utils.isEmptyOrNull(url.getHost())) { + L.e("[SDKCore] changeServerUrl, [" + serverUrl + "] is not an absolute http or https URL with a host, the server URL stays [" + config.getServerURL() + "]"); + return null; + } + + Set keyPins = config.getPublicKeyPins(); + Set certificatePins = config.getCertificatePins(); + boolean pinned = (keyPins != null && !keyPins.isEmpty()) || (certificatePins != null && !certificatePins.isEmpty()); + if (pinned && !"https".equals(url.getProtocol())) { + L.e("[SDKCore] changeServerUrl, certificate or public key pins are set, so [" + serverUrl + "] must use https, the server URL stays [" + config.getServerURL() + "]"); + return null; + } + return url; + } + + /** + * Closes what belongs to the old app under the old key, switches the key in memory, begins a + * session again if one was running and catches up the new app, see + * {@link Countly#changeAppKey(String)}. + * + * @param appKey the new app key, ignored with log code e103 when it is empty or "YOUR_APP_KEY" + */ + public void changeAppKey(@Nullable String appKey) { + if (ignoredInBackendMode("changeAppKey")) { + return; + } + if (Utils.isEmptyOrNull(appKey) || PLACEHOLDER_APP_KEY.equals(appKey)) { + L.e("[SDKCore] changeAppKey, [" + appKey + "] is not a valid app key, the app key stays the same"); + L.recordLogCode("e103"); + return; + } + if (appKey.equals(config.getServerAppKey())) { + L.d("[SDKCore] changeAppKey, the given app key is already in use, ignoring the call"); + return; + } + + //the lock of the views, user profile and device ID calls, so none of them runs between closing and switching + synchronized (Countly.instance()) { + ModuleViews views = module(ModuleViews.class); + if (views != null) { + views.stopAllViewsInternal(null); + } + ModuleEvents events = runningModule(CoreFeature.Events, ModuleEvents.class); + if (events != null) { + events.checkEventQueueToSend(true); + } + ModuleUserProfile.saveIfSentAutomatically(config); + SessionImpl session = getSession(); + boolean sessionWasRunning = session != null && session.isActive(); + if (sessionWasRunning) { + session.endAutomatically(null, null, null); + } + + config.setServerAppKey(appKey); + L.i("[SDKCore] changeAppKey, new requests carry the new app key from now on"); + + if (sessionWasRunning) { + session(null).beginAutomatically(); + } + } + catchUpAfterChange(true); + } + + /** + * Catches up a server or app that knows nothing of this device yet: requests the SDK behavior + * settings, keeping the stored ones until the answer arrives, and, as at init, downloads remote + * config when it downloads automatically, except in temporary device ID mode. + * + * @param appKeyChanged whether the stored remote config values belong to the old app, so they + * are cleared, or marked as cached with value caching + */ + private void catchUpAfterChange(boolean appKeyChanged) { + ModuleConfiguration configuration = moduleConfiguration; + if (configuration != null) { + configuration.fetchConfigFromServer(config); + } + ModuleRemoteConfig remoteConfig = runningModule(CoreFeature.RemoteConfig, ModuleRemoteConfig.class); + if (remoteConfig == null) { + return; + } + if (appKeyChanged) { + remoteConfig.cacheOrClearRCValuesIfNeeded(); + } + if (!config.isTemporaryIdEnabled()) { + remoteConfig.rcAutomaticDownloadTrigger(false); + } + } + + /** + * Drops the queued events and every queued request, unsent, see {@link Countly#flushQueues()}. + */ + public void flushQueues() { + if (ignoredInBackendMode("flushQueues")) { + return; + } + ModuleEvents events = module(ModuleEvents.class); + if (events != null) { + events.clearEventQueue(); + } else { + //events stored by an earlier run, which the events module restores once it runs + sdkStorage.storeEventQueue(""); + } + int removed = ModuleRequests.removeQueuedRequests(config, null); + L.i("[SDKCore] flushQueues, cleared the event queue and removed [" + removed + "] queued requests"); + } + + /** + * Sets the current app key on every queued request, so the requests queued under another key + * go to the current app. + */ + public void replaceAllAppKeysInQueueWithCurrentAppKey() { + if (ignoredInBackendMode("replaceAllAppKeysInQueueWithCurrentAppKey")) { + return; + } + int replaced = ModuleRequests.replaceAppKeysInQueue(config); + L.i("[SDKCore] replaceAllAppKeysInQueueWithCurrentAppKey, [" + replaced + "] queued requests now carry the current app key"); + } + + /** + * Removes, unsent, every queued request whose app key is not the current one. + */ + public void removeDifferentAppKeysFromQueue() { + if (ignoredInBackendMode("removeDifferentAppKeysFromQueue")) { + return; + } + String appKey = config.getServerAppKey(); + int removed = ModuleRequests.removeQueuedRequests(config, request -> !appKey.equals(request.params.get("app_key"))); + L.i("[SDKCore] removeDifferentAppKeysFromQueue, removed [" + removed + "] queued requests with another app key"); + } + + /** + * Moves the queued events, those of backend mode included, into requests and starts sending + * the request queue now, see {@link Countly#attemptToSendStoredRequests()}. + */ + public void attemptToSendStoredRequests() { + ModuleEvents events = runningModule(CoreFeature.Events, ModuleEvents.class); + if (events != null) { + events.checkEventQueueToSend(true); + } + ModuleBackendMode backendMode = module(ModuleBackendMode.class); + if (config.isBackendModeEnabled() && backendMode != null) { + backendMode.addEventsToRequestQ(); + } + checkNetworking(config); + } + + /** + * Whether a call is ignored as backend mode is enabled, where the application sets the app key + * of each request, so the queue holds requests of other apps on purpose. + * + * @param call the call, for the log line + * @return {@code true} when backend mode is enabled + */ + private boolean ignoredInBackendMode(@Nonnull String call) { + if (!config.isBackendModeEnabled()) { + return false; + } + L.w("[SDKCore] " + call + ", backend mode is enabled, where requests carry the app key the application sets, ignoring the call"); + return true; + } + public void notifyModulesDeviceIdChanged(@Nullable String old, final boolean withMerge) { L.d("[SDKCore] notifyModulesDeviceIdChanged, newDeviceId:[" + config.getDeviceId() + "], oldDeviceId:[ " + old + "]"); Config.DID id = config.getDeviceId(); diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/Storage.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/Storage.java index 1add3b9b..48f0011a 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/Storage.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/Storage.java @@ -2,6 +2,7 @@ import java.util.List; import java.util.Map; +import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; @@ -184,6 +185,28 @@ public Boolean call() throws Exception { return false; } + /** + * Runs a job on the storage thread and waits for it, so no other storage task reads or changes + * the stored files while the job runs. + * + * @param config to configure + * @param job the job to run + * @return the result of the job, {@code null} when it failed or the wait was interrupted + */ + static T runAndWait(final InternalConfig config, final Callable job) { + try { + return tasks.run(new Tasks.Task(Tasks.ID_STRICT) { + @Override + public T call() throws Exception { + return job.call(); + } + }).get(); + } catch (InterruptedException | ExecutionException e) { + config.getLogger().e("[Storage] runAndWait, the storage job failed, [" + e + "]"); + } + return null; + } + /** * Reinitializes storable with data stored previously in device internal memory. * diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/Transport.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/Transport.java index 281b5aba..4bcfd1e4 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/Transport.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/Transport.java @@ -141,9 +141,10 @@ HttpURLConnection connection(final Request request) throws IOException { endpoint = "/i?"; } - String path = config.getServerURL().toString() + endpoint; + URL serverUrl = config.getServerURL(); + String path = serverUrl.toString() + endpoint; byte[] maybePictureData = getPictureDataFromRequest(request); - boolean usingGET = !config.isHTTPPostForced() && request.isGettable(config.getServerURL()) && maybePictureData == null; + boolean usingGET = !config.isHTTPPostForced() && request.isGettable(serverUrl) && maybePictureData == null; if (usingGET && config.getParameterTamperingProtectionSalt() != null) { request.params.add(CHECKSUM, Utils.digestHex(PARAMETER_TAMPERING_DIGEST, request.params + config.getParameterTamperingProtectionSalt(), L)); diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/AppKeyChangeTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/AppKeyChangeTests.java new file mode 100644 index 00000000..7d73bd0d --- /dev/null +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/AppKeyChangeTests.java @@ -0,0 +1,395 @@ +package ly.count.sdk.java.internal; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CopyOnWriteArrayList; +import ly.count.sdk.java.Config; +import ly.count.sdk.java.Countly; +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Changing the app key while the SDK runs and the request queue calls, asserted on the request + * queue and the event queue the SDK stores. + */ +@RunWith(JUnit4.class) +public class AppKeyChangeTests { + + private static final String NEW_APP_KEY = "NEW_APP_KEY"; + private static final String VIEW_EVENT = "[CLY]_view"; + + /** + * Starts every test without SDK files. + */ + @Before + public void beforeTest() { + TestUtils.createCleanTestState(); + } + + /** + * Stops the SDK and deletes its data. + */ + @After + public void stop() { + Countly.instance().halt(); + } + + /** + * The running view, the queued events, the pending user profile changes and the running session + * of the old app go out under the old key, then a session begins under the new key. The timed + * event keeps running and goes out under the new key, and the device ID stays. + */ + @Test + public void changeAppKey_closesTheOldAppAndBeginsTheNewOne() { + Countly.instance().init(config(Config.Feature.Sessions, Config.Feature.Views, Config.Feature.Events, Config.Feature.UserProfiles)); + Countly.session().begin(); + Countly.instance().views().startView("home"); + Countly.instance().events().startEvent("timed"); + Countly.instance().events().recordEvent("before_change"); + Countly.instance().userProfile().setProperty("plan", "pro"); + Assert.assertEquals(1, TestUtils.getCurrentRQ().length); + + Countly.instance().changeAppKey(NEW_APP_KEY); + + Map[] rq = TestUtils.getCurrentRQ(); + Assert.assertEquals(NEW_APP_KEY, SDKCore.instance.config.getServerAppKey()); + Assert.assertEquals(0, TestUtils.getCurrentEQ().size()); + Assert.assertEquals("1", rq[0].get("begin_session")); + Assert.assertEquals("1", rq[rq.length - 2].get("end_session")); + Assert.assertEquals("1", rq[rq.length - 1].get("begin_session")); + Assert.assertEquals(NEW_APP_KEY, rq[rq.length - 1].get("app_key")); + Assert.assertNull(rq[rq.length - 1].get("events")); + for (int i = 0; i < rq.length - 1; i++) { + Assert.assertEquals(TestUtils.SERVER_APP_KEY, rq[i].get("app_key")); + } + for (Map request : rq) { + Assert.assertEquals(TestUtils.DEVICE_ID, request.get("device_id")); + } + + List oldAppEvents = events(rq, TestUtils.SERVER_APP_KEY); + Assert.assertEquals(1, count(oldAppEvents, "before_change")); + Assert.assertEquals(0, count(oldAppEvents, "timed")); + Assert.assertTrue("the running view is stopped under the old key", oldAppEvents.stream().anyMatch(e -> VIEW_EVENT.equals(e.getString("key")) && e.has("dur"))); + Assert.assertEquals(1, requestsWith(rq, "user_details", TestUtils.SERVER_APP_KEY)); + + Countly.instance().events().endEvent("timed"); + Countly.instance().attemptToSendStoredRequests(); + Map[] after = TestUtils.getCurrentRQ(); + Assert.assertEquals(rq.length + 1, after.length); + List newAppEvents = events(after, NEW_APP_KEY); + Assert.assertEquals(1, count(newAppEvents, "timed")); + Assert.assertEquals("the stopped view is not restarted", 0, count(newAppEvents, VIEW_EVENT)); + } + + /** + * An empty or placeholder app key is ignored with log code e103, and the key in use is ignored + * too: nothing is closed or sent, and the key stays. + */ + @Test + public void changeAppKey_invalidPlaceholderAndSameKeyAreIgnored() { + Countly.instance().init(config(Config.Feature.Sessions, Config.Feature.Events)); + Countly.session().begin(); + Countly.instance().events().recordEvent("stays_queued"); + + Countly.instance().changeAppKey(null); + Countly.instance().changeAppKey(""); + Countly.instance().changeAppKey("YOUR_APP_KEY"); + Countly.instance().changeAppKey(TestUtils.SERVER_APP_KEY); + + Assert.assertEquals(TestUtils.SERVER_APP_KEY, SDKCore.instance.config.getServerAppKey()); + Assert.assertEquals(3, logCode("e103")); + Map[] rq = TestUtils.getCurrentRQ(); + Assert.assertEquals(1, rq.length); + Assert.assertEquals("1", rq[0].get("begin_session")); + Assert.assertEquals(1, TestUtils.getCurrentEQ().size()); + Assert.assertTrue(Countly.session().isActive()); + } + + /** + * Without a running session no session is ended or begun: only the queued events are sent under + * the old key, and what is recorded afterwards carries the new key. + */ + @Test + public void changeAppKey_withoutSession_beginsNoSession() { + Countly.instance().init(config(Config.Feature.Sessions, Config.Feature.Events)); + Countly.instance().events().recordEvent("old_app"); + + Countly.instance().changeAppKey(NEW_APP_KEY); + Countly.instance().events().recordEvent("new_app"); + Countly.instance().attemptToSendStoredRequests(); + + Map[] rq = TestUtils.getCurrentRQ(); + Assert.assertEquals(2, rq.length); + Assert.assertEquals(1, count(events(rq, TestUtils.SERVER_APP_KEY), "old_app")); + Assert.assertEquals(1, count(events(rq, NEW_APP_KEY), "new_app")); + for (Map request : rq) { + Assert.assertNull(request.get("begin_session")); + Assert.assertNull(request.get("end_session")); + } + } + + /** + * Queued requests keep the key they were created with until the queue call rewrites them all + * to the current key, the rest of each request untouched. + */ + @Test + public void replaceAllAppKeysInQueueWithCurrentAppKey_rewritesTheOldRequests() { + Map[] before = queueOneRequestPerAppKey(); + Assert.assertEquals(TestUtils.SERVER_APP_KEY, before[0].get("app_key")); + Assert.assertEquals(NEW_APP_KEY, before[1].get("app_key")); + + Countly.instance().replaceAllAppKeysInQueueWithCurrentAppKey(); + + Map[] after = TestUtils.getCurrentRQ(); + Assert.assertEquals(2, after.length); + for (int i = 0; i < after.length; i++) { + Assert.assertEquals(NEW_APP_KEY, after[i].get("app_key")); + Assert.assertEquals(before[i].get("events"), after[i].get("events")); + Assert.assertEquals(before[i].get("timestamp"), after[i].get("timestamp")); + Assert.assertEquals(before[i].size(), after[i].size()); + } + } + + /** + * The queue call removes the requests created under another key and keeps the ones of the + * current key. + */ + @Test + public void removeDifferentAppKeysFromQueue_dropsTheOldRequests() { + Map[] before = queueOneRequestPerAppKey(); + + Countly.instance().removeDifferentAppKeysFromQueue(); + + Map[] after = TestUtils.getCurrentRQ(); + Assert.assertEquals(1, after.length); + Assert.assertEquals(NEW_APP_KEY, after[0].get("app_key")); + Assert.assertEquals(before[1].get("events"), after[0].get("events")); + } + + /** + * Flushing drops the queued requests and events. The timed event, the running view, the pending + * user profile change and the health check counters are kept, and what follows is queued again. + */ + @Test + public void flushQueues_dropsQueuedDataAndKeepsWhatIsNotQueued() { + Countly.instance().init(config(Config.Feature.Sessions, Config.Feature.Views, Config.Feature.Events, Config.Feature.UserProfiles)); + Countly.session().begin(); + Countly.instance().views().startView("running"); + Countly.instance().events().startEvent("timed"); + Countly.instance().events().recordEvent("flushed_request"); + Countly.instance().attemptToSendStoredRequests(); + Countly.instance().events().recordEvent("flushed_event"); + Countly.instance().userProfile().setProperty("kept", "yes"); + Countly.instance().changeAppKey(""); + Assert.assertEquals(2, TestUtils.getCurrentRQ().length); + Assert.assertEquals(1, TestUtils.getCurrentEQ().size()); + + Countly.instance().flushQueues(); + + Assert.assertEquals(0, TestUtils.getCurrentRQ().length); + Assert.assertEquals(0, TestUtils.getCurrentEQ().size()); + Assert.assertEquals(1, logCode("e103")); + + Countly.instance().userProfile().save(); + Countly.instance().events().endEvent("timed"); + Countly.instance().views().stopViewWithName("running"); + Countly.instance().attemptToSendStoredRequests(); + Map[] rq = TestUtils.getCurrentRQ(); + Assert.assertEquals(1, requestsWith(rq, "user_details", TestUtils.SERVER_APP_KEY)); + List events = events(rq, TestUtils.SERVER_APP_KEY); + Assert.assertEquals(1, count(events, "timed")); + Assert.assertTrue(events.stream().anyMatch(e -> VIEW_EVENT.equals(e.getString("key")) && e.has("dur"))); + Assert.assertEquals(0, count(events, "flushed_request") + count(events, "flushed_event")); + } + + /** + * The settings and remote config answers to the requests of the old app that arrive after the + * app key changed are not applied; the answers to the requests of the new app are. + */ + @Test + public void changeAppKey_lateAnswersForTheOldAppAreNotApplied() { + List requests = new CopyOnWriteArrayList<>(); + InternalConfig config = new InternalConfig(TestUtils.getConfigSdkBehaviorSettings().enableFeatures(Config.Feature.RemoteConfig).enableRemoteConfigAutomaticTriggers()); + config.immediateRequestGenerator = () -> (requestData, customEndpoint, cp, delayed, networkingIsEnabled, callback, log) -> requests.add(new Object[] { requestData, callback }); + Countly.instance().init(config); + int eventQueueSize = SDKCore.instance.config.getConfigurationProvider().getEventQueueSizeThreshold(); + Assert.assertNotEquals(7, eventQueueSize); + + Countly.instance().changeAppKey(NEW_APP_KEY); + answer(requests, "sc", TestUtils.SERVER_APP_KEY, new ServerConfigBuilder().eventQueueSize(7).build()); + answer(requests, "rc", TestUtils.SERVER_APP_KEY, "{\"color\":\"red\"}"); + + Assert.assertEquals(eventQueueSize, SDKCore.instance.config.getConfigurationProvider().getEventQueueSizeThreshold()); + Assert.assertTrue(Countly.instance().remoteConfig().getValues().isEmpty()); + + answer(requests, "sc", NEW_APP_KEY, new ServerConfigBuilder().eventQueueSize(7).build()); + answer(requests, "rc", NEW_APP_KEY, "{\"size\":\"big\"}"); + + Assert.assertEquals(7, SDKCore.instance.config.getConfigurationProvider().getEventQueueSizeThreshold()); + Assert.assertEquals(1, Countly.instance().remoteConfig().getValues().size()); + Assert.assertTrue(Countly.instance().remoteConfig().getValues().containsKey("size")); + } + + /** + * Backend mode keeps the app key the application set on each request: the app key change and + * the queue calls that rewrite or drop requests are ignored. The server URL change and sending + * the stored requests work. + */ + @Test + public void backendMode_ignoresTheAppKeyCalls() throws Exception { + Countly.instance().init(TestUtils.getBaseConfig().enableBackendMode()); + ModuleRequests.pushAsync(SDKCore.instance.config, new Request("app_key", "OTHER", "k", "v")).get(); + Countly.instance().backendM().sessionBegin("backend_device", null, null, null); + Countly.instance().backendM().recordEvent("backend_device", "backend_event", 1, null, null, null, null); + int queued = SDKCore.instance.requestQueueMemory.size(); + Assert.assertTrue(queued > 0); + + Countly.instance().changeAppKey(NEW_APP_KEY); + Countly.instance().flushQueues(); + Countly.instance().replaceAllAppKeysInQueueWithCurrentAppKey(); + Countly.instance().removeDifferentAppKeysFromQueue(); + + Assert.assertEquals(TestUtils.SERVER_APP_KEY, SDKCore.instance.config.getServerAppKey()); + Map[] stored = TestUtils.getCurrentRQ(); + Assert.assertEquals(1, stored.length); + Assert.assertEquals("OTHER", stored[0].get("app_key")); + Assert.assertEquals(queued, SDKCore.instance.requestQueueMemory.size()); + for (Request request : SDKCore.instance.requestQueueMemory) { + Assert.assertEquals(TestUtils.SERVER_APP_KEY, request.params.get("app_key")); + } + + Countly.instance().attemptToSendStoredRequests(); + Assert.assertEquals(queued + 1, SDKCore.instance.requestQueueMemory.size()); + Assert.assertTrue(((ModuleBackendMode) Countly.instance().backendM().getModule()).eventQueues.isEmpty()); + + Countly.instance().changeServerUrl("https://other.test.server.com"); + Assert.assertEquals("https://other.test.server.com", SDKCore.instance.config.getServerURL().toString()); + } + + /** + * Every call made before init does nothing and is counted as log code w101. The URL and key of + * a change are kept in memory only, so the next init uses the ones of the configuration. + */ + @Test + public void beforeInitAndNextInit() { + Countly.instance().init(TestUtils.getBaseConfig()); + Countly.instance().halt(); + + Countly.instance().changeServerUrl("https://other.test.server.com"); + Countly.instance().changeAppKey(NEW_APP_KEY); + Countly.instance().flushQueues(); + Countly.instance().replaceAllAppKeysInQueueWithCurrentAppKey(); + Countly.instance().removeDifferentAppKeysFromQueue(); + Countly.instance().attemptToSendStoredRequests(); + + Config config = TestUtils.getBaseConfig(); + Countly.instance().init(config); + Assert.assertEquals(6, logCode("w101")); + Assert.assertEquals(TestUtils.SERVER_URL, SDKCore.instance.config.getServerURL().toString()); + Assert.assertEquals(TestUtils.SERVER_APP_KEY, SDKCore.instance.config.getServerAppKey()); + + Countly.instance().changeServerUrl("https://other.test.server.com"); + Countly.instance().changeAppKey(NEW_APP_KEY); + Countly.instance().stop(); + Countly.instance().init(config); + Assert.assertEquals(TestUtils.SERVER_URL, SDKCore.instance.config.getServerURL().toString()); + Assert.assertEquals(TestUtils.SERVER_APP_KEY, SDKCore.instance.config.getServerAppKey()); + Assert.assertEquals(TestUtils.SERVER_URL, config.getServerURL().toString()); + Assert.assertEquals(TestUtils.SERVER_APP_KEY, config.getServerAppKey()); + } + + // region helpers + + /** + * The base test configuration with the given features and an event queue that holds ten events. + */ + private static Config config(Config.Feature... features) { + return TestUtils.getBaseConfig().enableFeatures(features).setEventQueueSizeToSend(10); + } + + /** + * Queues one event request under the configured key, changes the key, then queues one under the + * new key. + * + * @return the request queue + */ + private static Map[] queueOneRequestPerAppKey() { + Countly.instance().init(config(Config.Feature.Events)); + Countly.instance().events().recordEvent("old_app"); + Countly.instance().attemptToSendStoredRequests(); + Countly.instance().changeAppKey(NEW_APP_KEY); + Countly.instance().events().recordEvent("new_app"); + Countly.instance().attemptToSendStoredRequests(); + + Map[] rq = TestUtils.getCurrentRQ(); + Assert.assertEquals(2, rq.length); + return rq; + } + + /** + * Answers every captured immediate request of a method and app key with the given response. + */ + private static void answer(List requests, String method, String appKey, String response) { + int answered = 0; + for (Object[] request : requests) { + String data = (String) request[0]; + if (data.contains("method=" + method) && data.contains("app_key=" + appKey)) { + ((ImmediateRequestMaker.InternalImmediateRequestCallback) request[1]).callback(new JSONObject(response)); + answered++; + } + } + Assert.assertEquals(1, answered); + } + + /** + * How often the running health check counter recorded a log code. + */ + private static int logCode(String code) { + return SDKCore.instance.healthCheckCounter.snapshot().getJSONObject("lc").optInt(code, 0); + } + + /** + * The events of the queued requests that carry the given app key, in queue order. + */ + private static List events(Map[] rq, String appKey) { + List events = new ArrayList<>(); + for (Map request : rq) { + if (appKey.equals(request.get("app_key")) && request.containsKey("events")) { + JSONArray array = new JSONArray(request.get("events")); + for (int i = 0; i < array.length(); i++) { + events.add(array.getJSONObject(i)); + } + } + } + return events; + } + + /** + * How many of the events have the given key. + */ + private static int count(List events, String key) { + return (int) events.stream().filter(e -> key.equals(e.getString("key"))).count(); + } + + /** + * How many queued requests with the given app key carry the given parameter. + */ + private static int requestsWith(Map[] rq, String param, String appKey) { + int found = 0; + for (Map request : rq) { + if (appKey.equals(request.get("app_key")) && request.containsKey(param)) { + found++; + } + } + return found; + } + + // endregion +} diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/ServerUrlChangeTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/ServerUrlChangeTests.java new file mode 100644 index 00000000..9f5a98ca --- /dev/null +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/ServerUrlChangeTests.java @@ -0,0 +1,410 @@ +package ly.count.sdk.java.internal; + +import com.sun.net.httpserver.HttpServer; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.function.Predicate; +import ly.count.sdk.java.Config; +import ly.count.sdk.java.Countly; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Changing the server URL and the app key while the SDK runs, and the request queue calls, against + * two real HTTP servers on loopback standing in for an old and a new Countly server. The assertions + * are on what each server received. + */ +@RunWith(JUnit4.class) +public class ServerUrlChangeTests { + + private static final String NEW_APP_KEY = "NEW_APP_KEY"; + + private static final String PUBLIC_KEY_PEM = + "-----BEGIN PUBLIC KEY-----\n" + + "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoEAoIix1JRqGjxxZlVLc\n" + + "EYuoQE5QvdClJjdFxtkqnTxHim0Qlvh8tA0EoiFgDdhCvp2HLk3w8EloR5Op2JQK\n" + + "kyTEqPk5OY7ftg39WVsGIAP0CxwoyCymGgmP2gRimjV1wa+58Ng3f5MktTKAVWX9\n" + + "1/ki9khQ2XVIXUv5wq3Yx0VyCZa80W7bifXqfDnLmkhOA8jFy7effR/pkLk5j0HS\n" + + "9g3zYPFmid1gJHxF2/j7oZ9NOG7KBfKuB3Aml8R3Y9Xg/3RAbbh7jVwlYj8CJ1gC\n" + + "TU7V9bCRX+DFjX8XcCP7hVM8yHadS63dsnKPPlUtUbVQouNKqWA+ubhEpQsrxEmv\n" + + "jQIDAQAB\n" + + "-----END PUBLIC KEY-----\n"; + + private Server serverA; + private Server serverB; + + /** + * Starts every test without SDK files and with two new servers. + */ + @Before + public void beforeTest() throws IOException { + TestUtils.createCleanTestState(); + serverA = new Server(); + serverB = new Server(); + } + + /** + * Stops the SDK, deletes its data and stops both servers. + */ + @After + public void afterTest() { + Countly.instance().halt(); + serverA.stop(); + serverB.stop(); + } + + /** + * A request that failed at the old server is retried at the new one, given with trailing "/" + * characters, which are removed, so the path is not doubled. Later requests go there too, with + * the same app key, and changing back to the old URL brings the traffic back. + */ + @Test + public void changeServerUrl_queuedAndLaterRequestsGoToTheNewServer() throws Exception { + serverA.responseCode = 503; + Countly.instance().init(config(serverA, Config.Feature.Events).setEventQueueSizeToSend(1)); + Countly.instance().events().recordEvent("queued_while_down"); + serverA.await(withEvent("queued_while_down")); + + Countly.instance().changeServerUrl(serverB.url() + "///"); + + Assert.assertEquals(serverB.url(), SDKCore.instance.config.getServerURL().toString()); + Received retried = serverB.await(withEvent("queued_while_down")); + Assert.assertEquals("/i", retried.path); + Assert.assertEquals(TestUtils.SERVER_APP_KEY, retried.params.get("app_key")); + Assert.assertEquals(TestUtils.DEVICE_ID, retried.params.get("device_id")); + + Countly.instance().events().recordEvent("after_change"); + Received later = serverB.await(withEvent("after_change")); + Assert.assertEquals("/i", later.path); + Assert.assertEquals(TestUtils.SERVER_APP_KEY, later.params.get("app_key")); + Assert.assertEquals(1, serverA.count(withEvent("queued_while_down"))); + Assert.assertEquals(0, serverA.count(withEvent("after_change"))); + + serverA.responseCode = 200; + Countly.instance().changeServerUrl(serverA.url()); + Countly.instance().events().recordEvent("after_revert"); + serverA.await(withEvent("after_revert")); + Assert.assertEquals(0, serverB.count(withEvent("after_revert"))); + Assert.assertTrue("the request queue must drain", TestUtils.waitFor(10_000, () -> queuedRequestCount() == 0)); + } + + /** + * An invalid URL is ignored with log code e102, and the SDK keeps sending to the current server. + */ + @Test + public void changeServerUrl_invalidUrlsAreIgnored() throws Exception { + Countly.instance().init(config(serverA, Config.Feature.Events).setEventQueueSizeToSend(1)); + + String[] invalid = { null, "", "not a url", "ftp://x", "http://", "http:///no_host", "http://exa mple.com" }; + for (String url : invalid) { + Countly.instance().changeServerUrl(url); + } + + Assert.assertEquals(serverA.url(), SDKCore.instance.config.getServerURL().toString()); + Assert.assertEquals(invalid.length, logCode("e102")); + Countly.instance().events().recordEvent("still_on_a"); + Received received = serverA.await(withEvent("still_on_a")); + Assert.assertEquals(TestUtils.SERVER_APP_KEY, received.params.get("app_key")); + Assert.assertTrue(serverB.received.isEmpty()); + } + + /** + * While a public key pin is set, only an https URL is accepted. + */ + @Test + public void changeServerUrl_withPins_acceptsHttpsOnly() { + Countly.instance().init(config(serverA).addPublicKeyPin(PUBLIC_KEY_PEM)); + + Countly.instance().changeServerUrl(serverB.url()); + Assert.assertEquals(serverA.url(), SDKCore.instance.config.getServerURL().toString()); + Assert.assertEquals(1, logCode("e102")); + + Countly.instance().changeServerUrl(TestUtils.SERVER_URL + "/"); + Assert.assertEquals(TestUtils.SERVER_URL, SDKCore.instance.config.getServerURL().toString()); + Assert.assertEquals(1, logCode("e102")); + } + + /** + * Right after a URL change the new server is asked for the SDK behavior settings and, as remote + * config downloads automatically, for remote config, with the same app key. Immediate requests + * made later, as the feedback widget list, go to the new server too. + */ + @Test + public void changeServerUrl_catchesUpTheNewServer() throws Exception { + Config config = config(serverA, Config.Feature.RemoteConfig, Config.Feature.Feedback).enableRemoteConfigAutomaticTriggers(); + Countly.instance().init(config); + serverA.await(withMethod("sc")); + serverA.await(withMethod("rc")); + + Countly.instance().changeServerUrl(serverB.url()); + + Received settings = serverB.await(withMethod("sc")); + Received remoteConfig = serverB.await(withMethod("rc")); + Assert.assertEquals("/o/sdk", settings.path); + Assert.assertEquals("/o/sdk", remoteConfig.path); + Assert.assertEquals(TestUtils.SERVER_APP_KEY, settings.params.get("app_key")); + Assert.assertEquals(TestUtils.SERVER_APP_KEY, remoteConfig.params.get("app_key")); + Assert.assertEquals(TestUtils.DEVICE_ID, remoteConfig.params.get("device_id")); + + Countly.instance().feedback().getAvailableFeedbackWidgets((widgets, error) -> { + }); + serverB.await(withMethod("feedback")); + Assert.assertEquals(0, serverA.count(withMethod("feedback"))); + Assert.assertEquals(1, serverA.count(withMethod("sc"))); + Assert.assertEquals(1, serverA.count(withMethod("rc"))); + } + + /** + * Right after an app key change the server is asked for the SDK behavior settings and remote + * config of the new app. The remote config values of the old app are cleared first, so they are + * gone even when the new download fails. + */ + @Test + public void changeAppKey_catchesUpTheNewApp_andClearsTheRemoteConfigOfTheOldApp() throws Exception { + Countly.instance().init(config(serverA, Config.Feature.RemoteConfig).enableRemoteConfigAutomaticTriggers()); + serverA.await(withMethod("rc")); + Assert.assertTrue(TestUtils.waitFor(5_000, () -> Countly.instance().remoteConfig().getValues().containsKey("color"))); + + serverA.failRemoteConfig = true; + Countly.instance().changeAppKey(NEW_APP_KEY); + + Received settings = serverA.await(withMethod("sc").and(r -> NEW_APP_KEY.equals(r.params.get("app_key")))); + Received remoteConfig = serverA.await(withMethod("rc").and(r -> NEW_APP_KEY.equals(r.params.get("app_key")))); + Assert.assertEquals(TestUtils.DEVICE_ID, settings.params.get("device_id")); + Assert.assertEquals(TestUtils.DEVICE_ID, remoteConfig.params.get("device_id")); + Assert.assertTrue(Countly.instance().remoteConfig().getValues().isEmpty()); + } + + /** + * The queued events are packed into a request and the request queue is sent right away: first + * the event that waited in the event queue, then, once the server is back, the request that + * failed, without waiting for the timer. + */ + @Test + public void attemptToSendStoredRequests_sendsTheEventQueueAndTheRequestQueueNow() throws Exception { + serverA.responseCode = 503; + Countly.instance().init(config(serverA, Config.Feature.Events).setEventQueueSizeToSend(10)); + Countly.instance().events().recordEvent("waiting_in_the_event_queue"); + Assert.assertEquals(1, TestUtils.getCurrentEQ().size()); + + Countly.instance().attemptToSendStoredRequests(); + serverA.await(withEvent("waiting_in_the_event_queue")); + Assert.assertEquals(0, TestUtils.getCurrentEQ().size()); + Assert.assertEquals(1, queuedRequestCount()); + + serverA.responseCode = 200; + Countly.instance().attemptToSendStoredRequests(); + Assert.assertTrue(TestUtils.waitFor(5_000, () -> serverA.count(withEvent("waiting_in_the_event_queue")) == 2)); + Assert.assertTrue(TestUtils.waitFor(5_000, () -> queuedRequestCount() == 0)); + } + + /** + * The queue calls never lose a request that is not sent: the request being sent while the queue + * is flushed finishes, the flushed ones never arrive, and the one queued behind it before it + * finished is sent next, not removed in its place. + */ + @Test + public void flushQueues_whileARequestIsBeingSent() throws Exception { + serverA.gate = new CountDownLatch(1); + Countly.instance().init(config(serverA, Config.Feature.Events).setEventQueueSizeToSend(1)); + Countly.instance().events().recordEvent("being_sent"); + serverA.await(withEvent("being_sent")); + Countly.instance().events().recordEvent("flushed_1"); + Countly.instance().events().recordEvent("flushed_2"); + Assert.assertTrue(TestUtils.waitFor(5_000, () -> queuedRequestCount() == 3)); + + Countly.instance().flushQueues(); + Assert.assertEquals(0, queuedRequestCount()); + Countly.instance().events().recordEvent("after_flush"); + Assert.assertEquals(1, queuedRequestCount()); + serverA.gate.countDown(); + + serverA.await(withEvent("after_flush")); + Assert.assertTrue(TestUtils.waitFor(5_000, () -> queuedRequestCount() == 0)); + Assert.assertEquals(0, serverA.count(withEvent("flushed_1"))); + Assert.assertEquals(0, serverA.count(withEvent("flushed_2"))); + Assert.assertEquals(1, serverA.count(withEvent("being_sent"))); + } + + // region helpers + + /** + * A configuration that sends to the given server without request cooldowns or health check. + */ + private Config config(Server server, Config.Feature... features) { + File directory = TestUtils.getTestSDirectory(); + TestUtils.checkSdkStorageRootDirectoryExist(directory); + + Config config = new Config(server.url(), TestUtils.SERVER_APP_KEY, directory); + config.setApplicationVersion(TestUtils.APPLICATION_VERSION); + config.setCustomDeviceId(TestUtils.DEVICE_ID); + config.disableHealthCheck(); + config.enableFeatures(features); + config.setNetworkRequestCooldown(0); + config.setNetworkImportantRequestCooldown(0); + return config; + } + + /** + * How often the running health check counter recorded a log code. + */ + private static int logCode(String code) { + return SDKCore.instance.healthCheckCounter.snapshot().getJSONObject("lc").optInt(code, 0); + } + + /** + * How many requests are stored, once the pending storage tasks ran. + */ + private static int queuedRequestCount() { + Storage.await(null); + File[] files = TestUtils.getTestSDirectory().listFiles((dir, name) -> name.startsWith("[CLY]_request_")); + return files == null ? 0 : files.length; + } + + /** + * Matches a request that carries an event with the given key. + */ + private static Predicate withEvent(String key) { + return r -> r.params.containsKey("events") && r.params.get("events").contains("\"" + key + "\""); + } + + /** + * Matches an immediate request of the given method. + */ + private static Predicate withMethod(String method) { + return r -> method.equals(r.params.get("method")); + } + + /** + * One request as a server received it, with its parameters decoded. + */ + private static final class Received { + final String path; + final Map params; + + /** + * A request with its path and decoded parameters. + */ + Received(String path, Map params) { + this.path = path; + this.params = params; + } + } + + /** + * A Countly server stand-in on loopback that records every request. It answers remote config + * with one value, the SDK behavior settings with none, and anything else with success, unless + * told to fail; it can hold every event request until a gate opens. + */ + private static final class Server { + final HttpServer http; + final List received = new CopyOnWriteArrayList<>(); + volatile int responseCode = 200; + volatile boolean failRemoteConfig = false; + volatile CountDownLatch gate = null; + + /** + * Starts the server on a free loopback port. + */ + Server() throws IOException { + http = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0); + http.createContext("/", exchange -> { + String query = exchange.getRequestURI().getRawQuery(); + String body = new String(readAll(exchange.getRequestBody()), StandardCharsets.UTF_8); + Map params = TestUtils.parseQueryParams(Utils.isNotEmpty(query) ? query : body); + params.replaceAll((key, value) -> Utils.urldecode(value)); + received.add(new Received(exchange.getRequestURI().getPath(), params)); + + CountDownLatch waitFor = gate; + if (waitFor != null && params.containsKey("events")) { + try { + waitFor.await(10, TimeUnit.SECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + + int code = responseCode; + String response = "{\"result\":\"Success\"}"; + if ("rc".equals(params.get("method"))) { + code = failRemoteConfig ? 500 : code; + response = "{\"color\":\"red\"}"; + } else if ("sc".equals(params.get("method"))) { + response = "{}"; + } else if ("feedback".equals(params.get("method"))) { + response = "{\"result\":[]}"; + } + byte[] out = response.getBytes(StandardCharsets.UTF_8); + exchange.sendResponseHeaders(code, out.length); + try (OutputStream stream = exchange.getResponseBody()) { + stream.write(out); + } + }); + http.setExecutor(Executors.newSingleThreadExecutor()); + http.start(); + } + + /** + * The URL the SDK reaches this server at. + */ + String url() { + return "http://127.0.0.1:" + http.getAddress().getPort(); + } + + /** + * How many received requests match. + */ + int count(Predicate filter) { + return (int) received.stream().filter(filter).count(); + } + + /** + * Waits for the first matching request and returns it. + */ + Received await(Predicate filter) throws InterruptedException { + Assert.assertTrue("no matching request arrived at " + url() + ", it received " + received.size(), TestUtils.waitFor(15_000, () -> count(filter) > 0)); + return received.stream().filter(filter).findFirst().orElse(null); + } + + /** + * Opens the gate and stops the server. + */ + void stop() { + CountDownLatch waitFor = gate; + if (waitFor != null) { + waitFor.countDown(); + } + http.stop(0); + } + + /** + * Reads a request body. + */ + private static byte[] readAll(InputStream stream) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] buffer = new byte[4096]; + int read; + while ((read = stream.read(buffer)) != -1) { + out.write(buffer, 0, read); + } + return out.toByteArray(); + } + } + + // endregion +} From 7d2abb3deff1e24e57db7d954de7c076c9c4c200 Mon Sep 17 00:00:00 2001 From: Arif Burak Demiray Date: Tue, 10 Nov 2026 15:29:49 +0300 Subject: [PATCH 2/3] docs: simpler changelog entries --- CHANGELOG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4dae135a..1a41fa94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,9 +42,9 @@ * Added support for the SDK connection test. When the server asks for it, the SDK checks that it can reach every endpoint it depends on and reports the results. * Added "setMaxKeyLength(int)" init config method to limit the length of event keys, view names, segmentation keys and custom user property keys. Longer keys are truncated. There is no limit by default, and the key length limit of the SDK behavior settings overrides it. * Added "disableHealthCheck()" init config method to turn off the health check request. -* Added "changeServerUrl(String)" to "Countly.instance()" for changing the server URL while the SDK runs. Queued requests are sent to the new URL too, and the next initialization uses the URL of the config again. -* Added "changeAppKey(String)" to "Countly.instance()" for reporting to another app while the SDK runs. The running views, queued events, pending user profile changes and the running session are closed under the old app key, then a session begins under the new one if one was running. Queued requests keep the app key they were created with, and the next initialization uses the app key of the config again. -* Added "flushQueues()", "replaceAllAppKeysInQueueWithCurrentAppKey()", "removeDifferentAppKeysFromQueue()" and "attemptToSendStoredRequests()" to "Countly.instance()" for managing the request queue. +* Added "changeServerUrl(String)" to "Countly.instance()" to change the server URL after init +* Added "changeAppKey(String)" to "Countly.instance()" to change the app key after init +* Added "flushQueues()", "replaceAllAppKeysInQueueWithCurrentAppKey()", "removeDifferentAppKeysFromQueue()" and "attemptToSendStoredRequests()" to "Countly.instance()" to manage the request queue * The deprecated "event", "timedEvent", "view" and "addCrashReport" calls of the session interface, and "record" and "endAndRecord" of a legacy event, now log a warning that names the call to use instead. * Failed requests are now logged at debug level, and wrong uses of the session, event, view and crash methods at warning level instead of error level. From dac4475c0a9f1b1eaea2b7bcf9a5e63ce4d4e902 Mon Sep 17 00:00:00 2001 From: Arif Burak Demiray Date: Tue, 10 Nov 2026 16:05:37 +0300 Subject: [PATCH 3/3] test: wait for the failed send before retrying --- .../count/sdk/java/internal/ServerUrlChangeTests.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/ServerUrlChangeTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/ServerUrlChangeTests.java index 9f5a98ca..58f48e29 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/ServerUrlChangeTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/ServerUrlChangeTests.java @@ -79,6 +79,7 @@ public void changeServerUrl_queuedAndLaterRequestsGoToTheNewServer() throws Exce Countly.instance().init(config(serverA, Config.Feature.Events).setEventQueueSizeToSend(1)); Countly.instance().events().recordEvent("queued_while_down"); serverA.await(withEvent("queued_while_down")); + awaitSendHandled(); Countly.instance().changeServerUrl(serverB.url() + "///"); @@ -204,6 +205,7 @@ public void attemptToSendStoredRequests_sendsTheEventQueueAndTheRequestQueueNow( Countly.instance().attemptToSendStoredRequests(); serverA.await(withEvent("waiting_in_the_event_queue")); + awaitSendHandled(); Assert.assertEquals(0, TestUtils.getCurrentEQ().size()); Assert.assertEquals(1, queuedRequestCount()); @@ -276,6 +278,15 @@ private static int queuedRequestCount() { return files == null ? 0 : files.length; } + /** + * Waits until the SDK finished handling the request it is sending. A send asked for while a + * failed send is still being handled waits for the next request or timer tick, so a retry is + * asked for only once the failed send is handled. + */ + private static void awaitSendHandled() throws InterruptedException { + Assert.assertTrue("the send in progress must end", TestUtils.waitFor(10_000, () -> !SDKCore.instance.networking.isSending())); + } + /** * Matches a request that carries an event with the given key. */