Skip to content

Commit a4aecfe

Browse files
runningcodeclaude
andcommitted
ref(core): Build the hostname cache from a ticker, not the whole options
HostnameCache reads exactly one collaborator, so it now takes a MonotonicTicker rather than a SentryOptions it would only call getMonotonicTicker() on. This follows RateLimiter.create(MonotonicTicker, RateLimiterConfig), which names the collaborators a rate limiter actually reads for the same reason; a config interface is unnecessary here because there is only the one. Also drops SentryOptions.setHostnameCache(), which had no production caller. Unlike setDateProvider(), which AndroidOptionsInitializer uses, it was only a test seam, so MainEventProcessorTest overrides getHostnameCache() instead — the way CheckInUtilsTest already overrides getMonotonicTicker(). Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent c94d2f6 commit a4aecfe

5 files changed

Lines changed: 41 additions & 47 deletions

File tree

‎sentry/api/sentry.api‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3888,7 +3888,6 @@ public class io/sentry/SentryOptions : io/sentry/transport/RateLimiterConfig {
38883888
public fun setFullyDisplayedReporter (Lio/sentry/FullyDisplayedReporter;)V
38893889
public fun setGestureTargetLocators (Ljava/util/List;)V
38903890
public fun setGlobalHubMode (Ljava/lang/Boolean;)V
3891-
public fun setHostnameCache (Lio/sentry/HostnameCache;)V
38923891
public fun setIdleTimeout (Ljava/lang/Long;)V
38933892
public fun setIgnoredCheckIns (Ljava/util/List;)V
38943893
public fun setIgnoredErrors (Ljava/util/List;)V

‎sentry/src/main/java/io/sentry/HostnameCache.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,16 @@ public final class HostnameCache {
5454

5555
private final @NotNull ExecutorService executorService;
5656

57-
HostnameCache(final @NotNull SentryOptions options) {
57+
/**
58+
* Names the only collaborator a hostname cache reads, rather than taking the whole {@link
59+
* SentryOptions}.
60+
*
61+
* @param ticker the ticker the cache lifetime is measured on
62+
*/
63+
HostnameCache(final @NotNull MonotonicTicker ticker) {
5864
// avoid method refs on Android due to some issues with older AGP setups
5965
// noinspection Convert2MethodRef
60-
this(() -> InetAddress.getLocalHost(), options.getMonotonicTicker());
66+
this(() -> InetAddress.getLocalHost(), ticker);
6167
}
6268

6369
/**

‎sentry/src/main/java/io/sentry/SentryOptions.java‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ public class SentryOptions implements RateLimiterConfig {
539539
*/
540540
@ApiStatus.Internal
541541
private final @NotNull LazyEvaluator<HostnameCache> hostnameCache =
542-
new LazyEvaluator<>(() -> new HostnameCache(this));
542+
new LazyEvaluator<>(() -> new HostnameCache(getMonotonicTicker()));
543543

544544
private final @NotNull List<IPerformanceCollector> performanceCollectors = new ArrayList<>();
545545

@@ -3109,11 +3109,6 @@ public void setDateProvider(final @NotNull SentryDateProvider dateProvider) {
31093109
return hostnameCache.getValue();
31103110
}
31113111

3112-
@ApiStatus.Internal
3113-
public void setHostnameCache(final @NotNull HostnameCache hostnameCache) {
3114-
this.hostnameCache.setValue(hostnameCache);
3115-
}
3116-
31173112
/**
31183113
* Discards the cached instance, so that the next {@link #getHostnameCache()} builds a new one.
31193114
*

‎sentry/src/test/java/io/sentry/MainEventProcessorTest.kt‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,23 @@ import org.mockito.kotlin.whenever
2626

2727
class MainEventProcessorTest {
2828
class Fixture {
29-
val sentryOptions: SentryOptions =
30-
SentryOptions().apply {
31-
dsn = dsnString
32-
release = "release"
33-
dist = "dist"
34-
sdkVersion = SdkVersion("test", "1.2.3")
35-
}
3629
val scopes = mock<IScopes>()
3730
val getLocalhost = mock<InetAddress>()
3831
val hostnameCacheTicker = TestMonotonicTicker()
32+
// Built in getSut() rather than here: the constructor resolves the hostname straight away,
33+
// so it has to run after getLocalhost is stubbed.
34+
lateinit var hostnameCache: HostnameCache
35+
val sentryOptions: SentryOptions =
36+
object : SentryOptions() {
37+
// Qualified: an unqualified name here would resolve to this override, not the field.
38+
override fun getHostnameCache(): HostnameCache = this@Fixture.hostnameCache
39+
}
40+
.apply {
41+
dsn = dsnString
42+
release = "release"
43+
dist = "dist"
44+
sdkVersion = SdkVersion("test", "1.2.3")
45+
}
3946
lateinit var sentryTracer: SentryTracer
4047

4148
fun getSut(
@@ -73,8 +80,7 @@ class MainEventProcessorTest {
7380
}
7481
whenever(scopes.options).thenReturn(sentryOptions)
7582
sentryTracer = SentryTracer(TransactionContext("", ""), scopes)
76-
77-
sentryOptions.setHostnameCache(HostnameCache({ getLocalhost }, hostnameCacheTicker))
83+
hostnameCache = HostnameCache({ getLocalhost }, hostnameCacheTicker)
7884

7985
return MainEventProcessor(sentryOptions)
8086
}

‎sentry/src/test/java/io/sentry/SentryOptionsTest.kt‎

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import io.sentry.time.TestMonotonicTicker
99
import io.sentry.util.LazyEvaluator
1010
import io.sentry.util.StringUtils
1111
import java.io.File
12-
import java.net.InetAddress
1312
import java.net.Proxy
14-
import java.util.concurrent.Callable
1513
import kotlin.test.Test
1614
import kotlin.test.assertEquals
1715
import kotlin.test.assertFailsWith
@@ -1178,45 +1176,35 @@ class SentryOptionsTest {
11781176
private fun SentryOptions.peekHostnameCache(): HostnameCache? =
11791177
getProperty<LazyEvaluator<HostnameCache>>("hostnameCache").getProperty("value")
11801178

1179+
/** Options whose hostname cache resolves against a ticker the test controls. */
1180+
private fun optionsWithTicker(ticker: MonotonicTicker): SentryOptions =
1181+
object : SentryOptions() {
1182+
override fun getMonotonicTicker(): MonotonicTicker = ticker
1183+
}
1184+
11811185
@Test
1182-
fun `constructing options does not resolve the hostname`() {
1183-
val options = SentryOptions()
1184-
assertThat(options.peekHostnameCache()).isNull()
1186+
fun `hostname is resolved on first use, not when options are constructed`() {
1187+
val ticker = TestMonotonicTicker()
1188+
val options = optionsWithTicker(ticker)
11851189

1186-
// Asserting that peek reports an evaluated cache keeps the check above from passing vacuously.
1187-
options.hostnameCache = HostnameCache(Callable { mock<InetAddress>() }, TestMonotonicTicker())
1188-
assertThat(options.peekHostnameCache()).isNotNull()
1189-
}
1190+
assertThat(options.peekHostnameCache()).isNull()
11901191

1191-
@Test
1192-
fun `hostnameCache is created once and reused`() {
1193-
val options = SentryOptions()
1194-
val cache = HostnameCache(Callable { mock<InetAddress>() }, TestMonotonicTicker())
1195-
options.hostnameCache = cache
1192+
val cache = options.hostnameCache
11961193

1194+
// Also keeps the assertion above honest: peek does report a cache once one exists.
1195+
assertThat(options.peekHostnameCache()).isSameInstanceAs(cache)
11971196
assertThat(options.hostnameCache).isSameInstanceAs(cache)
1198-
assertThat(options.hostnameCache).isSameInstanceAs(cache)
1197+
assertThat(cache.getProperty<MonotonicTicker>("ticker")).isSameInstanceAs(ticker)
11991198
}
12001199

12011200
@Test
12021201
fun `resetHostnameCache discards the cached instance`() {
1203-
val options = SentryOptions()
1204-
options.hostnameCache = HostnameCache(Callable { mock<InetAddress>() }, TestMonotonicTicker())
1202+
val options = optionsWithTicker(TestMonotonicTicker())
1203+
val cache = options.hostnameCache
12051204

12061205
options.resetHostnameCache()
12071206

12081207
assertThat(options.peekHostnameCache()).isNull()
1209-
}
1210-
1211-
@Test
1212-
fun `hostnameCache measures its lifetime on the options ticker`() {
1213-
val ticker = TestMonotonicTicker()
1214-
val options =
1215-
object : SentryOptions() {
1216-
override fun getMonotonicTicker(): MonotonicTicker = ticker
1217-
}
1218-
1219-
assertThat(options.hostnameCache.getProperty<MonotonicTicker>("ticker"))
1220-
.isSameInstanceAs(ticker)
1208+
assertThat(options.hostnameCache).isNotSameInstanceAs(cache)
12211209
}
12221210
}

0 commit comments

Comments
 (0)