Skip to content

Commit

Permalink
fix: Reset stateful variables in WritableQueueStateBase on commit() (
Browse files Browse the repository at this point in the history
…#17905)

Signed-off-by: Neeharika-Sompalli <[email protected]>
  • Loading branch information
Neeharika-Sompalli authored Feb 14, 2025
1 parent 63cdf34 commit deeb01d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023-2024 Hedera Hashgraph, LLC
* Copyright (C) 2023-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.inOrder;

import com.swirlds.state.spi.QueueChangeListener;
Expand Down Expand Up @@ -341,6 +342,23 @@ void commitAfterRemovingSomeAddedElementsOnlyIncludesAdded() {
assertThat(backingList).containsExactly(CHEMISTRY);
}

@Test
void commitResetsIndex() {
final var backingList = new LinkedList<String>();
final var subject = new ListWritableQueueState<>(STEAM_STATE_KEY, backingList);
subject.add(ART);
subject.add(BIOLOGY);
subject.removeIf(s -> true);
assertEquals(BIOLOGY, subject.peek());
subject.removeIf(s -> true);
subject.commit();
assertThat(backingList).isEmpty();

subject.add(ART);
subject.add(ECOLOGY);
assertEquals(ART, subject.peek());
}

@Test
void commitAfterPeekingAndAddingStillAddsEverything() {
final var backingList = new LinkedList<String>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023-2024 Hedera Hashgraph, LLC
* Copyright (C) 2023-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -114,6 +114,7 @@ public final void reset() {
addedElements.clear();
peekedElement = null;
dsIterator = null;
currentAddedElementIndex = 0;
}

@NonNull
Expand Down

0 comments on commit deeb01d

Please sign in to comment.