Example #1
0
        public void TestAllUniquePiecesChosenByNextPiece()
        {
            MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent);

            file.Parse();
            Manager          manager   = new Manager();
            TorrentContext   tc        = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory);
            HashSet <UInt32> pieces    = new HashSet <UInt32>();
            Selector         selector  = new Selector();
            UInt32           nextPiece = 0;

            for (UInt32 pieceNumber = 0; pieceNumber < tc.numberOfPieces; pieceNumber++)
            {
                tc.IncrementPeerCount(pieceNumber);
            }
            while (selector.NextPiece(tc, ref nextPiece))
            {
                if ((nextPiece >= 0) && (nextPiece < tc.numberOfPieces))
                {
                    pieces.Add(nextPiece);
                    tc.MarkPieceMissing(nextPiece, false);
                }
            }
            Assert.False(selector.NextPiece(tc, ref nextPiece));
            Assert.Equal(tc.numberOfPieces, pieces.Count);
        }
Example #2
0
        public void TestInvalidPieceNumberPassedToIncrementPieceCount()
        {
            MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent);

            file.Parse();
            TorrentContext tc = new TorrentContext(file, new Selector(), new DiskIO(new Manager()), Constants.DestinationDirectory);

            Assert.Throws <IndexOutOfRangeException>(() => tc.IncrementPeerCount(Constants.InvalidPieceNumber));
        }
Example #3
0
        public void TestNoMissingPiecesOnCallToNextPiece()
        {
            MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent);

            file.Parse();
            Manager        manager   = new Manager();
            TorrentContext tc        = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory);
            UInt32         nextPiece = 0;
            Selector       selector  = new Selector();

            for (UInt32 pieceNumber = 0; pieceNumber < tc.numberOfPieces; pieceNumber++)
            {
                tc.IncrementPeerCount(pieceNumber);
                tc.MarkPieceMissing(pieceNumber, false);
            }
            Assert.False(selector.NextPiece(tc, ref nextPiece));
        }
Example #4
0
        public void TestAtLeastOnePeersHasPieceOnCallToNextPiece()
        {
            MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent);

            file.Parse();
            Manager        manager   = new Manager();
            TorrentContext tc        = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory);
            UInt32         nextPiece = 0;
            Selector       selector  = new Selector();

            for (UInt32 pieceNumber = 0; pieceNumber < tc.numberOfPieces; pieceNumber++)
            {
                tc.IncrementPeerCount(pieceNumber);
            }
            Assert.True(selector.NextPiece(tc, ref nextPiece));
            Assert.True((nextPiece >= 0 && (nextPiece < tc.numberOfPieces)));
        }
Example #5
0
        public void TestAskForZeroPiecesFromLocalPeerSuggestions()
        {
            MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent);

            file.Parse();
            Manager        manager  = new Manager();
            TorrentContext tc       = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory);
            Selector       selector = new Selector();
            Peer           peer     = new Peer("127.0.0.1", 6881, tc, new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0));

            for (UInt32 pieceNumber = 0; pieceNumber < tc.numberOfPieces; pieceNumber++)
            {
                tc.IncrementPeerCount(pieceNumber);
                tc.MarkPieceLocal(pieceNumber, true);
            }
            UInt32[] pieces = selector.LocalPieceSuggestions(peer, 0);
            Assert.Empty(pieces);
        }
Example #6
0
        public void TestReturnCorrectLastMissingPieceOnCallToNextPiece(UInt32 expected)
        {
            MetaInfoFile file = new MetaInfoFile(Constants.SingleFileTorrent);

            file.Parse();
            Manager        manager  = new Manager();
            TorrentContext tc       = new TorrentContext(file, new Selector(), new DiskIO(manager), Constants.DestinationDirectory);
            UInt32         actual   = 0;
            Selector       selector = new Selector();

            for (UInt32 pieceNumber = 0; pieceNumber < tc.numberOfPieces; pieceNumber++)
            {
                tc.IncrementPeerCount(pieceNumber);
                tc.MarkPieceMissing(pieceNumber, false);
            }
            tc.MarkPieceMissing(expected, true);
            Assert.True(selector.NextPiece(tc, ref actual));
            Assert.Equal(expected, actual);
        }