public void testDownloadProgressQueryDoneNoPurge() throws Exception {

    BlockingConnectionUtils.keepAllAlive(testUP, pingReplyFactory);
    // clear up any messages before we begin the test.
    drainAll();

    // luckily there is hacky little way to go through the download paces -
    // download from yourself :) .

    Message m = null;

    byte[] guid = searchServices.newQueryGUID();
    searchServices.query(guid, "metadata");
    callback.setGUID(new GUID(guid));

    QueryRequest qr =
        BlockingConnectionUtils.getFirstInstanceOfMessageType(testUP[0], QueryRequest.class);
    assertNotNull(qr);
    assertTrue(qr.desiresOutOfBandReplies());

    // just return ONE real result and the rest junk
    Response resp = null;
    QueryReply reply = null;
    {
      // get a correct response object
      QueryRequest qrTemp = queryRequestFactory.createQuery("metadata");
      testUP[0].send(qrTemp);
      testUP[0].flush();

      reply = BlockingConnectionUtils.getFirstInstanceOfMessageType(testUP[0], QueryReply.class);
      assertNotNull(reply);
      resp = (reply.getResultsAsList()).get(0);
    }
    assertNotNull(reply);
    assertNotNull(resp);
    Response[] res = new Response[] {resp};

    // this isn't really needed but just for completeness send it back to
    // the test Leaf
    m =
        queryReplyFactory.createQueryReply(
            guid,
            (byte) 1,
            SERVER_PORT,
            myIP(),
            0,
            res,
            GUID.makeGuid(),
            new byte[0],
            false,
            false,
            true,
            true,
            false,
            false,
            null);
    testUP[0].send(m);
    testUP[0].flush();

    // send back a lot of results via TCP so you konw the UDP one will be
    // bypassed
    for (int i = 0; i < testUP.length; i++) {
      res = new Response[75];
      for (int j = 0; j < res.length; j++)
        res[j] =
            responseFactory.createResponse(
                10 + j + i, 10 + j + i, "metadata " + j + i, UrnHelper.SHA1);
      m =
          queryReplyFactory.createQueryReply(
              guid,
              (byte) 1,
              testUP[0].getPort(),
              myIP(),
              0,
              res,
              GUID.makeGuid(),
              new byte[0],
              false,
              false,
              true,
              true,
              false,
              false,
              null);
      testUP[i].send(m);
      testUP[i].flush();
    }

    // allow for processing
    Thread.sleep(3000);

    {
      // now we should make sure MessageRouter has not bypassed anything
      // yet
      assertByPassedResultsCacheHasSize(qr.getGUID(), 0);
    }

    // send back a UDP response and make sure it was saved in bypassed...
    {
      ReplyNumberVendorMessage vm =
          replyNumberVendorMessageFactory.createV3ReplyNumberVendorMessage(new GUID(guid), 1);
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      vm.write(baos);
      DatagramPacket pack =
          new DatagramPacket(
              baos.toByteArray(),
              baos.toByteArray().length,
              testUP[0].getInetAddress(),
              SERVER_PORT);
      UDP_ACCESS[0].send(pack);
    }

    // allow for processing
    Thread.sleep(500);

    {
      // all the UDP ReplyNumberVMs should have been bypassed
      assertByPassedResultsCacheHasSize(guid, 1);
    }

    // now do the download, wait for it to finish, and then bypassed results
    // should be empty again
    RemoteFileDesc rfd =
        resp.toRemoteFileDesc(reply, null, remoteFileDescFactory, pushEndpointFactory);

    assertFalse("file should not be saved yet", new File(_savedDir, "metadata.mp3").exists());

    downloadServices.download(new RemoteFileDesc[] {rfd}, false, new GUID(guid));
    UploadSettings.UPLOAD_SPEED.setValue(5);

    searchServices.stopQuery(new GUID(guid));
    callback.clearGUID();

    // download still in progress, don't purge
    assertByPassedResultsCacheHasSize(guid, 1);

    UploadSettings.UPLOAD_SPEED.setValue(100);

    // sleep to make sure the download starts
    Thread.sleep(20000);

    assertTrue("file should saved", new File(_savedDir, "metadata.mp3").exists());

    // now we should make sure MessageRouter clears the cache
    assertByPassedResultsCacheHasSize(qr.getGUID(), 0);
  }
  public void testNoDownloadQueryDonePurge() throws Exception {

    // set smaller clear times so we can test in a timely fashion

    BlockingConnectionUtils.keepAllAlive(testUP, pingReplyFactory);
    // clear up any messages before we begin the test.
    drainAll();

    Message m = null;

    byte[] guid = searchServices.newQueryGUID();
    searchServices.query(guid, "whatever");
    // i need to pretend that the UI is showing the user the query still
    callback.setGUID(new GUID(guid));

    QueryRequest qr =
        BlockingConnectionUtils.getFirstInstanceOfMessageType(testUP[0], QueryRequest.class);
    assertNotNull(qr);
    assertTrue(qr.desiresOutOfBandReplies());

    // ok, the leaf is sending OOB queries - good stuff, now we should send
    // a lot of results back and make sure it buffers the bypassed OOB ones
    for (int i = 0; i < testUP.length; i++) {
      Response[] res = new Response[200];
      for (int j = 0; j < res.length; j++)
        res[j] =
            responseFactory.createResponse(
                10 + j + i, 10 + j + i, "whatever " + j + i, UrnHelper.SHA1);
      m =
          queryReplyFactory.createQueryReply(
              qr.getGUID(),
              (byte) 1,
              6355,
              myIP(),
              0,
              res,
              GUID.makeGuid(),
              new byte[0],
              false,
              false,
              true,
              true,
              false,
              false,
              null);
      testUP[i].send(m);
      testUP[i].flush();
    }

    // wait for processing
    Thread.sleep(2000);

    for (int i = 0; i < UDP_ACCESS.length; i++) {
      ReplyNumberVendorMessage vm =
          replyNumberVendorMessageFactory.createV3ReplyNumberVendorMessage(
              new GUID(qr.getGUID()), i + 1);
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      vm.write(baos);
      DatagramPacket pack =
          new DatagramPacket(
              baos.toByteArray(),
              baos.toByteArray().length,
              testUP[0].getInetAddress(),
              SERVER_PORT);
      UDP_ACCESS[i].send(pack);
    }

    // wait for processing
    Thread.sleep(1000);

    {
      // all the UDP ReplyNumberVMs should have been bypassed
      assertByPassedResultsCacheHasSize(qr.getGUID(), UDP_ACCESS.length);
    }

    {
      // now we should make sure MessageRouter clears the map
      searchServices.stopQuery(new GUID(qr.getGUID()));
      assertByPassedResultsCacheHasSize(qr.getGUID(), 0);
    }
    callback.clearGUID();
  }