Пример #1
0
        public void Advertise7()
        {
            PeerId other = rig.CreatePeer(true);

            // Check that peers which don't share only get a small number of pieces to share
            rig.Manager.Settings.UploadSlots = 1;
            unchoker.PeerDisconnected(peer);
            List <PeerId> peers = new List <PeerId>(new PeerId[] { peer, rig.CreatePeer(true) });

            peers.ForEach(unchoker.PeerConnected);
            unchoker.UnchokeReview();

            peers.ForEach(delegate(PeerId id) { id.IsInterested = true; });
            unchoker.UnchokeReview();

            Assert.Equal(unchoker.MaxAdvertised + 1, peers[0].QueueLength);
            while (peers[0].QueueLength > 1)
            {
                unchoker.ReceivedHave(peers[0], ((HaveMessage)peers[0].Dequeue()).PieceIndex);
            }
            unchoker.UnchokeReview();
            Assert.IsType(typeof(UnchokeMessage), peers[0].Dequeue());
            Assert.IsType(typeof(ChokeMessage), peers[0].Dequeue());

            Assert.Equal(unchoker.MaxAdvertised + 1, peers[1].QueueLength);
            while (peers[1].QueueLength > 1)
            {
                unchoker.ReceivedHave(other, ((HaveMessage)peers[1].Dequeue()).PieceIndex);
            }
            unchoker.UnchokeReview();
            Assert.IsType(typeof(UnchokeMessage), peers[1].Dequeue());
            Assert.IsType(typeof(ChokeMessage), peers[1].Dequeue());

            // He didn't share any, he should get 1 piece.
            Assert.Equal(1 + 1, peers[0].QueueLength);
            while (peers[0].QueueLength > 1)
            {
                unchoker.ReceivedHave(peers[0], ((HaveMessage)peers[0].Dequeue()).PieceIndex);
            }
            unchoker.UnchokeReview();
            Assert.IsType(typeof(UnchokeMessage), peers[0].Dequeue());
            Assert.IsType(typeof(ChokeMessage), peers[0].Dequeue());

            // He shared them all, he should get max allowance
            Assert.Equal(unchoker.MaxAdvertised + 1, peers[1].QueueLength);
            while (peers[1].QueueLength > 1)
            {
                unchoker.ReceivedHave(other, ((HaveMessage)peers[1].Dequeue()).PieceIndex);
            }
            unchoker.UnchokeReview();
            Assert.IsType(typeof(UnchokeMessage), peers[1].Dequeue());
            Assert.IsType(typeof(ChokeMessage), peers[1].Dequeue());
        }
 public override void HandlePeerDisconnected(PeerId id)
 {
     unchoker.PeerDisconnected(id);
     base.HandlePeerDisconnected(id);
 }