Skip to content

Commit b53a0de

Browse files
lwahlmeierjentfoo
authored andcommitted
fixed issue in rollback if the buffers that where given are in use
1 parent 9f78665 commit b53a0de

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/main/java/org/threadly/litesockets/buffers/TransactionalByteBuffers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public int get(final byte[] destBytes) {
135135
public ByteBuffer pullBuffer(final int size) {
136136
if(lock.isLocked()) {
137137
if(lock.isHeldByCurrentThread()) {
138-
final ByteBuffer bb = super.pullBuffer(size);
138+
final ByteBuffer bb = super.pullBuffer(size).duplicate();
139139
consumedSinceBegin+=size;
140140
return bb;
141141
} else {

src/test/java/org/threadly/litesockets/buffers/TransactionalByteBuffersTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.junit.Assert.assertTrue;
55

66
import java.nio.ByteBuffer;
7+
import java.util.ArrayList;
78
import java.util.concurrent.atomic.AtomicBoolean;
89

910
import org.junit.After;
@@ -62,6 +63,27 @@ public boolean get() {
6263
}
6364
}
6465

66+
67+
@Test
68+
public void rollBackWithBuffersActive() {
69+
int bufferSize = 1000000;
70+
TransactionalByteBuffers tbb = new TransactionalByteBuffers();
71+
tbb.add(ByteBuffer.allocate(bufferSize));
72+
ArrayList<ByteBuffer> lbb =new ArrayList<>();
73+
tbb.begin();
74+
while(tbb.hasRemaining()) {
75+
lbb.add(tbb.pullBuffer(Math.min(100, tbb.remaining())));
76+
}
77+
tbb.rollback();
78+
int countSize = 0;
79+
for (ByteBuffer bb : lbb) {
80+
countSize+=bb.remaining();
81+
}
82+
assertEquals(bufferSize, countSize);
83+
assertEquals(bufferSize, tbb.remaining());
84+
85+
}
86+
6587
@Test
6688
public void getArrayTest() {
6789
String s = "TEST1234567890";

0 commit comments

Comments
 (0)