Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@
"vmArgs": [
"-Djava.util.logging.config.file=src/test/resources/logging.properties"
]
},
"sonarlint.connectedMode.project": {
"connectionId": "exasol",
"projectKey": "com.exasol:telemetry-java"
}
}
9 changes: 5 additions & 4 deletions src/test/java/com/exasol/telemetry/HttpTransportTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.exasol.telemetry;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.IOException;
Expand All @@ -22,7 +23,7 @@ class HttpTransportTest {
private static final String FEATURE = "projectTag.feature";

@Test
void sendsJsonPayloadToConfiguredClient() throws Exception {
void sendsJsonPayloadToConfiguredClient() throws IOException {
final CapturingRequestSender requestSender = new CapturingRequestSender(202);
final HttpTransport transport = new HttpTransport(
TelemetryConfig.builder(PROJECT_TAG).endpoint(URI.create(DUMMY_ENDPOINT)).build(),
Expand All @@ -38,7 +39,7 @@ void sendsJsonPayloadToConfiguredClient() throws Exception {
}

@Test
void rejectsNonSuccessStatusCodes() throws Exception {
void rejectsNonSuccessStatusCodes() {
final HttpTransport transport = new HttpTransport(
TelemetryConfig.builder(PROJECT_TAG).endpoint(URI.create(DUMMY_ENDPOINT)).build(),
request -> new HttpTransport.Response(500, "server says no"));
Expand All @@ -51,7 +52,7 @@ void rejectsNonSuccessStatusCodes() throws Exception {
}

@Test
void convertsInterruptedExceptionToIoException() throws Exception {
void convertsInterruptedExceptionToIoException() {
final HttpTransport transport = new HttpTransport(
TelemetryConfig.builder(PROJECT_TAG).endpoint(URI.create(DUMMY_ENDPOINT)).build(),
request -> {
Expand Down
16 changes: 14 additions & 2 deletions src/test/java/com/exasol/telemetry/TelemetryClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import java.net.URI;
import java.time.Duration;
Expand Down Expand Up @@ -38,15 +39,26 @@ void ignoresBlankFeatureName() {
}

@Test
void ignoresTrackingAfterCloseAndCloseIsIdempotent() {
void ignoresTrackingAfterClose() {
final TelemetryConfig config = TelemetryConfig.builder("project").endpoint(URI.create("https://example.com"))
.environment(new MapEnvironment(Map.of(TelemetryConfig.DISABLED_ENV, "true")))
.build();
final TelemetryClient client = TelemetryClient.create(config);

client.close();

assertDoesNotThrow(() -> client.track("feature"));
}

@Test
void makesCloseIdempotent() {
final TelemetryConfig config = TelemetryConfig.builder("project").endpoint(URI.create("https://example.com"))
.environment(new MapEnvironment(Map.of(TelemetryConfig.DISABLED_ENV, "true")))
.build();
final TelemetryClient client = TelemetryClient.create(config);

client.close();

client.track("feature");
assertDoesNotThrow(client::close);
}
}
27 changes: 14 additions & 13 deletions src/test/java/com/exasol/telemetry/TrackingApiIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
import org.junit.jupiter.api.Test;

class TrackingApiIT {
private static final String PROJECT_TAG = "shop-ui";
private static final String FEATURE = "checkout-started";

@Test
void recordsTaggedFeatureUsageEvent() throws Exception {
try (RecordingHttpServer server = RecordingHttpServer.createSuccessServer();
TelemetryClient client = TelemetryClient.create(server.configBuilder("shop-ui")
TelemetryClient client = TelemetryClient.create(server.configBuilder(PROJECT_TAG)
.retryTimeout(Duration.ofMillis(500))
.build())) {
client.track("checkout-started");
client.track(FEATURE);

final List<RecordingHttpServer.RecordedRequest> requests = server.awaitRequests(1, Duration.ofSeconds(2));
assertThat(requests, hasSize(1));
Expand All @@ -30,10 +33,10 @@ void recordsTaggedFeatureUsageEvent() throws Exception {
@Test
void emitsPayloadAsValidJson() throws Exception {
try (RecordingHttpServer server = RecordingHttpServer.createSuccessServer();
TelemetryClient client = TelemetryClient.create(server.configBuilder("shop-ui")
TelemetryClient client = TelemetryClient.create(server.configBuilder(PROJECT_TAG)
.retryTimeout(Duration.ofMillis(500))
.build())) {
client.track("checkout-started");
client.track(FEATURE);

final List<RecordingHttpServer.RecordedRequest> requests = server.awaitRequests(1, Duration.ofSeconds(2));
assertThat(requests, hasSize(1));
Expand All @@ -44,11 +47,11 @@ void emitsPayloadAsValidJson() throws Exception {
@Test
void keepsCallerThreadOverheadLowForAcceptedTracking() throws Exception {
try (RecordingHttpServer server = RecordingHttpServer.createDelayedSuccessServer(300);
TelemetryClient client = TelemetryClient.create(server.configBuilder("shop-ui")
TelemetryClient client = TelemetryClient.create(server.configBuilder(PROJECT_TAG)
.retryTimeout(Duration.ofMillis(500))
.build())) {
final long start = System.nanoTime();
client.track("checkout-started");
client.track(FEATURE);
final long elapsedMillis = Duration.ofNanos(System.nanoTime() - start).toMillis();

assertThat("track should return before the delayed HTTP request completes", elapsedMillis, lessThan(150L));
Expand All @@ -59,26 +62,24 @@ void keepsCallerThreadOverheadLowForAcceptedTracking() throws Exception {
@Test
void makesDisabledTrackingNoOpWithoutTelemetryOverhead() throws Exception {
try (RecordingHttpServer server = RecordingHttpServer.createSuccessServer()) {
final TelemetryConfig config = server.configBuilder("shop-ui")
final TelemetryConfig config = server.configBuilder(PROJECT_TAG)
.environment(new MapEnvironment(Map.of(TelemetryConfig.DISABLED_ENV, "disabled")))
.build();
final TelemetryClient client = new TelemetryClient(config, new FailingClock());
try {
client.track("checkout-started");

try (TelemetryClient client = new TelemetryClient(config, new FailingClock())) {
client.track(FEATURE);

assertThat(client.awaitStopped(Duration.ofMillis(10)), is(true));
assertThat(client.isRunning(), is(false));
assertThat(server.awaitRequests(1, Duration.ofMillis(150)), empty());
} finally {
client.close();
}
}
}

@Test
void ignoresInvalidFeatureNames() throws Exception {
try (RecordingHttpServer server = RecordingHttpServer.createSuccessServer();
TelemetryClient client = TelemetryClient.create(server.configBuilder("shop-ui").build())) {
TelemetryClient client = TelemetryClient.create(server.configBuilder(PROJECT_TAG).build())) {
client.track(" ");

Thread.sleep(150);
Expand Down