Skip to content

Commit

Permalink
fix the bug of concurrent usage of toString in TransactionCapsule (#5696
Browse files Browse the repository at this point in the history
)
  • Loading branch information
317787106 authored Jan 26, 2024
1 parent baddd4f commit cbf2417
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ public class TransactionCapsule implements ProtoCapsule<Transaction> {
@Setter
private TransactionTrace trxTrace;

private StringBuilder toStringBuff = new StringBuilder();
@Getter
@Setter
private long time;
Expand Down Expand Up @@ -738,7 +737,7 @@ public Transaction getInstance() {

@Override
public String toString() {

StringBuilder toStringBuff = new StringBuilder();
toStringBuff.setLength(0);
toStringBuff.append("TransactionCapsule \n[ ");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import com.google.protobuf.ByteString;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
Expand Down Expand Up @@ -431,4 +432,22 @@ public void estimateConsumeBandWidthSizeCorner() {
long actual = TransactionUtil.estimateConsumeBandWidthSize(dps, balance);
Assert.assertEquals(expected, actual);
}

@Test
public void testConcurrentToString() throws InterruptedException {
Transaction.Builder builder = Transaction.newBuilder();
TransactionCapsule trx = new TransactionCapsule(builder.build());
List<Thread> threadList = new ArrayList<>();
int n = 10;
for (int i = 0; i < n; i++) {
threadList.add(new Thread(() -> trx.toString()));
}
for (int i = 0; i < n; i++) {
threadList.get(i).start();
}
for (int i = 0; i < n; i++) {
threadList.get(i).join();
}
Assert.assertTrue(true);
}
}

0 comments on commit cbf2417

Please sign in to comment.