Skip to content

Commit 62e8fbe

Browse files
authored
fix channel capacity check to be actually mb, fix associated tests (#698)
* fix channel capacity check to be actually mb, fix associated tests * some refactoring and adding comments * minor changes
1 parent 40809cb commit 62e8fbe

File tree

2 files changed

+39
-46
lines changed

2 files changed

+39
-46
lines changed

core/src/main/java/com/microsoft/applicationinsights/internal/channel/common/TransmissionFileSystemOutput.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public final class TransmissionFileSystemOutput implements TransmissionOutput {
8787
private File folder;
8888

8989
/// Capacity is the size of disk that we are can use
90-
private long capacityInKB = DEFAULT_CAPACITY_MEGABYTES * 1024;
90+
private long capacityInBytes = DEFAULT_CAPACITY_MEGABYTES * 1024 * 1024;
9191

9292
LimitsEnforcer capacityEnforcer;
9393

@@ -108,7 +108,7 @@ public TransmissionFileSystemOutput(String folderPath, String maxTransmissionSto
108108
DEFAULT_CAPACITY_MEGABYTES,
109109
MAX_TRANSMISSION_STORAGE_CAPACITY_NAME,
110110
maxTransmissionStorageCapacity);
111-
capacityInKB = capacityEnforcer.getCurrentValue() * 1024;
111+
capacityInBytes = capacityEnforcer.getCurrentValue() * 1024 * 1024;
112112

113113
folder = new File(folderPath);
114114

@@ -134,8 +134,13 @@ public TransmissionFileSystemOutput(String folderPath) {
134134

135135
@Override
136136
public boolean send(Transmission transmission) {
137-
if (size.get() >= capacityInKB) {
138-
InternalLogger.INSTANCE.logAlways(InternalLogger.LoggingLevel.WARN, "Persistent storage max capacity has been reached; currently at %s KB. Telemetry will be lost, please set the MaxTransmissionStorageFilesCapacityInMB property in the configuration file.", size.get());
137+
138+
long currentSizeInBytes = size.get();
139+
if (currentSizeInBytes >= capacityInBytes) {
140+
InternalLogger.INSTANCE.logAlways(InternalLogger.LoggingLevel.WARN, "Persistent storage max capacity has been reached; "
141+
+ "currently at %.3f KB. Telemetry will be lost, "
142+
+ "please consider increasing the value of MaxTransmissionStorageFilesCapacityInMB property in the configuration file.",
143+
(currentSizeInBytes / 1024.0));
139144
return false;
140145
}
141146

@@ -204,7 +209,7 @@ public Transmission fetchOldestFile() {
204209
}
205210

206211
public void setCapacity(int suggestedCapacity) {
207-
this.capacityInKB = capacityEnforcer.normalizeValue(suggestedCapacity) * 1024;
212+
this.capacityInBytes = capacityEnforcer.normalizeValue(suggestedCapacity) * 1024 * 1024;
208213
}
209214

210215
private List<File> sortOldestLastAndTrim(Collection<File> transmissions, int limit) {

core/src/test/java/com/microsoft/applicationinsights/internal/channel/common/TransmissionFileSystemOutputTest.java

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,34 @@
2121

2222
package com.microsoft.applicationinsights.internal.channel.common;
2323

24-
import java.io.File;
25-
import java.io.IOException;
26-
import java.util.Collection;
27-
28-
import com.microsoft.applicationinsights.internal.util.LocalFileSystemUtils;
29-
import org.junit.Ignore;
30-
import org.junit.Test;
31-
32-
import org.apache.commons.io.FileUtils;
33-
3424
import static org.junit.Assert.assertEquals;
3525
import static org.junit.Assert.assertNotNull;
3626
import static org.junit.Assert.assertNull;
3727

28+
import com.microsoft.applicationinsights.internal.util.LocalFileSystemUtils;
29+
import java.io.File;
30+
import java.util.Collection;
31+
import org.apache.commons.io.FileUtils;
32+
import org.junit.Rule;
33+
import org.junit.Test;
34+
import org.junit.rules.TemporaryFolder;
35+
3836
public final class TransmissionFileSystemOutputTest {
3937
private final static String TRANSMISSION_FILE_EXTENSION = "trn";
40-
private final static int SIZE_OF_TRANSMISSION_CONTENT = 100;
38+
39+
// This is derived from the following relationship
40+
// 100 Bytes -> fill 394 bytes of file
41+
// So by doing the math to fill 1 MB with 3 transmission each size of transmission should be the
42+
// following
43+
private final static int SIZE_OF_TRANSMISSION_CONTENT = 349525;
4144
private final static String TEMP_TEST_FOLDER = "TransmissionTests";
4245
private final static String MOCK_CONTENT = "MockContent";
4346
private final static String MOCK_CONTENT_TYPE_BASE = "MockContent";
4447
private final static String MOCK_ENCODING_TYPE_BASE = "MockEncodingType";
4548
private final static int SIZE_OF_MOCK_TRANSMISSION = 1;
4649

47-
private final String workingFolder;
48-
49-
public TransmissionFileSystemOutputTest() {
50-
workingFolder = new File(LocalFileSystemUtils.getTempDir(), TEMP_TEST_FOLDER).getAbsolutePath();
51-
}
50+
@Rule
51+
public TemporaryFolder tmpFolder = new TemporaryFolder();
5252

5353
@Test
5454
public void testSuccessfulSendOneFile() throws Exception {
@@ -71,13 +71,12 @@ public void testSuccessfulSendTenFilesWhereThereIsNoRoomForTheLastThree() throws
7171
}
7272

7373
@Test
74-
@Ignore("The test needs further debugging on the build machine")
7574
public void testFetchOldestFiles() throws Exception {
76-
File folder = createFolderForTest();
75+
File folder = tmpFolder.newFolder(TEMP_TEST_FOLDER);
7776
try {
78-
TransmissionFileSystemOutput tested = new TransmissionFileSystemOutput(workingFolder);
77+
TransmissionFileSystemOutput tested = new TransmissionFileSystemOutput(folder.getAbsolutePath());
7978

80-
for (int i = 10; i != 0; --i) {
79+
for (int i = 1; i <= 10; ++i) {
8180
String iAsString = String.valueOf(i);
8281
String content = MOCK_CONTENT + iAsString;
8382
tested.send(new Transmission(content.getBytes(), MOCK_CONTENT_TYPE_BASE + iAsString, MOCK_ENCODING_TYPE_BASE + iAsString));
@@ -88,10 +87,10 @@ public void testFetchOldestFiles() throws Exception {
8887
assertNotNull(transmission);
8988

9089
String iAsString = String.valueOf(i);
91-
assertEquals(String.format("Wrong WebContentType %s", transmission.getWebContentType()), transmission.getWebContentType(), MOCK_CONTENT_TYPE_BASE + iAsString);
92-
assertEquals(String.format("Wrong WebContentEncodingType %s", transmission.getWebContentEncodingType()), transmission.getWebContentEncodingType(), MOCK_ENCODING_TYPE_BASE + iAsString);
90+
assertEquals(String.format("Wrong WebContentType %s", transmission.getWebContentType()), MOCK_CONTENT_TYPE_BASE + iAsString, transmission.getWebContentType());
91+
assertEquals(String.format("Wrong WebContentEncodingType %s", transmission.getWebContentEncodingType()), MOCK_ENCODING_TYPE_BASE + iAsString, transmission.getWebContentEncodingType());
9392
String fetchedContent = new String(transmission.getContent());
94-
assertEquals(String.format("Wrong content %s", fetchedContent), fetchedContent, MOCK_CONTENT + iAsString);
93+
assertEquals(String.format("Wrong content %s", fetchedContent), MOCK_CONTENT + iAsString, fetchedContent);
9594
}
9695

9796
Transmission transmission = tested.fetchOldestFile();
@@ -108,15 +107,16 @@ private TransmissionFileSystemOutput testSuccessfulSends(int amount) throws Exce
108107
}
109108

110109
private TransmissionFileSystemOutput testSuccessfulSends(int amount, int expectedSuccess, Integer capacity, File testFolder) throws Exception {
111-
File folder = testFolder == null ? createFolderForTest() : testFolder;
110+
File folder = testFolder == null ? tmpFolder.newFolder(TEMP_TEST_FOLDER) : testFolder;
112111
TransmissionFileSystemOutput tested = null;
113112
try {
114-
tested = createAndSend(amount, capacity);
113+
tested = createAndSend(folder.getAbsolutePath(), amount, capacity);
115114

116115
Collection<File> transmissions = FileUtils.listFiles(folder, new String[]{TRANSMISSION_FILE_EXTENSION}, false);
117116

118117
assertNotNull(transmissions);
119-
assertEquals(transmissions.size(), expectedSuccess);
118+
assertEquals(expectedSuccess, transmissions.size());
119+
120120
} finally {
121121
if (testFolder == null && folder.exists()) {
122122
FileUtils.deleteDirectory(folder);
@@ -126,12 +126,12 @@ private TransmissionFileSystemOutput testSuccessfulSends(int amount, int expecte
126126
return tested;
127127
}
128128

129-
private TransmissionFileSystemOutput createAndSend(int amount, Integer capacity) {
129+
private TransmissionFileSystemOutput createAndSend(String absoulutePath, int amount, Integer capacity) {
130130
TransmissionFileSystemOutput tested = null;
131131
if (capacity != null) {
132-
tested = new TransmissionFileSystemOutput(workingFolder, String.valueOf(capacity));;
132+
tested = new TransmissionFileSystemOutput(absoulutePath, String.valueOf(capacity));;
133133
} else {
134-
tested = new TransmissionFileSystemOutput(workingFolder);
134+
tested = new TransmissionFileSystemOutput(absoulutePath);
135135
}
136136

137137
for (int i = 0; i < amount; ++i) {
@@ -140,16 +140,4 @@ private TransmissionFileSystemOutput createAndSend(int amount, Integer capacity)
140140

141141
return tested;
142142
}
143-
144-
private File createFolderForTest() throws IOException {
145-
File folder = new File(workingFolder);
146-
if (folder.exists()) {
147-
FileUtils.deleteDirectory(folder);
148-
}
149-
if (!folder.exists()) {
150-
folder.mkdir();
151-
}
152-
153-
return folder;
154-
}
155143
}

0 commit comments

Comments
 (0)