Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,13 @@ public TraceStateBuilder remove(String key) {
}
for (int i = 0; i < reversedEntries.size(); i += 2) {
if (reversedEntries.get(i).equals(key)) {
reversedEntries.set(i + 1, null);
numEntries--;
// Only account for the removal if the entry is still present. A repeated remove of an
// already-removed key must be a no-op, mirroring the guard in put(); otherwise numEntries
// is decremented twice and build() drops unrelated entries or emits a null-valued entry.
Comment on lines +95 to +97

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this comment can probably be tightened. The symmetry with put() is visible a few lines up, and the failure modes are documented in the PR description / tests. Something like:

// Mirror the tombstone guard in put(): skip already-removed entries so numEntries stays accurate.

if (reversedEntries.get(i + 1) != null) {
reversedEntries.set(i + 1, null);
numEntries--;
}
return this;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,40 @@ void removeNotPresent() {
.isEqualTo(multiValueTraceState);
}

@Test
void removeAlreadyRemovedKeyKeepsOtherEntries() {
TraceState state =
TraceState.builder()
.put("a", "1")
.put("b", "2")
.remove("a")
.remove("a") // removing an already-removed key must be a no-op
.build();
assertThat(state.get("b")).isEqualTo("2");
assertThat(state.size()).isEqualTo(1);
}

@Test
void removeAlreadyRemovedKeyDoesNotProduceNullValue() {
TraceState state = TraceState.builder().put("a", "1").remove("a").remove("a").build();
assertThat(state.isEmpty()).isTrue();
assertThat(state.get("a")).isNull();
}

@Test
void removeAlreadyRemovedKeyWithMultipleEntriesDoesNotThrow() {
assertThatCode(
() ->
TraceState.builder()
.put("a", "1")
.put("b", "2")
.put("c", "3")
.remove("a")
.remove("a")
.build())
.doesNotThrowAnyException();
}
Comment on lines +327 to +338

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is largely redundant with removeAlreadyRemovedKeyKeepsOtherEntries. Let's drop it.


@Test
void addAndRemoveEntry() {
assertThat(
Expand Down