Skip to content

Commit 7a59f20

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 36a8567 commit 7a59f20

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
@@ -3981,7 +3981,6 @@ public class io/sentry/SentryOptions : io/sentry/transport/RateLimiterConfig {
39813981
public fun setFullyDisplayedReporter (Lio/sentry/FullyDisplayedReporter;)V
39823982
public fun setGestureTargetLocators (Ljava/util/List;)V
39833983
public fun setGlobalHubMode (Ljava/lang/Boolean;)V
3984-
public fun setHostnameCache (Lio/sentry/HostnameCache;)V
39853984
public fun setIdleTimeout (Ljava/lang/Long;)V
39863985
public fun setIgnoredCheckIns (Ljava/util/List;)V
39873986
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
@@ -55,10 +55,16 @@ public final class HostnameCache {
5555

5656
private final @NotNull ExecutorService executorService;
5757

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

6470
/**

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ public class SentryOptions implements RateLimiterConfig {
544544
*/
545545
@ApiStatus.Internal
546546
private final @NotNull LazyEvaluator<HostnameCache> hostnameCache =
547-
new LazyEvaluator<>(() -> new HostnameCache(this));
547+
new LazyEvaluator<>(() -> new HostnameCache(getMonotonicTicker()));
548548

549549
private final @NotNull List<IPerformanceCollector> performanceCollectors = new ArrayList<>();
550550

@@ -3157,11 +3157,6 @@ public void setDateProvider(final @NotNull SentryDateProvider dateProvider) {
31573157
return hostnameCache.getValue();
31583158
}
31593159

3160-
@ApiStatus.Internal
3161-
public void setHostnameCache(final @NotNull HostnameCache hostnameCache) {
3162-
this.hostnameCache.setValue(hostnameCache);
3163-
}
3164-
31653160
/**
31663161
* Discards the cached instance, so that the next {@link #getHostnameCache()} builds a new one.
31673162
*

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

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

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

4249
fun getSut(
@@ -74,8 +81,7 @@ class MainEventProcessorTest {
7481
}
7582
whenever(scopes.options).thenReturn(sentryOptions)
7683
sentryTracer = SentryTracer(TransactionContext("", ""), scopes)
77-
78-
sentryOptions.setHostnameCache(HostnameCache({ getLocalhost }, hostnameCacheTicker))
84+
hostnameCache = HostnameCache({ getLocalhost }, hostnameCacheTicker)
7985

8086
return MainEventProcessor(sentryOptions)
8187
}

‎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
@@ -1341,45 +1339,35 @@ class SentryOptionsTest {
13411339
private fun SentryOptions.peekHostnameCache(): HostnameCache? =
13421340
getProperty<LazyEvaluator<HostnameCache>>("hostnameCache").getProperty("value")
13431341

1342+
/** Options whose hostname cache resolves against a ticker the test controls. */
1343+
private fun optionsWithTicker(ticker: MonotonicTicker): SentryOptions =
1344+
object : SentryOptions() {
1345+
override fun getMonotonicTicker(): MonotonicTicker = ticker
1346+
}
1347+
13441348
@Test
1345-
fun `constructing options does not resolve the hostname`() {
1346-
val options = SentryOptions()
1347-
assertThat(options.peekHostnameCache()).isNull()
1349+
fun `hostname is resolved on first use, not when options are constructed`() {
1350+
val ticker = TestMonotonicTicker()
1351+
val options = optionsWithTicker(ticker)
13481352

1349-
// Asserting that peek reports an evaluated cache keeps the check above from passing vacuously.
1350-
options.hostnameCache = HostnameCache(Callable { mock<InetAddress>() }, TestMonotonicTicker())
1351-
assertThat(options.peekHostnameCache()).isNotNull()
1352-
}
1353+
assertThat(options.peekHostnameCache()).isNull()
13531354

1354-
@Test
1355-
fun `hostnameCache is created once and reused`() {
1356-
val options = SentryOptions()
1357-
val cache = HostnameCache(Callable { mock<InetAddress>() }, TestMonotonicTicker())
1358-
options.hostnameCache = cache
1355+
val cache = options.hostnameCache
13591356

1357+
// Also keeps the assertion above honest: peek does report a cache once one exists.
1358+
assertThat(options.peekHostnameCache()).isSameInstanceAs(cache)
13601359
assertThat(options.hostnameCache).isSameInstanceAs(cache)
1361-
assertThat(options.hostnameCache).isSameInstanceAs(cache)
1360+
assertThat(cache.getProperty<MonotonicTicker>("ticker")).isSameInstanceAs(ticker)
13621361
}
13631362

13641363
@Test
13651364
fun `resetHostnameCache discards the cached instance`() {
1366-
val options = SentryOptions()
1367-
options.hostnameCache = HostnameCache(Callable { mock<InetAddress>() }, TestMonotonicTicker())
1365+
val options = optionsWithTicker(TestMonotonicTicker())
1366+
val cache = options.hostnameCache
13681367

13691368
options.resetHostnameCache()
13701369

13711370
assertThat(options.peekHostnameCache()).isNull()
1372-
}
1373-
1374-
@Test
1375-
fun `hostnameCache measures its lifetime on the options ticker`() {
1376-
val ticker = TestMonotonicTicker()
1377-
val options =
1378-
object : SentryOptions() {
1379-
override fun getMonotonicTicker(): MonotonicTicker = ticker
1380-
}
1381-
1382-
assertThat(options.hostnameCache.getProperty<MonotonicTicker>("ticker"))
1383-
.isSameInstanceAs(ticker)
1371+
assertThat(options.hostnameCache).isNotSameInstanceAs(cache)
13841372
}
13851373
}

0 commit comments

Comments
 (0)