Search in sources :

Example 1 with ForkChannel

use of org.jgroups.fork.ForkChannel in project JGroups by belaban.

the class ForkChannelTest method testRefcount.

public void testRefcount() throws Exception {
    FORK fork = a.getProtocolStack().findProtocol(FORK.class);
    Protocol prot = fork.get("stack");
    assert prot == null;
    fc1 = new ForkChannel(a, "stack", "fc1");
    prot = fork.get("stack");
    assert prot != null;
    ForkProtocolStack fork_stack = (ForkProtocolStack) getProtStack(prot);
    int inits = fork_stack.getInits();
    assert inits == 1 : "inits is " + inits + "(expected 1)";
    // uses the same fork stack "stack"
    fc2 = new ForkChannel(a, "stack", "fc2");
    inits = fork_stack.getInits();
    assert inits == 2 : "inits is " + inits + "(expected 2)";
    a.connect(CLUSTER);
    fc1.connect(CLUSTER);
    int connects = fork_stack.getConnects();
    assert connects == 1 : "connects is " + connects + "(expected 1)";
    // duplicate connect()
    fc1.connect(CLUSTER);
    connects = fork_stack.getConnects();
    assert connects == 1 : "connects is " + connects + "(expected 1)";
    fc2.connect(CLUSTER);
    connects = fork_stack.getConnects();
    assert connects == 2 : "connects is " + connects + "(expected 2)";
    fc2.disconnect();
    // duplicate disconnect() !
    fc2.disconnect();
    connects = fork_stack.getConnects();
    assert connects == 1 : "connects is " + connects + "(expected 1)";
    Util.close(fc2);
    inits = fork_stack.getInits();
    assert inits == 1 : "inits is " + inits + "(expected 1)";
    // duplicate close()
    Util.close(fc2);
    inits = fork_stack.getInits();
    assert inits == 1 : "inits is " + inits + "(expected 1)";
    Util.close(fc1);
    connects = fork_stack.getConnects();
    assert connects == 0 : "connects is " + connects + "(expected 0)";
    inits = fork_stack.getInits();
    assert inits == 0 : "inits is " + inits + "(expected 0)";
    prot = fork.get("stack");
    assert prot == null;
}
Also used : FORK(org.jgroups.protocols.FORK) Protocol(org.jgroups.stack.Protocol) ForkChannel(org.jgroups.fork.ForkChannel) ForkProtocolStack(org.jgroups.fork.ForkProtocolStack)

Example 2 with ForkChannel

use of org.jgroups.fork.ForkChannel 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)

Example 3 with ForkChannel

use of org.jgroups.fork.ForkChannel in project JGroups by belaban.

the class ForkChannelTest method testNullForkStack.

/**
 * Tests the case where we don't add any fork-stack specific protocols
 */
public void testNullForkStack() throws Exception {
    fc1 = new ForkChannel(a, "stack", "fc1");
    fc2 = new ForkChannel(a, "stack", "fc2");
    MyReceiver<Integer> r1 = new MyReceiver<>(), r2 = new MyReceiver<>();
    fc1.setReceiver(r1);
    fc2.setReceiver(r2);
    a.connect(CLUSTER);
    fc1.connect("foo");
    fc2.connect("bar");
    for (int i = 1; i <= 5; i++) {
        fc1.send(null, i);
        fc2.send(null, i + 5);
    }
    List<Integer> l1 = r1.list(), l2 = r2.list();
    for (int i = 0; i < 20; i++) {
        if (l1.size() == 5 && l2.size() == 5)
            break;
        Util.sleep(500);
    }
    System.out.println("r1: " + r1.list() + ", r2: " + r2.list());
    assert r1.size() == 5 && r2.size() == 5;
    for (int i = 1; i <= 5; i++) assert r1.list().contains(i) && r2.list().contains(i + 5);
}
Also used : ForkChannel(org.jgroups.fork.ForkChannel) MyReceiver(org.jgroups.util.MyReceiver)

Example 4 with ForkChannel

use of org.jgroups.fork.ForkChannel in project JGroups by belaban.

the class ForkChannelTest method testIncorrectLifecycle.

public void testIncorrectLifecycle() throws Exception {
    fc1 = new ForkChannel(a, "stack", "fc1");
    a.connect(CLUSTER);
    fc1.connect(CLUSTER);
    Util.close(fc1);
    try {
        fc1.connect(CLUSTER);
        assert false : "reconnecting a closed fork channel must throw an exception";
    } catch (Exception ex) {
        assert ex instanceof IllegalStateException;
        System.out.println("got exception as expected: " + ex);
    }
}
Also used : ForkChannel(org.jgroups.fork.ForkChannel)

Example 5 with ForkChannel

use of org.jgroups.fork.ForkChannel in project JGroups by belaban.

the class ForkChannelTest method testRefcount2.

public void testRefcount2() throws Exception {
    Prot p1 = new Prot("P1"), p2 = new Prot("P2");
    fc1 = new ForkChannel(a, "stack", "fc1", p1, p2);
    // uses p1 and p2 from fc1
    fc2 = new ForkChannel(a, "stack", "fc2");
    // uses p1 and p2 from fc1
    fc3 = new ForkChannel(a, "stack", "fc3");
    assert p1.inits == 1 && p2.inits == 1;
    FORK fork = a.getProtocolStack().findProtocol(FORK.class);
    Protocol prot = fork.get("stack");
    ForkProtocolStack fork_stack = (ForkProtocolStack) getProtStack(prot);
    int inits = fork_stack.getInits();
    assert inits == 3;
    a.connect(CLUSTER);
    fc1.connect(CLUSTER);
    int connects = fork_stack.getConnects();
    assert connects == 1;
    assert p1.starts == 1 && p2.starts == 1;
    fc2.connect(CLUSTER);
    fc3.connect(CLUSTER);
    connects = fork_stack.getConnects();
    assert connects == 3;
    assert p1.starts == 1 && p2.starts == 1;
    fc3.disconnect();
    fc2.disconnect();
    assert p1.starts == 1 && p2.starts == 1;
    assert p1.stops == 0 && p2.stops == 0;
    fc1.disconnect();
    assert p1.starts == 1 && p2.starts == 1;
    assert p1.stops == 1 && p2.stops == 1;
    Util.close(fc3, fc2);
    assert p1.destroys == 0 && p2.destroys == 0;
    Util.close(fc1);
    assert p1.destroys == 1 && p2.destroys == 1;
}
Also used : FORK(org.jgroups.protocols.FORK) Protocol(org.jgroups.stack.Protocol) ForkChannel(org.jgroups.fork.ForkChannel) ForkProtocolStack(org.jgroups.fork.ForkProtocolStack)

Aggregations

ForkChannel (org.jgroups.fork.ForkChannel)8 FORK (org.jgroups.protocols.FORK)3 ForkProtocolStack (org.jgroups.fork.ForkProtocolStack)2 Protocol (org.jgroups.stack.Protocol)2 JChannel (org.jgroups.JChannel)1 Counter (org.jgroups.blocks.atomic.Counter)1 CounterService (org.jgroups.blocks.atomic.CounterService)1 COUNTER (org.jgroups.protocols.COUNTER)1 STATE (org.jgroups.protocols.pbcast.STATE)1 MyReceiver (org.jgroups.util.MyReceiver)1 Test (org.testng.annotations.Test)1