예제 #1
0
        public async Task InitialiseFailure()
        {
            // Block until we've checked the status moved to Initializing
            var errorSource = new TaskCompletionSource <object> ();

            listener.MessageSent += (o, e) => errorSource.Task.GetAwaiter().GetResult();

            await engine.StartAsync();

            Assert.AreEqual(DhtState.Initialising, engine.State);

            // Then set an error and make sure the engine state moves to 'NotReady'
            errorSource.SetException(new Exception());
            await engine.WaitForState(DhtState.NotReady).WithTimeout(10000);
        }
예제 #2
0
        public async Task BucketRefreshTest()
        {
            List <Node> nodes = new List <Node>();

            for (int i = 0; i < 5; i++)
            {
                nodes.Add(new Node(NodeId.Create(), new IPEndPoint(IPAddress.Any, i)));
            }

            engine.MessageLoop.Timeout  = TimeSpan.FromMilliseconds(25);
            engine.BucketRefreshTimeout = TimeSpan.FromMilliseconds(75);
            listener.MessageSent       += (message, endpoint) => {
                Node current = nodes.Find(delegate(Node n) { return(n.EndPoint.Port.Equals(endpoint.Port)); });
                if (current == null)
                {
                    return;
                }

                if (message is Ping)
                {
                    PingResponse r = new PingResponse(current.Id, message.TransactionId);
                    listener.RaiseMessageReceived(r, current.EndPoint);
                }
                else if (message is FindNode)
                {
                    FindNodeResponse response = new FindNodeResponse(current.Id, message.TransactionId);
                    response.Nodes = "";
                    listener.RaiseMessageReceived(response, current.EndPoint);
                }
            };

            engine.Add(nodes);

            foreach (Bucket b in engine.RoutingTable.Buckets)
            {
                b.Changed(TimeSpan.FromDays(-1));
            }

            await engine.StartAsync();

            await engine.WaitForState(DhtState.Ready);

            foreach (Bucket b in engine.RoutingTable.Buckets)
            {
                Assert.IsTrue(b.LastChanged < TimeSpan.FromMinutes(1));
                Assert.IsTrue(b.Nodes.Exists(delegate(Node n) { return(n.LastSeen < TimeSpan.FromMilliseconds(900)); }));
            }
        }