public void testRefcount() throws Exception {
    FORK fork = (FORK) ch.getProtocolStack().findProtocol(FORK.class);
    Protocol prot = fork.get("stack");
    assert prot == null;
    fc1 = new ForkChannel(ch, "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)";

    fc2 = new ForkChannel(ch, "stack", "fc2"); // uses the same fork stack "stack"
    inits = fork_stack.getInits();
    assert inits == 2 : "inits is " + inits + "(expected 2)";

    ch.connect(CLUSTER);

    fc1.connect(CLUSTER);
    int connects = fork_stack.getConnects();
    assert connects == 1 : "connects is " + connects + "(expected 1)";

    fc1.connect(CLUSTER); // duplicate connect()
    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();
    fc2.disconnect(); // duplicate 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)";

    Util.close(fc2); // duplicate close()
    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;
  }
  public void testRefcount2() throws Exception {
    Prot p1 = new Prot("P1"), p2 = new Prot("P2");
    fc1 = new ForkChannel(ch, "stack", "fc1", p1, p2);
    fc2 = new ForkChannel(ch, "stack", "fc2"); // uses p1 and p2 from fc1
    fc3 = new ForkChannel(ch, "stack", "fc3"); // uses p1 and p2 from fc1

    assert p1.inits == 1 && p2.inits == 1;

    FORK fork = (FORK) ch.getProtocolStack().findProtocol(FORK.class);
    Protocol prot = fork.get("stack");
    ForkProtocolStack fork_stack = (ForkProtocolStack) getProtStack(prot);
    int inits = fork_stack.getInits();
    assert inits == 3;

    ch.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;
  }