Skip to content

Commit e18ffc1

Browse files
zhu-xiaoweixiaoweii
andauthored
feat: add flush events action when app move to background. (#21)
Co-authored-by: xiaoweii <xiaoweii@amazom.com>
1 parent 1e570e5 commit e18ffc1

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

clickstream/src/main/java/software/aws/solution/clickstream/client/AutoRecordEventClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public void recordUserEngagement() {
110110
event.addAttribute(Event.ReservedAttribute.SCREEN_ID, ScreenRefererTool.getCurrentScreenId());
111111
this.clickstreamContext.getAnalyticsClient().recordEvent(event);
112112
}
113+
this.clickstreamContext.getAnalyticsClient().submitEvents();
113114
}
114115

115116
/**

clickstream/src/test/java/software/aws/solution/clickstream/AutoRecordEventClientTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,16 @@
3939
import software.aws.solution.clickstream.client.ClickstreamContext;
4040
import software.aws.solution.clickstream.client.ClickstreamManager;
4141
import software.aws.solution.clickstream.client.Event;
42+
import software.aws.solution.clickstream.client.EventRecorder;
4243
import software.aws.solution.clickstream.client.ScreenRefererTool;
4344
import software.aws.solution.clickstream.client.db.ClickstreamDBUtil;
4445
import software.aws.solution.clickstream.client.util.StringUtil;
4546
import software.aws.solution.clickstream.util.ReflectUtil;
4647

4748
import java.util.ArrayList;
4849
import java.util.List;
50+
import java.util.concurrent.ExecutorService;
51+
import java.util.concurrent.ThreadPoolExecutor;
4952

5053
import static org.junit.Assert.assertEquals;
5154
import static org.junit.Assert.assertFalse;
@@ -411,6 +414,23 @@ public void testHandleAppStart() throws Exception {
411414
}
412415
}
413416

417+
/**
418+
* test case for app move to background with flush event.
419+
*
420+
* @throws Exception exception.
421+
*/
422+
@Test
423+
public void testBackgroundRequest() throws Exception {
424+
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START);
425+
Thread.sleep(1100);
426+
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
427+
EventRecorder eventRecorder =
428+
(EventRecorder) ReflectUtil.getFiled(clickstreamContext.getAnalyticsClient(), "eventRecorder");
429+
ExecutorService executorService =
430+
(ExecutorService) ReflectUtil.getFiled(eventRecorder, "submissionRunnableQueue");
431+
assertEquals(1, ((ThreadPoolExecutor) executorService).getActiveCount());
432+
}
433+
414434
/**
415435
* test init autoRecordEventClient with null analyticsClient.
416436
*/

0 commit comments

Comments
 (0)