示例#1
0
        public void CanUseAddrManager()
        {
            AddressManager addrman = new AddressManager();

            addrman.DebugMode = true;
            var localhost = new NetworkAddress(IPAddress.Parse("127.0.0.1"), 8333);

            addrman.Add(localhost, IPAddress.Loopback);
            Assert.NotNull(addrman.nKey);
            Assert.True(addrman.nKey != new uint256(0));
            Assert.True(addrman.nNew == 1);
            addrman.Good(localhost);
            Assert.True(addrman.nNew == 0);
            Assert.True(addrman.nTried == 1);
            addrman.Attempt(localhost);

            var addr = addrman.Select();

            Assert.False(addr.Ago < TimeSpan.FromSeconds(10.0));

            addrman.Connected(localhost);

            addr = addrman.Select();
            Assert.True(addr.Ago < TimeSpan.FromSeconds(1.0));
        }
示例#2
0
        public void CanStressAddrManager()
        {
            Exception exception   = null;
            var       addrmanager = new AddressManager();
            Random    randl       = new Random();

            for (int i = 0; i < 30; i++)
            {
                NetworkAddress address       = RandomNetworkAddress(randl);
                IPAddress      addressSource = RandomAddress(randl);
                address.Ago = TimeSpan.FromMinutes(5.0);
                addrmanager.Add(address, addressSource);
            }

            addrmanager.DebugMode = true;
            var threads =
                Enumerable
                .Range(0, 20)
                .Select(t => new Thread(() =>
            {
                try
                {
                    Random rand = new Random(t);
                    for (int i = 0; i < 50; i++)
                    {
                        NetworkAddress address  = RandomNetworkAddress(rand);
                        IPAddress addressSource = RandomAddress(rand);
                        var operation           = rand.Next(0, 7);
                        switch (operation)
                        {
                        case 0:
                            addrmanager.Attempt(address);
                            break;

                        case 1:
                            addrmanager.Add(address, addressSource);
                            break;

                        case 2:
                            addrmanager.Select();
                            break;

                        case 3:
                            addrmanager.GetAddr();
                            break;

                        case 4:
                            {
                                var several = addrmanager.GetAddr();
                                addrmanager.Good(several.Length == 0 ? address : several[0]);
                            }
                            break;

                        case 5:
                            addrmanager.Connected(address);
                            break;

                        case 6:
                            addrmanager.ToBytes();
                            break;

                        default:
                            throw new NotSupportedException();
                        }
                    }
                }
                catch (Exception ex)
                {
                    exception = ex;
                    throw;
                }
            })).ToArray();

            foreach (var t in threads)
            {
                t.Start();
            }
            foreach (var t in threads)
            {
                t.Join();
            }

            Assert.True(addrmanager.nNew != 0);
            Assert.True(addrmanager.nTried != 0);
            Assert.True(addrmanager.GetAddr().Length != 0);
            Assert.Null(exception);
        }