Skip to content
Browse files

Added RingBuffer.getMissing()

  • Loading branch information...
1 parent 2c7feec commit a610dd1e137c81d80e378ae543e157ccc539340a @belaban committed Dec 21, 2011
Showing with 39 additions and 3 deletions.
  1. +23 −1 src/org/jgroups/util/RingBuffer.java
  2. +16 −2 tests/junit-functional/org/jgroups/tests/RingBufferTest.java
View
24 src/org/jgroups/util/RingBuffer.java
@@ -188,6 +188,28 @@ public int missing() {
return count(true);
}
+ public SeqnoList getMissing() {
+ SeqnoList missing=null;
+ long tmp_hd=hd, tmp_hr=hr.get();
+ for(long i=tmp_hd+1; i <= tmp_hr; i++) {
+ if(buf.get(index(i)) == null) {
+ if(missing == null)
+ missing=new SeqnoList();
+
+ long end=i;
+ while(buf.get(index(end+1)) == null && end <= tmp_hr)
+ end++;
+
+ if(end == i)
+ missing.add(i);
+ else
+ missing.add(i, end);
+ i=end;
+ }
+ }
+ return missing;
+ }
+
public String toString() {
StringBuilder sb=new StringBuilder();
@@ -196,7 +218,7 @@ public String toString() {
}
- protected static void validate(long seqno) {
+ protected static final void validate(long seqno) {
if(seqno < 0)
throw new IllegalArgumentException("seqno " + seqno + " cannot be negative");
}
View
18 tests/junit-functional/org/jgroups/tests/RingBufferTest.java
@@ -1,6 +1,8 @@
package org.jgroups.tests;
+import org.jgroups.Global;
import org.jgroups.util.RingBuffer;
+import org.jgroups.util.SeqnoList;
import org.jgroups.util.Util;
import org.testng.annotations.Test;
@@ -11,8 +13,7 @@
* @author Bela Ban
* @since 3.1
*/
-// todo: add tests for long overflow (can become negative)
-@Test(description="Functional tests for the RingBuffer class")
+@Test(groups=Global.FUNCTIONAL,description="Functional tests for the RingBuffer class")
public class RingBufferTest {
public void testConstructor() {
@@ -89,6 +90,19 @@ public void testAddMissing() {
assert num == null;
}
+
+ public void testGetMissing() {
+ RingBuffer<Integer> buf=new RingBuffer<Integer>(30, 0);
+ for(int i: Arrays.asList(2,5,10,11,12,13,15,20,28,30))
+ buf.add(i, i);
+ System.out.println("buf = " + buf);
+ int missing=buf.missing();
+ System.out.println("missing=" + missing);
+ SeqnoList missing_list=buf.getMissing();
+ System.out.println("missing_list = " + missing_list);
+ assert missing_list.size() == missing;
+ }
+
public void testBlockingAddAndDestroy() {
final RingBuffer<Integer> buf=new RingBuffer<Integer>(10, 0);
for(int i=0; i <= 10; i++)

0 comments on commit a610dd1

Please sign in to comment.
Something went wrong with that request. Please try again.