Search in sources :

Example 1 with COUNTER

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;
}
Also used : FORK(org.jgroups.protocols.FORK) CounterService(org.jgroups.blocks.atomic.CounterService) Counter(org.jgroups.blocks.atomic.Counter) ForkChannel(org.jgroups.fork.ForkChannel) COUNTER(org.jgroups.protocols.COUNTER)

Aggregations

Counter (org.jgroups.blocks.atomic.Counter)1 CounterService (org.jgroups.blocks.atomic.CounterService)1 ForkChannel (org.jgroups.fork.ForkChannel)1 COUNTER (org.jgroups.protocols.COUNTER)1 FORK (org.jgroups.protocols.FORK)1