Skip to content

Commit f60ffc5

Browse files
authored
Add request timer tests (#431)
1 parent c7e9175 commit f60ffc5

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

AndroidSDKCore/src/main/java/com/leanplum/internal/RequestSenderTimer.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package com.leanplum.internal;
2222

2323
import androidx.annotation.NonNull;
24+
import androidx.annotation.VisibleForTesting;
2425
import com.leanplum.EventsUploadInterval;
2526
import com.leanplum.internal.Request.RequestType;
2627

@@ -34,7 +35,8 @@ public static RequestSenderTimer get() {
3435
return INSTANCE;
3536
}
3637

37-
private long getIntervalMillis() {
38+
@VisibleForTesting
39+
protected long getIntervalMillis() {
3840
return timerInterval.getMinutes() * 60 * 1000;
3941
}
4042

@@ -46,7 +48,8 @@ private void sendAllRequestsWithHeartbeat() {
4648
RequestSender.getInstance().send(request);
4749
}
4850

49-
private Runnable createTimerOperation() {
51+
@VisibleForTesting
52+
protected Runnable createTimerOperation() {
5053
return new Runnable() {
5154
@Override
5255
public void run() {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.leanplum.internal;
2+
3+
import com.leanplum.EventsUploadInterval;
4+
import static org.junit.Assert.*;
5+
6+
import com.leanplum.__setup.AbstractTest;
7+
import org.junit.Test;
8+
import org.mockito.Mockito;
9+
10+
public class RequestSenderTimerTest extends AbstractTest {
11+
12+
private boolean operationExecuted;
13+
14+
@Test
15+
public void testDefaultInterval() {
16+
RequestSenderTimer timer = new RequestSenderTimer();
17+
assertEquals(15*60*1000, timer.getIntervalMillis()); // default is 15min
18+
}
19+
20+
@Test
21+
public void testInterval() {
22+
RequestSenderTimer timer = new RequestSenderTimer();
23+
timer.setTimerInterval(EventsUploadInterval.AT_MOST_5_MINUTES);
24+
assertEquals(5*60*1000, timer.getIntervalMillis());
25+
26+
timer.setTimerInterval(EventsUploadInterval.AT_MOST_10_MINUTES);
27+
assertEquals(10*60*1000, timer.getIntervalMillis());
28+
29+
timer.setTimerInterval(EventsUploadInterval.AT_MOST_15_MINUTES);
30+
assertEquals(15*60*1000, timer.getIntervalMillis());
31+
}
32+
33+
/**
34+
* Tests if timer operation is scheduled successfully.
35+
*/
36+
@Test
37+
public void testStartTimer() {
38+
Runnable timerOperation = new Runnable() {
39+
@Override
40+
public void run() {
41+
operationExecuted = true;
42+
}
43+
};
44+
45+
RequestSenderTimer timer = Mockito.spy(RequestSenderTimer.class);
46+
Mockito.when(timer.createTimerOperation()).thenReturn(timerOperation);
47+
48+
operationExecuted = false;
49+
timer.start();
50+
assertTrue(operationExecuted);
51+
}
52+
}

0 commit comments

Comments
 (0)