Skip to content

Commit f19752d

Browse files
zhu-xiaoweizxkane
authored andcommitted
feat: add dns configuration.
1 parent 825d413 commit f19752d

File tree

9 files changed

+304
-9
lines changed

9 files changed

+304
-9
lines changed

build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ ext {
5252
desugartools: 'com.android.tools:desugar_jdk_libs:1.0.9',
5353
],
5454
androidx: [
55-
appcompat: 'androidx.appcompat:appcompat:1.2.0',
5655
test: 'androidx.test:core:1.5.0'
5756
],
5857
amplifyframework: [

clickstream/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ apply plugin: 'com.android.library'
1717
apply from: rootProject.file("configuration/checkstyle.gradle")
1818

1919
dependencies {
20-
implementation dependency.androidx.appcompat
2120
implementation dependency.aws.mobileclient
2221
implementation dependency.amplifyframework.core
2322
implementation dependency.okhttp

clickstream/src/main/java/com/amazonaws/solution/clickstream/AWSClickstreamPluginConfiguration.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
*/
2121
public final class AWSClickstreamPluginConfiguration {
2222
private static final long DEFAULT_SEND_EVENTS_INTERVAL = 10000L;
23+
private static final long DEFAULT_CALL_TIME_OUT = 15000L;
2324

2425
// Clickstream configuration options
2526
private final String appId;
2627
private final String endpoint;
2728
private final long sendEventsInterval;
29+
private final long callTimeOut;
2830
private final boolean isTrackAppLifecycleEvents;
2931
private final boolean isTrackAppExceptionEvents;
3032
private final boolean isCompressEvents;
@@ -33,6 +35,7 @@ private AWSClickstreamPluginConfiguration(Builder builder) {
3335
this.appId = builder.appId;
3436
this.isTrackAppLifecycleEvents = builder.isTrackAppLifecycleEvents;
3537
this.isTrackAppExceptionEvents = builder.isTrackAppExceptionEvents;
38+
this.callTimeOut = builder.callTimeOut;
3639
this.sendEventsInterval = builder.sendEventsInterval;
3740
this.endpoint = builder.endpoint;
3841
this.isCompressEvents = builder.isCompressEvents;
@@ -56,6 +59,15 @@ long getSendEventsInterval() {
5659
return sendEventsInterval;
5760
}
5861

62+
/**
63+
* Accessor for http call time out.
64+
*
65+
* @return callTimeOut.
66+
*/
67+
long getCallTimeOut() {
68+
return callTimeOut;
69+
}
70+
5971
/**
6072
* Is auto session tracking enabled.
6173
*
@@ -109,6 +121,7 @@ static final class Builder {
109121
private String appId;
110122
private String endpoint;
111123
private long sendEventsInterval = DEFAULT_SEND_EVENTS_INTERVAL;
124+
private final long callTimeOut = DEFAULT_CALL_TIME_OUT;
112125
private boolean isCompressEvents = true;
113126
private boolean isTrackAppLifecycleEvents = true;
114127
private boolean isTrackAppExceptionEvents = false;

clickstream/src/main/java/com/amazonaws/solution/clickstream/ClickstreamManagerFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ static ClickstreamManager create(Context context,
3434
new ClickstreamConfiguration(context, clickstreamPluginConfiguration.getAppId(),
3535
clickstreamPluginConfiguration.getEndpoint())
3636
.withSendEventsInterval(clickstreamPluginConfiguration.getSendEventsInterval())
37+
.withCallTimeOut(clickstreamPluginConfiguration.getCallTimeOut())
3738
.withCompressEvents(clickstreamPluginConfiguration.isCompressEvents())
3839
.withTrackAppLifecycleEvents(clickstreamPluginConfiguration.isTrackAppLifecycleEvents())
3940
.withTrackAppExceptionEvents(clickstreamPluginConfiguration.isTrackAppExceptionEvents());

clickstream/src/main/java/com/amazonaws/solution/clickstream/client/ClickstreamConfiguration.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import android.content.Context;
1919

20+
import okhttp3.Dns;
21+
2022
/**
2123
* Clickstream Configuration.
2224
*/
@@ -25,7 +27,9 @@ public class ClickstreamConfiguration {
2527
private Context context;
2628
private String appId;
2729
private String endpoint;
30+
private Dns dns;
2831
private long sendEventsInterval;
32+
private long callTimeOut;
2933
private boolean isCompressEvents;
3034
private boolean isTrackAppLifecycleEvents;
3135
private boolean isTrackAppExceptionEvents;
@@ -86,6 +90,15 @@ public String getEndpoint() {
8690
return this.endpoint;
8791
}
8892

93+
/**
94+
* For get the Clickstream Okhttp3 dns.
95+
*
96+
* @return the dns.
97+
*/
98+
public Dns getDns() {
99+
return this.dns;
100+
}
101+
89102
/**
90103
* The endpoint for Clickstream.
91104
*
@@ -97,6 +110,17 @@ public ClickstreamConfiguration withEndpoint(final String endpoint) {
97110
return this;
98111
}
99112

113+
/**
114+
* The Custom Okhttp3 dns for Clickstream.
115+
*
116+
* @param dns The custom dns.
117+
* @return the current ClickstreamConfiguration instance.
118+
*/
119+
public ClickstreamConfiguration withCustomDns(final Dns dns) {
120+
this.dns = dns;
121+
return this;
122+
}
123+
100124
/**
101125
* The interval of events sent at once.
102126
*
@@ -106,6 +130,15 @@ public long getSendEventsInterval() {
106130
return this.sendEventsInterval;
107131
}
108132

133+
/**
134+
* The time out of entire http call.
135+
*
136+
* @return callTimeOut.
137+
*/
138+
public Long getCallTimeOut() {
139+
return this.callTimeOut;
140+
}
141+
109142
/**
110143
* The interval of events sent at once.
111144
*
@@ -117,6 +150,17 @@ public ClickstreamConfiguration withSendEventsInterval(final long sendEventsInte
117150
return this;
118151
}
119152

153+
/**
154+
* The http call time out.
155+
*
156+
* @param callTimeOut call time out.
157+
* @return the current ClickstreamConfiguration instance.
158+
*/
159+
public ClickstreamConfiguration withCallTimeOut(final long callTimeOut) {
160+
this.callTimeOut = callTimeOut;
161+
return this;
162+
}
163+
120164
/**
121165
* Is compress events.
122166
*

clickstream/src/main/java/com/amazonaws/solution/clickstream/client/network/NetRequest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,12 @@ private static Response request(@NonNull String eventJson, @NonNull ClickstreamC
115115
okHttpClientBuilder.connectTimeout(HTTP_CONNECT_TIME_OUT, TimeUnit.SECONDS);
116116
okHttpClientBuilder.readTimeout(HTTP_READ_TIME_OUT, TimeUnit.SECONDS);
117117
okHttpClientBuilder.writeTimeout(HTTP_WRITE_TIME_OUT, TimeUnit.SECONDS);
118+
okHttpClientBuilder.callTimeout(configuration.getCallTimeOut(), TimeUnit.SECONDS);
118119
okHttpClientBuilder.retryOnConnectionFailure(true);
119120
okHttpClientBuilder.addNetworkInterceptor(UserAgentInterceptor.using(UserAgent::string));
121+
if (configuration.getDns() != null) {
122+
okHttpClientBuilder.dns(configuration.getDns());
123+
}
120124

121125
OkHttpClient client = okHttpClientBuilder.build();
122126
LOG.debug(

clickstream/src/test/java/com/amazonaws/solution/clickstream/IntegrationTest.java

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.amazonaws.solution.clickstream.client.Event;
3131
import com.amazonaws.solution.clickstream.client.EventRecorder;
3232
import com.amazonaws.solution.clickstream.client.db.ClickstreamDBUtil;
33+
import com.amazonaws.solution.clickstream.util.CustomOkhttpDns;
3334
import com.amazonaws.solution.clickstream.util.ReflectUtil;
3435
import com.github.dreamhead.moco.HttpServer;
3536
import com.github.dreamhead.moco.Runner;
@@ -451,11 +452,13 @@ public void testCustomConfig() throws Exception {
451452
.withSendEventsInterval(15000)
452453
.withTrackAppExceptionEvents(false)
453454
.withLogEvents(true)
455+
.withCustomDns(CustomOkhttpDns.getInstance())
454456
.withCompressEvents(true);
455457

456458
assertEquals("23982", this.analyticsClient.getClickstreamConfiguration().getAppId());
457459
assertEquals(assembleEndpointUrl(COLLECT_SUCCESS1),
458460
this.analyticsClient.getClickstreamConfiguration().getEndpoint());
461+
assertEquals(CustomOkhttpDns.getInstance(), this.analyticsClient.getClickstreamConfiguration().getDns());
459462
assertEquals(15000, this.analyticsClient.getClickstreamConfiguration().getSendEventsInterval());
460463
Assert.assertFalse(this.analyticsClient.getClickstreamConfiguration().isTrackAppExceptionEvents());
461464
Assert.assertTrue(this.analyticsClient.getClickstreamConfiguration().isCompressEvents());
@@ -476,6 +479,110 @@ public void testCustomConfig() throws Exception {
476479
assertEquals(0, dbUtil.getTotalNumber());
477480
}
478481

482+
/**
483+
* test custom dns for success.
484+
*
485+
* @throws Exception exception
486+
*/
487+
@Test
488+
public void testCustomDnsSuccess() throws Exception {
489+
CustomOkhttpDns dns = CustomOkhttpDns.getInstance();
490+
dns.setDefaultIp("127.0.0.1");
491+
492+
ClickstreamAnalytics.getClickStreamConfiguration().withCustomDns(dns);
493+
494+
assertEquals(dns, this.analyticsClient.getClickstreamConfiguration().getDns());
495+
496+
ClickstreamAnalytics.recordEvent("testRecordEvent");
497+
assertEquals(1, dbUtil.getTotalNumber());
498+
ClickstreamAnalytics.flushEvents();
499+
Thread.sleep(1500);
500+
assertEquals(0, dbUtil.getTotalNumber());
501+
}
502+
503+
/**
504+
* test custom dns for fail.
505+
*
506+
* @throws Exception exception
507+
*/
508+
@Test
509+
public void testCustomDnsFail() throws Exception {
510+
CustomOkhttpDns dns = CustomOkhttpDns.getInstance();
511+
dns.setDefaultIp("192.168.1.10");
512+
ClickstreamAnalytics.getClickStreamConfiguration().withCustomDns(dns);
513+
514+
assertEquals(dns, this.analyticsClient.getClickstreamConfiguration().getDns());
515+
516+
ClickstreamAnalytics.recordEvent("testRecordEvent");
517+
assertEquals(1, dbUtil.getTotalNumber());
518+
setHttpRequestTimeOut(1L);
519+
ClickstreamAnalytics.flushEvents();
520+
Thread.sleep(2000);
521+
assertEquals(1, dbUtil.getTotalNumber());
522+
523+
dns.setDefaultIp("127.0.0.1");
524+
setHttpRequestTimeOut(15L);
525+
ClickstreamAnalytics.flushEvents();
526+
// wait for success
527+
Thread.sleep(1000);
528+
assertEquals(0, dbUtil.getTotalNumber());
529+
}
530+
531+
/**
532+
* test custom dns for resolution timeout fail.
533+
*
534+
* @throws Exception exception
535+
*/
536+
@Test
537+
public void testCustomDnsResolutionTimeoutFail() throws Exception {
538+
CustomOkhttpDns dns = CustomOkhttpDns.getInstance();
539+
dns.setDefaultIp("127.0.0.1");
540+
dns.setIsResolutionTimeout(true);
541+
setHttpRequestTimeOut(1L);
542+
543+
ClickstreamAnalytics.getClickStreamConfiguration().withCustomDns(dns);
544+
assertEquals(dns, this.analyticsClient.getClickstreamConfiguration().getDns());
545+
546+
ClickstreamAnalytics.recordEvent("testRecordEvent");
547+
assertEquals(1, dbUtil.getTotalNumber());
548+
ClickstreamAnalytics.flushEvents();
549+
Thread.sleep(1500);
550+
assertEquals(1, dbUtil.getTotalNumber());
551+
552+
dns.setIsResolutionTimeout(false);
553+
setHttpRequestTimeOut(15L);
554+
ClickstreamAnalytics.flushEvents();
555+
Thread.sleep(1000);
556+
assertEquals(0, dbUtil.getTotalNumber());
557+
}
558+
559+
/**
560+
* test custom dns for unKnow host fail.
561+
*
562+
* @throws Exception exception
563+
*/
564+
@Test
565+
public void testCustomDnsForUnKnowHostFail() throws Exception {
566+
CustomOkhttpDns dns = CustomOkhttpDns.getInstance();
567+
dns.setDefaultIp("127.0.0.1");
568+
dns.setIsResolutionTimeout(false);
569+
dns.setIsUnKnowHost(true);
570+
ClickstreamAnalytics.getClickStreamConfiguration().withCustomDns(dns);
571+
assertEquals(dns, this.analyticsClient.getClickstreamConfiguration().getDns());
572+
573+
ClickstreamAnalytics.recordEvent("testRecordEvent");
574+
assertEquals(1, dbUtil.getTotalNumber());
575+
ClickstreamAnalytics.flushEvents();
576+
Thread.sleep(1000);
577+
assertEquals(1, dbUtil.getTotalNumber());
578+
579+
dns.setIsUnKnowHost(false);
580+
ClickstreamAnalytics.flushEvents();
581+
Thread.sleep(1000);
582+
assertEquals(0, dbUtil.getTotalNumber());
583+
}
584+
585+
479586
/**
480587
* test enable.
481588
*
@@ -567,6 +674,10 @@ private void stopThreadSafely() throws Exception {
567674
ReflectUtil.modifyFiled(submitter, "handler", mock(Handler.class));
568675
}
569676

677+
private void setHttpRequestTimeOut(long timeOutSecond) throws Exception {
678+
ReflectUtil.modifyFiled(ClickstreamAnalytics.getClickStreamConfiguration(), "callTimeOut", timeOutSecond);
679+
}
680+
570681
/**
571682
* close db and stop handler executed thread.
572683
*
@@ -576,6 +687,7 @@ private void stopThreadSafely() throws Exception {
576687
public void tearDown() throws Exception {
577688
dbUtil.closeDB();
578689
stopThreadSafely();
690+
ClickstreamAnalytics.getClickStreamConfiguration().withCustomDns(null);
579691
Map<String, Object> globalAttribute =
580692
(Map<String, Object>) ReflectUtil.getFiled(analyticsClient, "globalAttributes");
581693
Map<String, Object> userAttributes =

clickstream/src/test/java/com/amazonaws/solution/clickstream/uniqueid/SharedPrefsUniqueIdServiceTest.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222
import org.junit.Before;
2323
import org.junit.Test;
2424
import org.junit.runner.RunWith;
25-
import org.mockito.Mockito;
2625
import org.robolectric.RobolectricTestRunner;
2726
import org.robolectric.annotation.Config;
2827

2928
import static org.junit.Assert.assertEquals;
3029
import static org.junit.Assert.assertNotNull;
31-
import static org.mockito.Matchers.any;
32-
import static org.mockito.Matchers.anyString;
33-
import static org.mockito.Matchers.eq;
30+
import static org.mockito.ArgumentMatchers.any;
31+
import static org.mockito.ArgumentMatchers.anyString;
32+
import static org.mockito.ArgumentMatchers.eq;
33+
import static org.mockito.Mockito.mock;
3434
import static org.mockito.Mockito.times;
3535
import static org.mockito.Mockito.verify;
3636
import static org.mockito.Mockito.when;
@@ -39,9 +39,9 @@
3939
@Config(manifest = Config.NONE)
4040
public class SharedPrefsUniqueIdServiceTest {
4141

42-
private final ClickstreamContext mockClickstreamContext = Mockito.mock(ClickstreamContext.class);
43-
private final AndroidSystem mockSystem = Mockito.mock(AndroidSystem.class);
44-
private final AndroidPreferences mockPreferences = Mockito.mock(AndroidPreferences.class);
42+
private ClickstreamContext mockClickstreamContext;
43+
private AndroidSystem mockSystem;
44+
private AndroidPreferences mockPreferences;
4545

4646
private SharedPrefsUniqueIdService serviceToTest = null;
4747

@@ -50,6 +50,9 @@ public class SharedPrefsUniqueIdServiceTest {
5050
*/
5151
@Before
5252
public void setup() {
53+
mockClickstreamContext = mock(ClickstreamContext.class);
54+
mockSystem = mock(AndroidSystem.class);
55+
mockPreferences = mock(AndroidPreferences.class);
5356
when(mockClickstreamContext.getSystem()).thenReturn(mockSystem);
5457
when(mockSystem.getPreferences()).thenReturn(mockPreferences);
5558
serviceToTest = new SharedPrefsUniqueIdService();

0 commit comments

Comments
 (0)