Esempio n. 1
0
  // @Test
  public void peerShouldTransmitEvent()
      throws InterruptedException, CannotReadMessageException, THPRequestException, HashException,
          CannotUnserializeException, CannotReadTokenException, CannotReadBencodedException {
    lock = new CountDownLatch(2);
    initGuice();
    initEventBus();
    initTorrentStream();
    initClient();

    client.loadMetaInfo(torrentStream);
    client.refreshPeerList();

    System.out.println("Refreshing peer list");
    lock.await(30, TimeUnit.SECONDS);
    assertNotNull(peers);
    assertFalse(peers.isEmpty());

    Peer testedPeer = peers.iterator().next();

    testMessageType(testedPeer, MessageType.CHOKE, new ChokeMessage(new byte[1]));
    assertTrue(testedPeer.isChokingUs());

    testMessageType(testedPeer, MessageType.UNCHOKE, new UnChokeMessage(new byte[1]));
    assertFalse(testedPeer.isChokingUs());

    testMessageType(testedPeer, MessageType.INTERESTED, new InterestedMessage(new byte[1]));
    assertTrue(testedPeer.isInterestedInUs());

    testMessageType(testedPeer, MessageType.NOT_INTERESTED, new NotInterestedMessage(new byte[1]));
    assertFalse(testedPeer.isInterestedInUs());
  }
Esempio n. 2
0
  // @Test
  public void peerShouldDownloadBlock()
      throws InterruptedException, THPRequestException, HashException, CannotUnserializeException,
          CannotReadTokenException, CannotReadBencodedException, CannotReadMessageException {
    lock = new CountDownLatch(2);
    initGuice();
    initEventBus();
    initTorrentStream();
    initClient();

    client.loadMetaInfo(torrentStream);
    client.refreshPeerList();

    System.out.println("Refreshing peer list");
    lock.await(30, TimeUnit.SECONDS);
    assertNotNull(peers);
    assertFalse(peers.isEmpty());

    expectedMessageType = MessageType.PIECE;
    expectedMessageReceived = false;
    atLastOneExpected = true;
    lock = new CountDownLatch(1);

    for (Peer testedPeer : peers) {
      testedPeer.connect();

      // We have to wait for a while until the peer has been connected to and initialized
      Thread.sleep(15000);
      // We test if the peer has some pieces
      BitSet havePieces = testedPeer.getAvailablePieces();

      if (havePieces.cardinality() > 0) {
        assertNotEquals(0, havePieces.cardinality());

        // We request the first available piece
        int pieceIndex = havePieces.nextSetBit(0);
        assertTrue(pieceIndex >= 0);

        // We need to unchoke and signal our interest to the peer first
        testedPeer.unchoke();
        testedPeer.interested();

        if (!testedPeer.isChokingUs()) {
          // We can now request
          testedPeer.requestPieceBlock(pieceIndex, 0);
        }
      }
    }

    // Now that the piece has been requested we need to wait for a block message
    lock.await();
    assertTrue(expectedMessageReceived);
  }