use of com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryChannel in project ApplicationInsights-Java by microsoft.
the class IntegrationTests method setup.
@BeforeEach
public void setup() throws Exception {
HttpClient mockedClient = mock(HttpClient.class);
if (testWithException) {
when(mockedClient.send(any(HttpRequest.class), any(Context.class))).then(invocation -> Mono.error(() -> new Exception("this is expected to be logged by the operation logger")));
} else {
// 401, 403, 408, 429, 500, and 503 response codes result in storing to disk
when(mockedClient.send(any(HttpRequest.class), any(Context.class))).then(invocation -> Mono.just(new MockHttpResponse(invocation.getArgument(0, HttpRequest.class), 500)));
}
HttpPipelineBuilder pipelineBuilder = new HttpPipelineBuilder().httpClient(mockedClient);
localFileCache = new LocalFileCache(tempFolder);
localFileLoader = new LocalFileLoader(localFileCache, tempFolder, null);
StatsbeatModule statsbeatModule = Mockito.mock(StatsbeatModule.class);
when(statsbeatModule.getNetworkStatsbeat()).thenReturn(Mockito.mock(NetworkStatsbeat.class));
telemetryChannel = new TelemetryChannel(pipelineBuilder.build(), new URL("http://foo.bar"), new LocalFileWriter(localFileCache, tempFolder, null), statsbeatModule, false);
}
use of com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryChannel in project ApplicationInsights-Java by microsoft.
the class LocalFileLoaderTests method testDeleteFilePermanentlyOnSuccess.
@Test
public void testDeleteFilePermanentlyOnSuccess() throws Exception {
HttpClient mockedClient = getMockHttpClientSuccess();
HttpPipelineBuilder pipelineBuilder = new HttpPipelineBuilder().httpClient(mockedClient);
LocalFileCache localFileCache = new LocalFileCache(tempFolder);
LocalFileWriter localFileWriter = new LocalFileWriter(localFileCache, tempFolder, null);
LocalFileLoader localFileLoader = new LocalFileLoader(localFileCache, tempFolder, null);
StatsbeatModule mockedStatsbeatModule = Mockito.mock(StatsbeatModule.class);
when(mockedStatsbeatModule.getNetworkStatsbeat()).thenReturn(Mockito.mock(NetworkStatsbeat.class));
TelemetryChannel telemetryChannel = new TelemetryChannel(pipelineBuilder.build(), new URL("http://foo.bar"), localFileWriter, mockedStatsbeatModule, false);
// persist 10 files to disk
for (int i = 0; i < 10; i++) {
localFileWriter.writeToDisk(singletonList(ByteBuffer.wrap("hello world".getBytes(UTF_8))), INSTRUMENTATION_KEY);
}
assertThat(localFileCache.getPersistedFilesCache().size()).isEqualTo(10);
Collection<File> files = FileUtils.listFiles(tempFolder, new String[] { "trn" }, false);
assertThat(files.size()).isEqualTo(10);
int expectedCount = 10;
// send persisted files one by one and then delete it permanently.
for (int i = 0; i < 10; i++) {
LocalFileLoader.PersistedFile persistedFile = localFileLoader.loadTelemetriesFromDisk();
CompletableResultCode completableResultCode = telemetryChannel.sendRawBytes(persistedFile.rawBytes, persistedFile.instrumentationKey, () -> {
}, retryable -> {
});
completableResultCode.join(10, SECONDS);
assertThat(completableResultCode.isSuccess()).isEqualTo(true);
localFileLoader.updateProcessedFileStatus(true, persistedFile.file);
// sleep 1 second to wait for delete to complete
Thread.sleep(1000);
files = FileUtils.listFiles(tempFolder, new String[] { "trn" }, false);
assertThat(files.size()).isEqualTo(--expectedCount);
}
assertThat(localFileCache.getPersistedFilesCache().size()).isEqualTo(0);
}
use of com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryChannel in project ApplicationInsights-Java by microsoft.
the class LocalFileLoaderTests method testDeleteFilePermanentlyOnFailure.
@Test
public void testDeleteFilePermanentlyOnFailure() throws Exception {
HttpClient mockedClient = mock(HttpClient.class);
when(mockedClient.send(any(HttpRequest.class), any(Context.class))).then(invocation -> Mono.error(() -> new Exception("this is expected to be logged by the operation logger")));
HttpPipelineBuilder pipelineBuilder = new HttpPipelineBuilder().httpClient(mockedClient);
LocalFileCache localFileCache = new LocalFileCache(tempFolder);
LocalFileLoader localFileLoader = new LocalFileLoader(localFileCache, tempFolder, null);
LocalFileWriter localFileWriter = new LocalFileWriter(localFileCache, tempFolder, null);
StatsbeatModule statsbeatModule = mock(StatsbeatModule.class);
when(statsbeatModule.getNetworkStatsbeat()).thenReturn(mock(NetworkStatsbeat.class));
TelemetryChannel telemetryChannel = new TelemetryChannel(pipelineBuilder.build(), new URL("http://foo.bar"), localFileWriter, statsbeatModule, false);
// persist 10 files to disk
for (int i = 0; i < 10; i++) {
localFileWriter.writeToDisk(singletonList(ByteBuffer.wrap("hello world".getBytes(UTF_8))), INSTRUMENTATION_KEY);
}
assertThat(localFileCache.getPersistedFilesCache().size()).isEqualTo(10);
Collection<File> files = FileUtils.listFiles(tempFolder, new String[] { "trn" }, false);
assertThat(files.size()).isEqualTo(10);
// fail to send persisted files and expect them to be kept on disk
for (int i = 0; i < 10; i++) {
LocalFileLoader.PersistedFile persistedFile = localFileLoader.loadTelemetriesFromDisk();
assertThat(persistedFile.instrumentationKey).isEqualTo(INSTRUMENTATION_KEY);
CompletableResultCode completableResultCode = telemetryChannel.sendRawBytes(persistedFile.rawBytes, persistedFile.instrumentationKey, () -> {
}, retryable -> {
});
completableResultCode.join(10, SECONDS);
assertThat(completableResultCode.isSuccess()).isEqualTo(false);
localFileLoader.updateProcessedFileStatus(false, persistedFile.file);
}
files = FileUtils.listFiles(tempFolder, new String[] { "trn" }, false);
assertThat(files.size()).isEqualTo(10);
assertThat(localFileCache.getPersistedFilesCache().size()).isEqualTo(10);
}
Aggregations