Skip to content

Commit 64e7df9

Browse files
committed
refactor(java-agent): replace resetForTesting with injectable Provider<Long> in AgentTelemetryStore
1 parent ad0b2ac commit 64e7df9

4 files changed

Lines changed: 26 additions & 5 deletions

File tree

rollbar-java-agent/src/main/java/com/rollbar/agent/AgentTelemetryStore.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.rollbar.agent;
22

33
import com.rollbar.api.payload.data.TelemetryEvent;
4+
import com.rollbar.notifier.provider.Provider;
45
import com.rollbar.notifier.telemetry.RollbarTelemetryEventTracker;
56
import com.rollbar.notifier.telemetry.TelemetryEventTracker;
67

@@ -21,7 +22,7 @@ public static List<TelemetryEvent> getAll() {
2122
return INSTANCE.getAll();
2223
}
2324

24-
public static void resetForTesting() {
25-
INSTANCE = new RollbarTelemetryEventTracker(System::currentTimeMillis, 100);
25+
public static void init(Provider<Long> timestampProvider) {
26+
INSTANCE = new RollbarTelemetryEventTracker(timestampProvider, 100);
2627
}
2728
}

rollbar-java-agent/src/test/java/com/rollbar/agent/RollbarAgentTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
package com.rollbar.agent;
22

3+
import com.rollbar.api.payload.data.TelemetryEvent;
34
import com.rollbar.notifier.telemetry.TelemetryEventTracker;
45
import org.junit.jupiter.api.BeforeEach;
56
import org.junit.jupiter.api.Test;
67

8+
import java.util.List;
9+
710
import static org.junit.jupiter.api.Assertions.*;
811

12+
913
public class RollbarAgentTest {
1014

1115
@BeforeEach
1216
public void setUp() {
13-
AgentTelemetryStore.resetForTesting();
17+
AgentTelemetryStore.init(System::currentTimeMillis);
1418
}
1519

1620
@Test
@@ -20,6 +24,22 @@ public void getTelemetryTracker_returnsSingletonInstance() {
2024
assertSame(first, second);
2125
}
2226

27+
@Test
28+
public void init_withCustomTimestamp_usesProvidedTimestamp() {
29+
long fixedTime = 1_000_000L;
30+
AgentTelemetryStore.init(() -> fixedTime);
31+
32+
AgentTelemetryStore.getInstance().recordManualEventFor(
33+
com.rollbar.api.payload.data.Level.WARNING,
34+
com.rollbar.api.payload.data.Source.CLIENT,
35+
"test"
36+
);
37+
38+
List<TelemetryEvent> events = AgentTelemetryStore.getInstance().getAll();
39+
assertEquals(1, events.size());
40+
assertEquals(fixedTime, events.get(0).asJson().get("timestamp_ms"));
41+
}
42+
2343
@Test
2444
public void urlSanitizer_stripsQueryAndFragment() {
2545
String sanitized = UrlSanitizer.sanitize("https://api.example.com/path?token=secret#section");

rollbar-java-agent/src/test/java/com/rollbar/agent/instrumentation/HttpURLConnectionInstrumentationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class HttpURLConnectionInstrumentationTest {
2626
public void setUp() {
2727
server = new WireMockServer(WireMockConfiguration.wireMockConfig().dynamicPort());
2828
server.start();
29-
AgentTelemetryStore.resetForTesting();
29+
AgentTelemetryStore.init(System::currentTimeMillis);
3030
NetworkEventBridge.resetRecordedForTesting();
3131
}
3232

rollbar-java-agent/src/test/java/com/rollbar/agent/instrumentation/JavaHttpClientInstrumentationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void setUp() {
2929
server = new WireMockServer(WireMockConfiguration.wireMockConfig().dynamicPort());
3030
server.start();
3131
client = HttpClient.newHttpClient();
32-
AgentTelemetryStore.resetForTesting();
32+
AgentTelemetryStore.init(System::currentTimeMillis);
3333
NetworkEventBridge.resetRecordedForTesting();
3434
}
3535

0 commit comments

Comments
 (0)