1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
package org.jgroups.blocks;
import org.jgroups.Address;
import org.jgroups.Global;
import org.jgroups.util.UUID;
import org.jgroups.util.Util;
import org.testng.annotations.Test;
import java.util.Arrays;
import java.util.List;
/**
* @author Bela Ban
*/
@Test(groups=Global.FUNCTIONAL,sequential=false)
public class LazyRemovalSetTest {
public static void testAdd() {
LazyRemovalSet<UUID> cache=new LazyRemovalSet<UUID>();
UUID uuid=UUID.randomUUID();
cache.add(uuid);
System.out.println("cache = " + cache);
assert 1 == cache.size();
assert cache.contains(uuid);
cache.remove(uuid);
System.out.println("cache = " + cache);
assert cache.contains(uuid);
}
public static void testRemoveAll() {
LazyRemovalSet<String> cache=new LazyRemovalSet<String>(10, 0);
cache.add("one", "two", "three", "four", "five", "two");
System.out.println("cache = " + cache);
assert cache.size() == 5;
List<String> list=Arrays.asList("four", "two");
System.out.println("removing " + list);
cache.removeAll(list);
System.out.println("cache = " + cache);
assert cache.size() == 5;
cache.removeMarkedElements();
System.out.println("cache = " + cache);
assert cache.size() == 3;
assert cache.contains("one");
assert cache.contains("three");
assert cache.contains("five");
}
public static void testRetainAll() {
LazyRemovalSet<String> cache=new LazyRemovalSet<String>(10, 0);
cache.add("one", "two", "three");
System.out.println("cache = " + cache);
assert cache.size() == 3;
List<String> retain=Arrays.asList("two", "three");
System.out.println("retaining " + retain);
cache.retainAll(retain);
System.out.println("cache = " + cache);
assert cache.size() == 3;
assert cache.contains("two");
assert cache.contains("three");
cache.removeMarkedElements();
System.out.println("cache = " + cache);
assert cache.size() == 2;
}
public static void testRemovalOnExceedingMaxSize() {
LazyRemovalSet<String> cache=new LazyRemovalSet<String>(2, 0);
cache.add("u1", "u2", "u3", "u4");
assert cache.size() == 4;
cache.remove("u3");
System.out.println("cache = " + cache);
assert cache.size() == 3;
cache.remove("u1");
System.out.println("cache = " + cache);
assert cache.size() == 2;
cache.remove("u4");
System.out.println("cache = " + cache);
assert cache.size() == 2;
cache.removeMarkedElements();
System.out.println("cache = " + cache);
assert cache.size() == 1;
}
public static void testRemovalOnExceedingMaxSizeAndMaxTime() {
LazyRemovalSet<String> cache=new LazyRemovalSet<String>(2, 1000);
cache.add("u1", "u2", "u3", "u4");
System.out.println("cache = " + cache);
assert cache.size() == 4;
cache.remove("u3");
System.out.println("cache = " + cache);
assert cache.size() == 4;
cache.remove("u1");
System.out.println("cache = " + cache);
assert cache.size() == 4;
cache.remove("u4");
System.out.println("cache = " + cache);
assert cache.size() == 4;
cache.removeMarkedElements();
System.out.println("cache = " + cache);
assert cache.size() == 4;
System.out.println("sleeping for 1 sec");
Util.sleep(1100);
cache.remove("u4");
System.out.println("cache = " + cache);
assert cache.size() == 1;
}
public static void testCapacityExceeded() {
LazyRemovalSet<Integer> cache=new LazyRemovalSet<Integer>(5, 0);
for(int i=1; i <=10; i++)
cache.add(i);
System.out.println("cache = " + cache);
assert cache.size() == 10;
cache.retainAll(Arrays.asList(1,4,6,8));
cache.add(11);
System.out.println("cache = " + cache);
assert cache.size() == 5;
}
public static void testContains() {
LazyRemovalSet<Address> cache=new LazyRemovalSet<Address>(5, 0);
Address a=Util.createRandomAddress("A"),
b=Util.createRandomAddress("B"),
c=Util.createRandomAddress("C"),
d=Util.createRandomAddress("D");
cache.add(a,b,c,d);
System.out.println("cache = " + cache);
assert cache.size() == 4;
assert cache.contains(a);
assert cache.contains(b);
assert cache.contains(d);
assert cache.contains(d);
cache.retainAll(Arrays.asList(a,c));
System.out.println("cache = " + cache);
assert cache.size() == 4;
cache.removeMarkedElements();
System.out.println("cache = " + cache);
assert cache.size() == 2;
}
public static void testReAddition() {
LazyRemovalSet<String> cache=new LazyRemovalSet<String>(1, 1000);
cache.add("one", "two", "three");
Util.sleep(1500);
System.out.println("cache = " + cache);
cache.add("two");
System.out.println("cache = " + cache);
cache.clear(false);
cache.removeMarkedElements();
System.out.println("cache = " + cache);
assert cache.size() == 1;
assert cache.contains("two");
}
}
|