Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
updated LazyEvaluator javadoc
added test for LazyEvaluator.resetValue
setting a new dsn resets the cached parsed dsn
  • Loading branch information
stefanosiano committed Oct 16, 2024
commit 0c26bbc4a2285bac2b3231d0d37c2579337039b3
12 changes: 5 additions & 7 deletions sentry/src/main/java/io/sentry/SentryOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ public void addIntegration(@NotNull Integration integration) {
}

/**
* Returns the DSN
* Returns the DSN.
*
* @return the DSN or null if not set
*/
Expand All @@ -541,13 +541,13 @@ public void addIntegration(@NotNull Integration integration) {
}

/**
* Returns the DSN
* Evaluates and parses the DSN. May throw an exception if the DSN is invalid.
*
* @return the DSN or null if not set
* @return the parsed DSN or throws if dsn is invalid
*/
@ApiStatus.Internal
@NotNull
Dsn getParsedDsn() {
Dsn getParsedDsn() throws IllegalArgumentException {
return parsedDsn.getValue();
}

Expand All @@ -558,9 +558,7 @@ Dsn getParsedDsn() {
*/
public void setDsn(final @Nullable String dsn) {
this.dsn = dsn;
if (!isEnabled() || (dsn != null && dsn.isEmpty())) {
this.parsedDsn.resetValue();
}
this.parsedDsn.resetValue();

dsnHash = StringUtils.calculateStringHash(this.dsn, logger);
}
Expand Down
7 changes: 6 additions & 1 deletion sentry/src/main/java/io/sentry/util/LazyEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public LazyEvaluator(final @NotNull Evaluator<T> evaluator) {
}

/**
* Executes the evaluator function and caches its result, so that it's called only once.
* Executes the evaluator function and caches its result, so that it's called only once, unless
* resetValue is called.
*
* @return The result of the evaluator function.
*/
Expand All @@ -48,6 +49,10 @@ public void setValue(final @Nullable T value) {
}
}

/**
* Resets the internal value and forces the evaluator function to be called the next time
* getValue() is called.
*/
public void resetValue() {
synchronized (this) {
this.value = null;
Expand Down
14 changes: 14 additions & 0 deletions sentry/src/test/java/io/sentry/util/LazyEvaluatorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,18 @@ class LazyEvaluatorTest {
assertEquals(1, evaluator.value)
assertEquals(1, fixture.count)
}

@Test
fun `evaluates again after resetValue`() {
val evaluator = fixture.getSut()
assertEquals(0, fixture.count)
assertEquals(1, evaluator.value)
assertEquals(1, evaluator.value)
assertEquals(1, fixture.count)
// Evaluate again, only once
evaluator.resetValue()
assertEquals(2, evaluator.value)
assertEquals(2, evaluator.value)
assertEquals(2, fixture.count)
}
}