Skip to content

Commit

Permalink
fix: format issue, add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
zhu-xiaowei committed Aug 22, 2024
1 parent e0ab694 commit b5c8edb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import com.amazonaws.logging.Log;
import com.amazonaws.logging.LogFactory;
import java.util.concurrent.Executors;
import software.aws.solution.clickstream.client.db.ClickstreamDBUtil;
import software.aws.solution.clickstream.client.db.EventTable;
import software.aws.solution.clickstream.client.network.NetRequest;
Expand All @@ -30,6 +29,7 @@

import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -210,6 +210,10 @@ String[] getBatchOfEvents(final Cursor cursor) {
return new String[] {eventBuilder.toString(), lastEventId};
}

/**
* Method for send event immediately when event saved fail.
* @param event AnalyticsEvent
*/
public void sendEventImmediately(AnalyticsEvent event) {
Runnable task = () -> {
NetRequest.uploadEvents("[" + event.toJSONObject().toString() + "]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
* Clickstream Database Util.
*/
public class ClickstreamDBUtil {
private static final Log LOG = LogFactory.getLog(EventRecorder.class);

/**
* ClickstreamDBBase is a basic helper for accessing the database.
*/
private ClickstreamDBBase clickstreamDBBase;

private static final Log LOG = LogFactory.getLog(EventRecorder.class);

/**
* Constructs a ClickstreamDBUtil with the given Context.
*
Expand Down Expand Up @@ -67,8 +67,8 @@ public Uri saveEvent(final AnalyticsEvent event) {
Uri uri = null;
try {
uri = clickstreamDBBase.insert(clickstreamDBBase.getContentUri(), generateContentValuesFromEvent(event));
} catch (SQLException e) {
LOG.info("SQLException: " + e.getMessage());
} catch (SQLException error) {
LOG.info("SQLException: " + error.getMessage());
}
return uri;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
Expand Down Expand Up @@ -567,6 +568,23 @@ public void testRecordEventRequestUploadTimestamp() throws Exception {
assertTrue(requestResult);
}

/**
* test save event failed and send event immediately.
*
* @throws Exception exception.
*/
@Test
public void testSaveEventFailedAndWillSendEventImmediately() throws Exception {
dbUtil = mock(ClickstreamDBUtil.class);
ReflectUtil.modifyFiled(eventRecorder, "dbUtil", dbUtil);
when(dbUtil.saveEvent(event)).thenReturn(null);
eventRecorder.recordEvent(event);
verify(log).error("Error to save event with EventType: testEvent");
Thread.sleep(1500);
int bundleSequenceId = (int) ReflectUtil.getFiled(eventRecorder, "bundleSequenceId");
assertTrue(bundleSequenceId > 1);
}

/**
* common method to set request path.
*
Expand Down

0 comments on commit b5c8edb

Please sign in to comment.