/** Tests case #3 of UNICAST.new.txt */
  public void testBothChannelsClosing() throws Exception {
    sendToEachOtherAndCheck(10);

    // now close the connections to each other
    System.out.println("==== Closing the connections on both sides");
    removeConnection(u1, b_addr);
    removeConnection(u2, a_addr);
    r1.clear();
    r2.clear();

    // causes new connection establishment
    sendToEachOtherAndCheck(10);
  }
  private void sendAndCheckMessages(int num_msgs, JChannel... channels) throws Exception {
    ra.clear();
    rb.clear();
    rc.clear();
    // 1. send unicast messages
    Set<Address> mbrs = new HashSet<Address>(channels.length);
    for (JChannel ch : channels) mbrs.add(ch.getAddress());

    for (JChannel ch : channels) {
      Address addr = ch.getAddress();
      for (Address dest : mbrs) {
        for (int i = 1; i <= num_msgs; i++) {
          ch.send(dest, null, "unicast msg #" + i + " from " + addr);
        }
      }
    }
    int total_msgs = num_msgs * channels.length;
    MyReceiver[] receivers = new MyReceiver[channels.length];
    for (int i = 0; i < channels.length; i++) receivers[i] = (MyReceiver) channels[i].getReceiver();
    checkReceivedMessages(total_msgs, receivers);
  }
Exemplo n.º 3
0
 private static void checkMsgDelivery(MyReceiver... rl) {
   for (MyReceiver r : rl) {
     List<Integer> list = r.getList();
     for (int i = 0; i < 20; i++) {
       if (!list.isEmpty()) break;
       Util.sleep(500);
     }
     System.out.println("list = " + list);
     assert list.size() == 1 && list.get(0) == 0;
     r.clear();
   }
 }
 private static void sendAndCheck(JChannel channel, Address dest, int num, MyReceiver receiver)
     throws Exception {
   receiver.clear();
   for (int i = 1; i <= num; i++) channel.send(dest, "m" + i);
   List<Message> list = receiver.getMessages();
   for (int i = 0; i < 20; i++) {
     if (list.size() == num) break;
     Util.sleep(500);
   }
   System.out.println("list = " + print(list));
   int size = list.size();
   assert size == num : "list has " + size + " elements: " + list;
 }
  /**
   * Tests concurrent reception of multiple messages with a different conn_id
   * (https://issues.jboss.org/browse/JGRP-1347)
   */
  public void testMultipleConcurrentResets() throws Exception {
    sendAndCheck(a, b_addr, 1, r2);

    // now close connection on A unilaterally
    System.out.println("==== Closing the connection on A");
    removeConnection(u1, b_addr);

    r2.clear();

    final UNICAST unicast = (UNICAST) b.getProtocolStack().findProtocol(UNICAST.class);

    int NUM = 10;

    final List<Message> msgs = new ArrayList<Message>(NUM);

    for (int i = 1; i <= NUM; i++) {
      Message msg = new Message(b_addr, a_addr, "m" + i);
      UNICAST.UnicastHeader hdr = UNICAST.UnicastHeader.createDataHeader(1, (short) 2, true);
      msg.putHeader(unicast.getId(), hdr);
      msgs.add(msg);
    }

    Thread[] threads = new Thread[NUM];
    final CyclicBarrier barrier = new CyclicBarrier(NUM + 1);
    for (int i = 0; i < NUM; i++) {
      final int index = i;
      threads[i] =
          new Thread() {
            public void run() {
              try {
                barrier.await();
                unicast.up(new Event(Event.MSG, msgs.get(index)));
              } catch (Exception e) {
                e.printStackTrace();
              }
            }
          };
      threads[i].start();
    }

    barrier.await();
    for (Thread thread : threads) thread.join();

    List<Message> list = r2.getMessages();
    System.out.println("list = " + print(list));

    assert list.size() == 1
        : "list must have 1 element but has " + list.size() + ": " + print(list);
  }