/**
   * Test to make sure that dynamic querying sends a query with TTL=1 and other properties when a
   * neighboring Ultrapeer has a hit in its QRP table for that query.
   */
  public void testDynamicQueryingWithQRPHit() throws Exception {
    assertTrue("should be connected", connectionServices.isConnected());

    searchServices.query(searchServices.newQueryGUID(), match);
    Thread.sleep(4000);

    QueryRequest qSent =
        BlockingConnectionUtils.getFirstInstanceOfMessageType(ULTRAPEER[0], QueryRequest.class);

    // The TTL on the sent query should be 1 because the other Ultrapeer
    // should have a "hit" in its QRP table.  When there's a hit, we
    // send with TTL 1 simply because it's likely that it's popular.
    if (qSent.getTTL() != 1) {
      // see if qrp got exchanged properly
      int num = connectionManager.getInitializedConnections().size();
      double totalQrp = 0;
      for (RoutedConnection rc : connectionManager.getInitializedClientConnections())
        totalQrp += rc.getRoutedConnectionStatistics().getQueryRouteTablePercentFull();
      fail(
          "ttl was not 1 but "
              + qSent.getTTL()
              + " there were "
              + num
              + " connections with qrp total "
              + totalQrp);
    }
    assertEquals("wrong hops", 0, qSent.getHops());
  }
  public void testQRP() throws Exception {
    RoutedConnection c = connectionManager.getInitializedConnections().get(0);
    c.getRoutedConnectionStatistics().incrementNextQRPForwardTime(0);
    PatchTableMessage ptm =
        BlockingConnectionUtils.getFirstInstanceOfMessageType(
            testUP[0], PatchTableMessage.class, 22000);
    assertNotNull(ptm);
    QueryRouteTable qrt = new QueryRouteTable();
    qrt.patch(ptm);

    // initially, the qrp words should be included
    assertTrue(qrt.contains(queryRequestFactory.createQuery("badger")));

    // change some words, an updated qrp should be sent shortly
    SearchSettings.LIME_QRP_ENTRIES.set(new String[] {"mushroom"});
    c.getRoutedConnectionStatistics().incrementNextQRPForwardTime(0);
    triggerSimppUpdate();
    ptm =
        BlockingConnectionUtils.getFirstInstanceOfMessageType(
            testUP[0], PatchTableMessage.class, 12000);
    assertNotNull(ptm);
    qrt.patch(ptm);

    // the new word should be there, the old one gone.
    assertTrue(qrt.contains(queryRequestFactory.createQuery("mushroom")));
    assertFalse(qrt.contains(queryRequestFactory.createQuery("badger")));
  }
 public void testNonBlockingConnectSucceeds() throws Exception {
   RoutedConnection rc = routedConnectionFactory.createRoutedConnection("127.0.0.1", LISTEN_PORT);
   StubGnetConnectObserver observer = new StubGnetConnectObserver();
   rc.initialize(observer);
   observer.waitForResponse(3000);
   assertFalse(observer.isShutdown());
   assertFalse(observer.isBadHandshake());
   assertFalse(observer.isNoGOK());
   assertTrue(observer.isConnect());
 }
 public void testNonBlockingBadHandshake() throws Exception {
   ACCEPTOR.getObserver().setBadHandshake(true);
   RoutedConnection rc =
       routedConnectionFactory.createRoutedConnection("127.0.0.1", LISTEN_PORT, ConnectType.PLAIN);
   StubGnetConnectObserver observer = new StubGnetConnectObserver();
   rc.initialize(observer);
   observer.waitForResponse(10000);
   assertFalse(observer.isShutdown());
   assertTrue(observer.isBadHandshake());
   assertFalse(observer.isConnect());
   assertFalse(observer.isNoGOK());
 }
 /** Adds all current connections that have the right update ID as a source for this download. */
 private void addCurrentDownloadSources(ManagedDownloader md, DownloadInformation info) {
   for (RoutedConnection mc : connectionManager.get().getConnections()) {
     if (mc.getConnectionCapabilities().getRemoteHostUpdateVersion() == _lastId) {
       LOG.debug("Adding source: " + mc);
       md.addDownload(rfd(mc, info), false);
     } else
       LOG.debug(
           "Not adding source because bad id: "
               + mc.getConnectionCapabilities().getRemoteHostUpdateVersion()
               + ", us: "
               + _lastId);
   }
 }