Skip to content

Commit

Permalink
feat: immediate req test
Browse files Browse the repository at this point in the history
  • Loading branch information
arifBurakDemiray committed Sep 25, 2023
1 parent 2fd128b commit ea10142
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 12 deletions.
3 changes: 3 additions & 0 deletions sdk-java/src/main/java/ly/count/sdk/java/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Set;
import ly.count.sdk.java.internal.Byteable;
import ly.count.sdk.java.internal.CoreFeature;
import ly.count.sdk.java.internal.ImmediateRequestGenerator;
import ly.count.sdk.java.internal.Log;
import ly.count.sdk.java.internal.LogCallback;
import ly.count.sdk.java.internal.ModuleBase;
Expand Down Expand Up @@ -466,6 +467,8 @@ public boolean restore(byte[] data, Log L) {
*/
File sdkStorageRootDirectory = null;

protected ImmediateRequestGenerator immediateRequestGenerator = null;

// /**
// * Maximum size of all string keys
// */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,9 @@ public void stop(CtxCore ctx) {
shutdown = true;
tasks.shutdown();
}

@Override
public Transport getTransport() {
return transport;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ protected interface InternalImmediateRequestCallback {
void callback(JSONObject checkResponse);
}

InternalImmediateRequestCallback callback;
Log L;
private InternalImmediateRequestCallback callback;
private Log L;

/**
* params fields:
Expand All @@ -36,10 +36,9 @@ protected interface InternalImmediateRequestCallback {
* 5 - callback
* 6 - log module
*/
protected JSONObject doInBackground(String requestData, String customEndpoint, Transport cp, boolean requestShouldBeDelayed, boolean networkingIsEnabled, InternalImmediateRequestCallback callback, Log log) {
private JSONObject doInBackground(String requestData, String customEndpoint, Transport cp, boolean requestShouldBeDelayed, boolean networkingIsEnabled, InternalImmediateRequestCallback callback, Log log) {
this.callback = callback;
L = log;

if (!networkingIsEnabled) {
L.w("[ImmediateRequestMaker] ImmediateRequestMaker, Networking config is disabled, request cancelled. Endpoint[" + customEndpoint + "] request[" + requestData + "]");

Expand All @@ -63,7 +62,6 @@ protected JSONObject doInBackground(String requestData, String customEndpoint, T
L.w("[ImmediateRequestMaker] While waiting for 0.5 seconds in ImmediateRequestMaker, sleep was interrupted");
}
}

Request request = new Request();
request.params.add(requestData);
request.endpoint(customEndpoint);
Expand All @@ -72,15 +70,12 @@ protected JSONObject doInBackground(String requestData, String customEndpoint, T
connection = cp.connection(request);
} catch (IOException e) {
L.e("[ImmediateRequestMaker] IOException while preparing remote config update request :[" + e.toString() + "]");

return null;
}

//connecting
connection.connect();

int code = connection.getResponseCode();

String receivedBuffer = cp.response(connection);

if (receivedBuffer == null) {
Expand Down Expand Up @@ -112,9 +107,8 @@ protected JSONObject doInBackground(String requestData, String customEndpoint, T
return null;
}

protected void onFinished(JSONObject result) {
private void onFinished(JSONObject result) {
L.v("[ImmediateRequestMaker] onPostExecute");

if (callback != null) {
callback.callback(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public interface Networking {
boolean check(CtxCore ctx);

void stop(CtxCore ctx);

Transport getTransport();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ly.count.sdk.java.internal;

import java.util.concurrent.atomic.AtomicReference;
import ly.count.sdk.java.Countly;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import static org.mockito.Mockito.mock;

@RunWith(JUnit4.class)
public class ImmediateRequestTest {
Log L = mock(Log.class);

/**
* Immediate request maker "doWork" function
* Immediate Request Generator is default and endpoint and data are not valid, and app key, server url is default
* should return null because response is not okay
*
* @throws InterruptedException
*/
@Test
public void doWork_null() throws InterruptedException {
Countly.instance().init(TestUtils.getBaseConfig());
AtomicReference<Boolean> callbackResult = new AtomicReference<>(true);

ImmediateRequestMaker immediateRequestMaker = new ImmediateRequestMaker();
immediateRequestMaker.doWork("test_event", "/o?", SDKCore.instance.networking.getTransport(), false, true,
(result) -> {
if (result == null) {
callbackResult.set(false);
}
}, L);

Thread.sleep(2000); // wait for background thread to finish
Assert.assertFalse(callbackResult.get()); // check if callback was called and response is null
}
}
36 changes: 34 additions & 2 deletions sdk-java/src/test/java/ly/count/sdk/java/internal/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,51 @@
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Stream;
import ly.count.sdk.java.Config;

import static ly.count.sdk.java.internal.SDKStorage.EVENT_QUEUE_FILE_NAME;
import static ly.count.sdk.java.internal.SDKStorage.FILE_NAME_PREFIX;
import static ly.count.sdk.java.internal.SDKStorage.FILE_NAME_SEPARATOR;

public class TestUtils {

private static String DELIMETER = ":::";
static String DELIMETER = ":::";
static String SERVER_URL = "https://try.count.ly";
static String SERVER_APP_KEY = "COUNTLY_APP_KEY";
static String DEVICE_ID = "some_random_test_device_id";

private TestUtils() {
}

static Config getBaseConfig() {
File sdkStorageRootDirectory = getSdkStorageRootDirectory();
checkSdkStorageRootDirectoryExist(sdkStorageRootDirectory);
Config config = new Config(SERVER_URL, SERVER_APP_KEY, sdkStorageRootDirectory);
config.setCustomDeviceId(DEVICE_ID);

return config;
}

static Config getVariantConfig(ImmediateRequestGenerator generator) {
Config config = getBaseConfig();
InternalConfig internalConfig = new InternalConfig(config);

internalConfig.immediateRequestGenerator = generator;
return config;
}

static File getSdkStorageRootDirectory() {
// System specific folder structure
String[] sdkStorageRootPath = { System.getProperty("user.home"), "__COUNTLY", "java_test" };
return new File(String.join(File.separator, sdkStorageRootPath));
}

static void checkSdkStorageRootDirectoryExist(File directory) {
if (directory.mkdirs()) {
throw new RuntimeException("Directory creation failed");
}
}

/**
* Get current request queue from target folder
*
Expand Down Expand Up @@ -80,7 +113,6 @@ protected static List<EventImpl> getCurrentEventQueue(File targetFolder, Log log
//do nothing
}

//EventImplQueue.DELIMITER
Arrays.stream(fileContent.split(DELIMETER)).forEach(s -> {
final EventImpl event = EventImpl.fromJSON(s, (ev) -> {
}, logger);
Expand Down

0 comments on commit ea10142

Please sign in to comment.