Exemplo n.º 1
0
        public void CancelAllPendingWhenPieceReceived()
        {
            id.BitField[0] = other.BitField[0] = true;
            picker.Initialise(bitfield, torrentData, new List <Piece> ());

            var otherRequest = picker.PickPiece(other, other.BitField, new List <PeerId> ());

            PieceRequest message;
            Piece        piece;

            while ((message = picker.PickPiece(id, id.BitField, new List <PeerId> ())) != null)
            {
                Assert.IsTrue(picker.ValidatePiece(id, message.PieceIndex, message.StartOffset, message.RequestLength, out piece));
                if (piece.AllBlocksReceived)
                {
                    break;
                }
            }

            Assert.AreEqual(0, id.AmRequestingPiecesCount, "#requesting");
            Assert.IsFalse(picker.ValidatePiece(other, otherRequest.PieceIndex, otherRequest.StartOffset, otherRequest.RequestLength, out piece), "#1");
            Assert.IsNull(piece, "#2");

            message = picker.PickPiece(other, other.BitField, new List <PeerId> ());
            Assert.AreEqual(0, message.PieceIndex, "#3");
        }
Exemplo n.º 2
0
        public void MultiPick()
        {
            id.BitField.Set(pieces[0].Index, true);
            other.BitField.Set(pieces[0].Index, true);

            for (int i = 2; i < pieces[0].BlockCount; i++)
            {
                pieces[0].Blocks[i].CreateRequest(PeerId.CreateNull(torrentData.PieceCount));
                pieces[0].Blocks[i].Received = true;
            }

            picker.Initialise(bitfield, torrentData, pieces);

            // Pick blocks 1 and 2 for both peers
            while (picker.PickPiece(id, id.BitField, new List <PeerId>()) != null)
            {
            }
            while (picker.PickPiece(other, id.BitField, new List <PeerId>()) != null)
            {
            }

            Assert.AreEqual(2, id.AmRequestingPiecesCount, "#1");
            Assert.AreEqual(2, other.AmRequestingPiecesCount, "#1");

            Piece piece;

            if (!picker.ValidatePiece(id, pieces[0].Index, pieces[0][0].StartOffset, pieces[0][0].RequestLength, out piece))
            {
                Assert.Fail("I should've validated!");
            }

            if (picker.ValidatePiece(other, pieces[0].Index, pieces[0][0].StartOffset, pieces[0][0].RequestLength, out piece))
            {
                Assert.Fail("I should not have validated!");
            }

            Assert.AreEqual(1, id.AmRequestingPiecesCount, "#1");
            Assert.AreEqual(1, other.AmRequestingPiecesCount, "#1");
            Assert.IsTrue(pieces[0][0].Received, "#5");
            Assert.AreEqual(16, pieces[0].TotalRequested, "#6");
            Assert.AreEqual(15, pieces[0].TotalReceived, "#7");
        }