示例#1
0
        public void Clear()
        {
            var bucket = new KBucket <Contact>();

            Assert.AreEqual(0, bucket.Count);

            bucket.Add(new Contact("a"));
            bucket.Add(new Contact("b"));
            bucket.Add(new Contact("c"));
            Assert.AreEqual(3, bucket.Count);

            bucket.Clear();
            Assert.AreEqual(0, bucket.Count);
        }
示例#2
0
        public void BucketTest()
        {
            var bucket = new KBucket(4, new System.Random(), Logger.None);
            var peer1  = new BoundPeer(
                new PrivateKey().PublicKey,
                new DnsEndPoint("0.0.0.0", 1234),
                0);
            var peer2 = new BoundPeer(
                new PrivateKey().PublicKey,
                new DnsEndPoint("0.0.0.0", 1234),
                0);
            var peer3 = new BoundPeer(
                new PrivateKey().PublicKey,
                new DnsEndPoint("0.0.0.0", 1234),
                0);
            var peer4 = new BoundPeer(
                new PrivateKey().PublicKey,
                new DnsEndPoint("0.0.0.0", 1234),
                0);
            var peer5 = new BoundPeer(
                new PrivateKey().PublicKey,
                new DnsEndPoint("0.0.0.0", 1234),
                0);

            Assert.Empty(bucket.Peers);
            Assert.True(bucket.IsEmpty());
            Assert.Null(bucket.AddPeer(peer1));
            Assert.True(bucket.Contains(peer1));
            Assert.False(bucket.Contains(peer2));
            Assert.False(bucket.IsEmpty());
            Assert.False(bucket.IsFull());
            Assert.Null(bucket.AddPeer(peer2));
            Assert.Null(bucket.AddPeer(peer3));
            Assert.Null(bucket.AddPeer(peer4));
            Assert.True(bucket.IsFull());
            Assert.Contains(
                bucket.GetRandomPeer(),
                new[] { peer1, peer2, peer3, peer4 });
            Assert.Equal(peer1, bucket.AddPeer(peer5));
            Assert.True(bucket.Contains(peer1));
            Assert.False(bucket.Contains(peer5));
            bucket.Clear();
            Assert.True(bucket.IsEmpty());
        }
示例#3
0
        public void BucketTest()
        {
            var bucket = new KBucket(4, new System.Random(), Logger.None);
            var peer1  = new BoundPeer(
                new PrivateKey().PublicKey,
                new DnsEndPoint("0.0.0.0", 1234),
                AppProtocolVer);
            var peer2 = new BoundPeer(
                new PrivateKey().PublicKey,
                new DnsEndPoint("0.0.0.0", 1234),
                AppProtocolVer);
            var peer3 = new BoundPeer(
                new PrivateKey().PublicKey,
                new DnsEndPoint("0.0.0.0", 1234),
                AppProtocolVer);
            var peer4 = new BoundPeer(
                new PrivateKey().PublicKey,
                new DnsEndPoint("0.0.0.0", 1234),
                AppProtocolVer);
            var peer5 = new BoundPeer(
                new PrivateKey().PublicKey,
                new DnsEndPoint("0.0.0.0", 1234),
                AppProtocolVer);

            Assert.Empty(bucket.Peers);
            Assert.True(bucket.IsEmpty());
            bucket.AddPeer(peer1);
            Assert.True(bucket.Contains(peer1));
            Assert.False(bucket.Contains(peer2));
            Assert.False(bucket.IsEmpty());
            Assert.False(bucket.IsFull());
            // This sleep statement is used to distinguish updated time of followings.
            Thread.Sleep(100);
            bucket.AddPeer(peer2);
            Thread.Sleep(100);
            Assert.Contains(
                bucket.GetRandomPeer(null),
                new[] { peer1, peer2 }
                );
            Assert.Contains(
                bucket.GetRandomPeer(peer1.Address),
                new[] { peer2 }
                );
            bucket.AddPeer(peer3);
            Thread.Sleep(100);
            bucket.AddPeer(peer4);
            Assert.True(bucket.IsFull());
            Assert.Equal(
                bucket.Peers.ToHashSet(),
                new HashSet <BoundPeer> {
                peer1, peer2, peer3, peer4
            }
                );
            Assert.Contains(
                bucket.GetRandomPeer(null),
                new[] { peer1, peer2, peer3, peer4 }
                );
            Thread.Sleep(100);
            bucket.AddPeer(peer5);
            Assert.Equal(
                bucket.Peers.ToHashSet(),
                new HashSet <BoundPeer> {
                peer1, peer2, peer3, peer4
            }
                );
            Assert.False(bucket.Contains(peer5));
            Assert.Equal(peer4, bucket.Head.Peer);
            Assert.Equal(peer1, bucket.Tail.Peer);
            Thread.Sleep(100);
            bucket.AddPeer(peer1);
            Assert.Equal(peer1, bucket.Head.Peer);
            Assert.Equal(peer2, bucket.Tail.Peer);

            Assert.False(bucket.RemovePeer(peer5));
            Assert.True(bucket.RemovePeer(peer1));
            Assert.DoesNotContain(peer1, bucket.Peers);
            Assert.Equal(3, bucket.Peers.Count());

            bucket.Clear();
            Assert.True(bucket.IsEmpty());
        }
