// make an incoming to the servent, wait a little, and then make sure
  // it asks for a connect back again
  public void testUDPExpireRequestsSent() throws Exception {
    drainAll();
    UDPService udpServ = udpService;
    for (int i = 0; i < 2; i++) {
      // wait until the expire time is realized

      // drain the UDP buffer
      Message m = null;
      try {
        while (true) {
          m = testUP[0].receive(200);
        }
      } catch (InterruptedIOException drained) {
      }
      // get the UDP message
      do {
        m = testUP[0].receive();
      } while (!(m instanceof UDPConnectBackVendorMessage));
      Thread.sleep(1000);
      assertFalse(udpServ.canReceiveUnsolicited());
      // get the UDP message but this time answer it
      do {
        m = testUP[0].receive();
      } while (!(m instanceof UDPConnectBackVendorMessage));
      cbGuid = ((UDPConnectBackVendorMessage) m).getConnectBackGUID().bytes();

      // now connect back and it should switch on unsolicited
      DatagramSocket s = new DatagramSocket();
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      PingRequest ping = pingRequestFactory.createPingRequest(cbGuid, (byte) 1, (byte) 1);
      ping.write(baos);
      DatagramPacket pack =
          new DatagramPacket(
              baos.toByteArray(), baos.toByteArray().length, InetAddress.getLocalHost(), PORT);
      s.send(pack);
      s.close();

      Thread.sleep(1000);
      assertTrue(udpServ.canReceiveUnsolicited());
    }
  }
  // This test checks that if _acceptedUnsolicitedIncoming is false, connect
  // back messages are NOT sent
  public void testUDPExpireRequestsNotSent() throws Exception {
    drainAll();
    UDPService udpServ = udpService;
    for (int i = 0; i < 2; i++) {
      assertFalse(udpServ.canReceiveUnsolicited());

      try {
        testUP[0].receive(TIMEOUT);
      } catch (InterruptedIOException expected) {
      }

      Thread.sleep(MY_EXPIRE_TIME + MY_VALIDATE_TIME);
      assertFalse(udpServ.canReceiveUnsolicited());
      Thread.sleep(100);
      // now we should get the connect backs cuz it has been a while
      Message m = null;
      do {
        m = testUP[0].receive(TIMEOUT);
      } while (!(m instanceof UDPConnectBackVendorMessage));
      cbGuid = m.getGUID();
    }
  }
  // make an incoming to the servent, wait a little, and then make sure
  // it asks for a connect back again
  public void testUDPInterleavingRequestsSent() throws Exception {
    drainAll();
    UDPService udpServ = udpService;
    Random rand = new Random();
    for (int i = 0; i < 6; i++) {
      if (rand.nextBoolean()) {
        DatagramSocket s = new DatagramSocket();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ReplyNumberVendorMessage vm = replyNumberVendorMessageFactory.create(new GUID(cbGuid), 1);
        vm.write(baos);
        DatagramPacket pack =
            new DatagramPacket(
                baos.toByteArray(), baos.toByteArray().length, InetAddress.getLocalHost(), PORT);
        s.send(pack);
        s.close();
        Thread.sleep(100);
      }

      // wait until the expire time is realized
      Thread.sleep(MY_EXPIRE_TIME + MY_VALIDATE_TIME);

      // throw some randomness in there - if we get an incoming we should
      // not send messages out
      if (udpServ.canReceiveUnsolicited()) {
        DatagramSocket s = new DatagramSocket();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ReplyNumberVendorMessage vm = replyNumberVendorMessageFactory.create(new GUID(cbGuid), 1);
        vm.write(baos);
        DatagramPacket pack =
            new DatagramPacket(
                baos.toByteArray(), baos.toByteArray().length, InetAddress.getLocalHost(), PORT);
        s.send(pack);
        s.close();
        Thread.sleep(100);
        assertTrue(udpServ.canReceiveUnsolicited());
        try {
          testUP[0].receive(TIMEOUT);
        } catch (InterruptedIOException expected) {
        }
      } else {
        Thread.sleep(MY_VALIDATE_TIME);
        // query the Acceptor - it should send off more requests
        Thread.sleep(100);
        Message m = null;
        do {
          m = testUP[0].receive(TIMEOUT);
        } while (!(m instanceof UDPConnectBackVendorMessage));
        cbGuid = ((UDPConnectBackVendorMessage) m).getConnectBackGUID().bytes();

        // now connect back and it should switch on unsolicited
        DatagramSocket s = new DatagramSocket();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PingRequest ping = pingRequestFactory.createPingRequest(cbGuid, (byte) 1, (byte) 1);
        ping.write(baos);
        DatagramPacket pack =
            new DatagramPacket(
                baos.toByteArray(), baos.toByteArray().length, InetAddress.getLocalHost(), PORT);
        s.send(pack);
        s.close();
      }
    }
  }