use of org.jgroups.protocols.COUNTER in project JGroups by belaban.
the class ForkChannelTest method testCounterService.
/**
* Tests CounterService on 2 different fork-channels, using the *same* fork-stack. This means the 2 counter
* services will 'see' each other and the counters must have the same value
* @throws Exception
*/
public void testCounterService() throws Exception {
a.connect(CLUSTER);
fc1 = new ForkChannel(a, "stack", "fc1", false, ProtocolStack.Position.ABOVE, FORK.class, new COUNTER());
fc2 = new ForkChannel(a, "stack", "fc2", false, ProtocolStack.Position.ABOVE, FORK.class, new COUNTER());
fc1.connect("foo");
fc2.connect("bar");
CounterService cs1 = new CounterService(fc1), cs2 = new CounterService(fc2);
Counter c1 = cs1.getOrCreateCounter("counter", 1), c2 = cs2.getOrCreateCounter("counter", 1);
System.out.println("counter1=" + c1 + ", counter2=" + c2);
assert c1.get() == 1 && c2.get() == 1;
c1.addAndGet(5);
System.out.println("counter1=" + c1 + ", counter2=" + c2);
assert c1.get() == 6 && c2.get() == 6;
c2.compareAndSet(6, 10);
System.out.println("counter1=" + c1 + ", counter2=" + c2);
assert c1.get() == 10 && c2.get() == 10;
}
Aggregations