diff --git a/splunk-otel-android/src/main/java/com/splunk/rum/FileSender.java b/splunk-otel-android/src/main/java/com/splunk/rum/FileSender.java index 5af3473e0..1f2200df6 100644 --- a/splunk-otel-android/src/main/java/com/splunk/rum/FileSender.java +++ b/splunk-otel-android/src/main/java/com/splunk/rum/FileSender.java @@ -60,6 +60,7 @@ boolean handleFileOnDisk(File file) { Log.d(LOG_TAG, "Reading file content for ingest: " + file); List encodedSpans = readFileCompletely(file); if (encodedSpans.isEmpty()) { + fileUtils.safeDelete(file); return false; } diff --git a/splunk-otel-android/src/main/java/com/splunk/rum/ZipkinToDiskSender.java b/splunk-otel-android/src/main/java/com/splunk/rum/ZipkinToDiskSender.java index 8a2c804fa..ba7762752 100644 --- a/splunk-otel-android/src/main/java/com/splunk/rum/ZipkinToDiskSender.java +++ b/splunk-otel-android/src/main/java/com/splunk/rum/ZipkinToDiskSender.java @@ -59,6 +59,9 @@ public int messageSizeInBytes(List encodedSpans) { @Override public Call sendSpans(List encodedSpans) { + if (encodedSpans.isEmpty()) { + return Call.create(null); + } if (!storageLimiter.ensureFreeSpace()) { Log.e( SplunkRum.LOG_TAG, diff --git a/splunk-otel-android/src/test/java/com/splunk/rum/FileSenderTest.java b/splunk-otel-android/src/test/java/com/splunk/rum/FileSenderTest.java index 25847979b..a83499e5d 100644 --- a/splunk-otel-android/src/test/java/com/splunk/rum/FileSenderTest.java +++ b/splunk-otel-android/src/test/java/com/splunk/rum/FileSenderTest.java @@ -62,7 +62,7 @@ void setup() throws Exception { } @Test - void testEmptyFile() throws Exception { + void sendEmptyFile() throws Exception { Mockito.reset(fileUtils); Mockito.reset(delegate); File file = new File("/asdflkajsdfoij"); @@ -70,10 +70,11 @@ void testEmptyFile() throws Exception { FileSender sender = buildSender(); boolean result = sender.handleFileOnDisk(file); assertFalse(result); + verify(fileUtils).safeDelete(file); } @Test - void testHappyPathSendSpans() { + void happyPathSendSpans() { FileSender sender = buildSender(); boolean result = sender.handleFileOnDisk(file); assertTrue(result); @@ -81,7 +82,7 @@ void testHappyPathSendSpans() { } @Test - void testSendFailsButNotExceeded() throws Exception { + void sendFailsButNotExceeded() throws Exception { when(httpCall.execute()).thenThrow(new IOException("boom")); FileSender sender = buildSender(); boolean result = sender.handleFileOnDisk(file); @@ -91,7 +92,7 @@ void testSendFailsButNotExceeded() throws Exception { } @Test - void testSenderFailureRetriesExhausted() throws Exception { + void senderFailureRetriesExhausted() throws Exception { when(httpCall.execute()).thenThrow(new IOException("boom")); FileSender sender = buildSender(3); boolean result = sender.handleFileOnDisk(file); @@ -109,7 +110,7 @@ void testSenderFailureRetriesExhausted() throws Exception { } @Test - void testReadFileFails() throws IOException { + void readFileFails() throws IOException { Mockito.reset(fileUtils); Mockito.reset(delegate); when(fileUtils.readFileCompletely(file)).thenThrow(new IOException("boom")); diff --git a/splunk-otel-android/src/test/java/com/splunk/rum/ZipkinToDiskSenderTest.java b/splunk-otel-android/src/test/java/com/splunk/rum/ZipkinToDiskSenderTest.java index 554ab8669..edb2971a7 100644 --- a/splunk-otel-android/src/test/java/com/splunk/rum/ZipkinToDiskSenderTest.java +++ b/splunk-otel-android/src/test/java/com/splunk/rum/ZipkinToDiskSenderTest.java @@ -16,8 +16,11 @@ package com.splunk.rum; +import static java.util.Collections.emptyList; import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.lenient; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; @@ -51,8 +54,8 @@ class ZipkinToDiskSenderTest { @BeforeEach void setup() { - when(clock.now()).thenReturn(now); - when(limiter.ensureFreeSpace()).thenReturn(true); + lenient().when(clock.now()).thenReturn(now); + lenient().when(limiter.ensureFreeSpace()).thenReturn(true); } @Test @@ -70,6 +73,18 @@ void testHappyPath() throws Exception { verify(fileUtils).writeAsLines(finalPath, spans); } + @Test + void testEmptyListDoesNotWriteFile() { + ZipkinToDiskSender sender = + ZipkinToDiskSender.builder() + .path(path) + .fileUtils(fileUtils) + .storageLimiter(limiter) + .build(); + sender.sendSpans(emptyList()); + verifyNoInteractions(fileUtils); + } + @Test void testWriteFails() throws Exception { doThrow(new IOException("boom")).when(fileUtils).writeAsLines(finalPath, spans);