示例#4
0
        public void BucketTest()
        {
            var bucket = new KBucket(4, new System.Random(), Logger.None);
            var peer1  = new BoundPeer(
                new PrivateKey().PublicKey,
                new DnsEndPoint("0.0.0.0", 1234));
            var peer2 = new BoundPeer(
                new PrivateKey().PublicKey,
                new DnsEndPoint("0.0.0.0", 1234));
            var peer3 = new BoundPeer(
                new PrivateKey().PublicKey,
                new DnsEndPoint("0.0.0.0", 1234));
            var peer4 = new BoundPeer(
                new PrivateKey().PublicKey,
                new DnsEndPoint("0.0.0.0", 1234));
            var peer5 = new BoundPeer(
                new PrivateKey().PublicKey,
                new DnsEndPoint("0.0.0.0", 1234));

            // Checks for an empty bucket.
            Assert.True(bucket.IsEmpty);
            Assert.False(bucket.IsFull);
            Assert.Empty(bucket.Peers);
            Assert.Empty(bucket.PeerStates);
            Assert.Null(bucket.GetRandomPeer());
            Assert.Null(bucket.Head);
            Assert.Null(bucket.Tail);

            // Checks for a partially filled bucket.
            bucket.AddPeer(peer1, DateTimeOffset.UtcNow);
            Assert.False(bucket.IsEmpty);
            Assert.False(bucket.IsFull);
            Assert.True(bucket.Contains(peer1));
            Assert.False(bucket.Contains(peer2));
            Assert.NotNull(bucket.GetRandomPeer());
            Assert.Null(bucket.GetRandomPeer(peer1.Address));
            Assert.NotNull(bucket.GetRandomPeer(peer2.Address));
            Assert.Equal(peer1, bucket.Head.Peer);
            Assert.Equal(peer1, bucket.Tail.Peer);

            // Sleep statement is used to distinguish updated times.
            Thread.Sleep(100);
            bucket.AddPeer(peer2, DateTimeOffset.UtcNow);
            Assert.Contains(
                bucket.GetRandomPeer(),
                new[] { peer1, peer2 }
                );
            Assert.Contains(
                bucket.GetRandomPeer(peer1.Address),
                new[] { peer2 }
                );

            // Checks for a full bucket.
            Thread.Sleep(100);
            bucket.AddPeer(peer3, DateTimeOffset.UtcNow);
            Thread.Sleep(100);
            bucket.AddPeer(peer4, DateTimeOffset.UtcNow);
            Assert.True(bucket.IsFull);
            Assert.Equal(
                bucket.Peers.ToHashSet(),
                new HashSet <BoundPeer> {
                peer1, peer2, peer3, peer4
            }
                );
            Assert.Contains(
                bucket.GetRandomPeer(),
                new[] { peer1, peer2, peer3, peer4 }
                );
            Thread.Sleep(100);
            bucket.AddPeer(peer5, DateTimeOffset.UtcNow);
            Assert.Equal(
                bucket.Peers.ToHashSet(),
                new HashSet <BoundPeer> {
                peer1, peer2, peer3, peer4
            }
                );
            Assert.False(bucket.Contains(peer5));
            Assert.Equal(peer4, bucket.Head.Peer);
            Assert.Equal(peer1, bucket.Tail.Peer);

            // Check order has changed.
            Thread.Sleep(100);
            bucket.AddPeer(peer1, DateTimeOffset.UtcNow);
            Assert.Equal(peer1, bucket.Head.Peer);
            Assert.Equal(peer2, bucket.Tail.Peer);

            Assert.False(bucket.RemovePeer(peer5));
            Assert.True(bucket.RemovePeer(peer1));
            Assert.DoesNotContain(peer1, bucket.Peers);
            Assert.Equal(3, bucket.Peers.Count());

            // Clear the bucket.
            bucket.Clear();
            Assert.True(bucket.IsEmpty);
            Assert.Empty(bucket.Peers);
            Assert.Null(bucket.Head);
            Assert.Null(bucket.Tail);
            Assert.Null(bucket.GetRandomPeer());
        }