diff --git a/client/src/main/java/org/apache/rocketmq/client/common/ThreadLocalIndex.java b/client/src/main/java/org/apache/rocketmq/client/common/ThreadLocalIndex.java index ab223c3ecac23c373716c4b2f5844c404328c892..8f0f63f374f4fd712346d8fa7269d641652d6725 100644 --- a/client/src/main/java/org/apache/rocketmq/client/common/ThreadLocalIndex.java +++ b/client/src/main/java/org/apache/rocketmq/client/common/ThreadLocalIndex.java @@ -27,15 +27,10 @@ public class ThreadLocalIndex { Integer index = this.threadLocalIndex.get(); if (null == index) { index = Math.abs(random.nextInt()); - if (index < 0) - index = 0; this.threadLocalIndex.set(index); } index = Math.abs(index + 1); - if (index < 0) - index = 0; - this.threadLocalIndex.set(index); return index; } diff --git a/client/src/test/java/org/apache/rocketmq/client/common/ThreadLocalIndexTest.java b/client/src/test/java/org/apache/rocketmq/client/common/ThreadLocalIndexTest.java index 1be93ce0f3a1dd6d557d841c85109f3410bc40c6..6ff5dad2621c9be7c20d9904314cad09c150fc27 100644 --- a/client/src/test/java/org/apache/rocketmq/client/common/ThreadLocalIndexTest.java +++ b/client/src/test/java/org/apache/rocketmq/client/common/ThreadLocalIndexTest.java @@ -29,4 +29,11 @@ public class ThreadLocalIndexTest { assertThat(localIndex.getAndIncrement()).isEqualTo(initialVal + 1); } + @Test + public void testGetAndIncrement2() throws Exception { + ThreadLocalIndex localIndex = new ThreadLocalIndex(); + int initialVal = localIndex.getAndIncrement(); + assertThat(initialVal >= 0); + } + } \ No newline at end of file