@Test
  public void sendMessageCompletable() throws Exception {
    BlockingQueue<MmsMessage> q = new LinkedBlockingQueue<>();
    Session s = connectNormally(msg -> q.add(msg));

    CompletableFuture<Void> cf = new CompletableFuture<>();
    s.sendMessage(new Broadcast().setSenderId("abc"), cf);
    t.t(); // take and ignore

    t.send(new Broadcast().setSenderId("abc"), 1, 1);

    cf.get(2, TimeUnit.SECONDS);

    for (int i = 2; i < 100; i++) {
      cf = new CompletableFuture<>();
      s.sendMessage(new Broadcast().setSenderId("abc" + i), cf);

      MmsMessage mm = t.t();
      assertEquals(i, mm.getMessageId());
      t.send(new Broadcast().setSenderId("abc"), i, mm.getMessageId());

      cf.get(2, TimeUnit.SECONDS);
    }
    // if this breaks it is a timeing issue, just keep trying until its 99
    assertEquals(99, s.latestReceivedId);

    s.closeSession(MmsConnectionClosingCode.NORMAL);
  }
  @Test
  public void receiveMessagesAckCheck() throws Exception {
    BlockingQueue<MmsMessage> q = new LinkedBlockingQueue<>();

    Session s = connectNormally(msg -> q.add(msg));

    t.send(new Broadcast().setSenderId("abc"), 1, 0);

    MmsMessage mm = q.poll(2, TimeUnit.SECONDS);
    assertEquals(1, mm.getMessageId());
    assertEquals(0, mm.getLatestReceivedId());
    assertEquals("abc", mm.cast(Broadcast.class).getSenderId());

    // We need to acquire this lock to make sure we are out of the receiving loop (last ack adjusted
    s.receiveLock.lock();
    s.receiveLock.unlock();

    CompletableFuture<Void> cf = new CompletableFuture<>();
    s.sendMessage(new Broadcast().setSenderId("cba"), cf);

    mm = t.t();
    assertEquals(1, mm.getMessageId());
    assertEquals(1, mm.getLatestReceivedId());
    assertEquals("cba", mm.cast(Broadcast.class).getSenderId());

    s.closeSession(MmsConnectionClosingCode.NORMAL);
  }
 private void register() {
   if (session != null && session.isConnected()) {
     onlineUsers.clear();
     // get the current list
     Message msg = session.sendMessage("", "list", "");
     System.out.println("LIST: " + msg.getBody());
     StringTokenizer st = new StringTokenizer(msg.getBody(), "\n");
     while (st.hasMoreTokens()) {
       onlineUsers.addElement(new HostItem(st.nextToken()));
     }
     session.postMessage("", "register", "");
   }
 }
  @Test
  public void sendMessages() throws Exception {
    Session s = connectNormally(msg -> {});

    // First msg
    s.sendMessage(new Broadcast().setSenderId("abc"), new CompletableFuture<>());

    MmsMessage mm = t.t();
    assertEquals(1, mm.getMessageId());
    assertEquals(0, mm.getLatestReceivedId());
    assertEquals("abc", mm.cast(Broadcast.class).getSenderId());

    // next messages
    for (int i = 1; i < 100; i++) {
      s.sendMessage(new Broadcast().setSenderId("abcd" + i), new CompletableFuture<>());

      mm = t.t();
      assertEquals(i + 1, mm.getMessageId());
      assertEquals(0, mm.getLatestReceivedId());
      assertEquals("abcd" + i, mm.cast(Broadcast.class).getSenderId());
    }
    s.closeSession(MmsConnectionClosingCode.NORMAL);
  }
  public static void main(String[] args) {
    if (args.length > 0) {
      try {
        if (args[0].equals("-stress")) {
          String userName = args[1];
          String server = args[2];
          String targetUser = args[3];
          int rate = Integer.parseInt(args[4]);
          Session session = new Session();
          if (session.connect(server, userName)) {
            long msgCount = 0;
            while (true) {
              for (int i = 0; i < rate; i++) {
                msgCount++;
                session.postMessage(
                    targetUser, ("subject " + String.valueOf(msgCount)), Util.randomString(1000));
              }
              Thread.currentThread().sleep(2000);
            }
          }
        } else if (args[0].equals("-stress2")) {
          String userName = args[1];
          String server = args[2];
          String targetUser = args[3];
          int rate = Integer.parseInt(args[4]);
          Session session = new Session();
          long msgCount = 0;
          while (true) {
            if (session.connect(server, userName)) {
              for (int i = 0; i < rate; i++) {
                msgCount++;
                session.postMessage(
                    targetUser, ("subject " + String.valueOf(msgCount)), Util.randomString(1000));
              }
              session.disconnect();
            }
            Thread.currentThread().sleep(2000);
          }

        } else if (args[0].equals("-stress3")) {
          String userName = args[1];
          String server = args[2];
          String targetUser = args[3];
          int rate = Integer.parseInt(args[4]);
          Session session = new Session();
          long msgCount = 0;
          while (true) {
            if (session.connect(server, userName)) {
              for (int i = 0; i < rate; i++) {
                msgCount++;
                session.postMessage(
                    targetUser, ("subject " + String.valueOf(msgCount)), Util.randomString(1000));
              }
              session.dropConnection();
            }
            Thread.currentThread().sleep(2000);
          }
        } else if (args[0].equals("-stress4")) {
          String userName = args[1];
          String server = args[2];
          String targetUser = args[3];
          int rate = Integer.parseInt(args[4]);
          Session session = new Session();
          long msgCount = 0;
          while (true) {
            if (session.connect(server, userName)) {
              for (int i = 0; i < rate; i++) {
                msgCount++;
                session.postMessage(
                    targetUser, ("subject " + String.valueOf(msgCount)), Util.fixedString(300000));
              }
              session.dropConnection();
            }
            Thread.currentThread().sleep(5000);
          }

        } else if (args[0].equals("-stress5")) {
          String userName = args[1];
          String server = args[2];
          String targetUser = args[3];
          int rate = Integer.parseInt(args[4]);
          Session session = new Session();
          if (session.connect(server, userName)) {
            long msgCount = 0;
            while (true) {
              for (int i = 0; i < rate; i++) {
                msgCount++;
                session.postMessage(
                    targetUser, ("subject " + String.valueOf(msgCount)), Util.randomString(10));
              }
              Thread.currentThread().sleep(2000);
            }
          }
        } else if (args[0].equals("-monitor")) {
          String userName = args[1];
          String server = args[2];
          long interval = Long.parseLong(args[3]);
          Session session = new Session();
          long msgCount = 0;
          if (session.connect(server, userName)) {
            while (true) {
              Message msg = session.sendMessage("", "get send queue stats", "");
              System.out.println("SEND QUEUE STATS");
              System.out.println(msg.getBody());
              Thread.currentThread().sleep(interval);
            }
          }
        } else if (args[0].equals("-responder")) {
          String userName = args[1];
          String server = args[2];
          long interval = Long.parseLong(args[3]);
          final Session session = new Session();
          if (session.connect(server, userName)) {
            session.addListener(
                new AsyncMessageReceiverListener() {
                  public void receiveMsg(Message msg) {
                    session.postMessage(
                        msg.getFrom(), "RE: " + msg.getFrom(), msg.getThread(), msg.getBody());
                  }

                  public void messageReceiverClosed(MessageReceiver mr) {}
                });
            while (true) {
              Thread.currentThread().sleep(interval);
            }
          }
        } else if (args[0].equals("-threaded-sender")) {
          String userName = args[1];
          String server = args[2];
          String targetUser = args[3];
          int rate = Integer.parseInt(args[4]);
          Session session = new Session();
          long msgCount = 0;
          if (session.connect(server, userName)) {
            while (true) {
              for (int i = 0; i < rate; i++) {
                msgCount++;
                Message reply =
                    session.sendMessage(
                        targetUser, ("subject " + String.valueOf(msgCount)), "body", 10000);
                if (reply == null) {
                  System.out.println("reply not received");
                }
              }
              Thread.currentThread().sleep(5000);
            }
          }
        }
      } catch (Exception e) {
        System.out.println(
            "Unable to start stress test.  Format: -stress <user name> <server> <target user> <msg rate>");
      }
    } else {
      Client client = new Client();
      client.setSize(800, 420);
      center(client);
      client.show();
    }
  }