Skip to content

Commit 22fe8fc

Browse files
authored
Merge 2683cdb into b292090
2 parents b292090 + 2683cdb commit 22fe8fc

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Fixes
66

7+
- Optimize scope when maxBreadcrumb is 0 ([#4504](https://github.com/getsentry/sentry-java/pull/4504))
78
- No longer send out empty log envelopes ([#4497](https://github.com/getsentry/sentry-java/pull/4497))
89

910
### Dependencies

sentry/src/main/java/io/sentry/Scope.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ public Queue<Breadcrumb> getBreadcrumbs() {
466466
*/
467467
@Override
468468
public void addBreadcrumb(@NotNull Breadcrumb breadcrumb, @Nullable Hint hint) {
469-
if (breadcrumb == null) {
469+
if (breadcrumb == null || breadcrumbs instanceof DisabledQueue) {
470470
return;
471471
}
472472
if (hint == null) {
@@ -862,7 +862,7 @@ public void clearAttachments() {
862862
static @NotNull Queue<Breadcrumb> createBreadcrumbsList(final int maxBreadcrumb) {
863863
return maxBreadcrumb > 0
864864
? SynchronizedQueue.synchronizedQueue(new CircularFifoQueue<>(maxBreadcrumb))
865-
: SynchronizedQueue.synchronizedQueue(new DisabledQueue<>());
865+
: new DisabledQueue<>();
866866
}
867867

868868
/**

sentry/src/test/java/io/sentry/ScopeTest.kt

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import io.sentry.protocol.SentryId
66
import io.sentry.protocol.User
77
import io.sentry.test.callMethod
88
import org.junit.Assert.assertArrayEquals
9+
import org.mockito.kotlin.any
910
import org.mockito.kotlin.argThat
1011
import org.mockito.kotlin.check
1112
import org.mockito.kotlin.eq
1213
import org.mockito.kotlin.mock
1314
import org.mockito.kotlin.never
1415
import org.mockito.kotlin.times
1516
import org.mockito.kotlin.verify
17+
import org.mockito.kotlin.verifyNoInteractions
1618
import org.mockito.kotlin.whenever
1719
import java.util.concurrent.CopyOnWriteArrayList
1820
import kotlin.test.Test
@@ -339,6 +341,66 @@ class ScopeTest {
339341
assertEquals(1, scope.breadcrumbs.count())
340342
}
341343

344+
@Test
345+
fun `when adding breadcrumb and maxBreadcrumb is 0, beforeBreadcrumb is not executed`() {
346+
var called = false
347+
val options = SentryOptions().apply {
348+
maxBreadcrumbs = 0
349+
beforeBreadcrumb = SentryOptions.BeforeBreadcrumbCallback { breadcrumb, _ ->
350+
called = true
351+
breadcrumb
352+
}
353+
}
354+
355+
val scope = Scope(options)
356+
scope.addBreadcrumb(Breadcrumb())
357+
assertEquals(0, scope.breadcrumbs.count())
358+
assertFalse(called)
359+
}
360+
361+
@Test
362+
fun `when adding breadcrumb and maxBreadcrumb is not 0, beforeBreadcrumb is executed`() {
363+
var called = false
364+
val options = SentryOptions().apply {
365+
beforeBreadcrumb = SentryOptions.BeforeBreadcrumbCallback { breadcrumb, _ ->
366+
called = true
367+
breadcrumb
368+
}
369+
}
370+
371+
val scope = Scope(options)
372+
scope.addBreadcrumb(Breadcrumb())
373+
assertEquals(1, scope.breadcrumbs.count())
374+
assertTrue(called)
375+
}
376+
377+
@Test
378+
fun `when adding breadcrumb and maxBreadcrumb is 0, scopesObservers are not called`() {
379+
val observer = mock<IScopeObserver>()
380+
val options = SentryOptions().apply {
381+
maxBreadcrumbs = 0
382+
addScopeObserver(observer)
383+
}
384+
385+
val scope = Scope(options)
386+
scope.addBreadcrumb(Breadcrumb())
387+
assertEquals(0, scope.breadcrumbs.count())
388+
verifyNoInteractions(observer)
389+
}
390+
391+
@Test
392+
fun `when adding breadcrumb and maxBreadcrumb is not 0, scopesObservers are called`() {
393+
val observer = mock<IScopeObserver>()
394+
val options = SentryOptions().apply {
395+
addScopeObserver(observer)
396+
}
397+
398+
val scope = Scope(options)
399+
scope.addBreadcrumb(Breadcrumb())
400+
assertEquals(1, scope.breadcrumbs.count())
401+
verify(observer).addBreadcrumb(any())
402+
}
403+
342404
@Test
343405
fun `when adding eventProcessor, eventProcessor should be in the list`() {
344406
val processor = CustomEventProcessor()

0 commit comments

Comments
 (0)