Skip to content

Commit

Permalink
Fix: Cloning Stack (#1483)
Browse files Browse the repository at this point in the history
The root item was added twice, first as exactly the same reference, second as a cloned item.

This fix ensures that root item is added only once as a clone.
  • Loading branch information
maciejwalkowiak committed May 20, 2021
1 parent c68d7be commit 248e549
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Unreleased

* Feat: Add breadcrumb in Spring RestTemplate integration (#1481)
* Fix: Cloning Stack (#1483)

# 5.0.0-beta.4

Expand Down
12 changes: 9 additions & 3 deletions sentry/src/main/java/io/sentry/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.sentry.util.Objects;
import java.util.Deque;
import java.util.Iterator;
import java.util.concurrent.LinkedBlockingDeque;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -60,9 +61,14 @@ public Stack(final @NotNull ILogger logger, final @NotNull StackItem rootStackIt
}

public Stack(final @NotNull Stack stack) {
this(stack.logger, stack.items.getFirst());
for (final StackItem item : stack.items) {
push(new StackItem(item));
this(stack.logger, new StackItem(stack.items.getFirst()));
final Iterator<StackItem> iterator = stack.items.iterator();
// skip first item (root item)
if (iterator.hasNext()) {
iterator.next();
}
while (iterator.hasNext()) {
push(new StackItem(iterator.next()));
}
}

Expand Down
19 changes: 14 additions & 5 deletions sentry/src/test/java/io/sentry/StackTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,21 @@ class StackTest {
@Test
fun `cloning stack clones stack items`() {
val stack = fixture.getSut()
stack.push(StackItem(fixture.options, fixture.client, fixture.scope))
val clone = Stack(stack)

val stackRootItem = stack.peek()
val cloneRootItem = clone.peek()
assertNotEquals(stackRootItem, cloneRootItem)
assertNotEquals(stackRootItem.scope, cloneRootItem.scope)
assertEquals(stackRootItem.client, cloneRootItem.client)
assertEquals(stack.size(), clone.size())
// assert first stack item
assertStackItems(stack.peek(), clone.peek())
stack.pop()
clone.pop()
// assert root item
assertStackItems(stack.peek(), clone.peek())
}

private fun assertStackItems(item1: StackItem, item2: StackItem) {
assertNotEquals(item1, item2)
assertNotEquals(item1.scope, item2.scope)
assertEquals(item1.client, item2.client)
}
}

0 comments on commit 248e549

Please sign in to comment.