public async System.Threading.Tasks.Task SendPingAsync()
        {
            await engine.AddAsync(node);

            engine.TimeOut = TimeSpan.FromMilliseconds(75);
            ManualResetEvent handle = new ManualResetEvent(false);

            engine.MessageLoop.QuerySent += delegate(object o, SendQueryEventArgs e) {
                if (!e.TimedOut && e.Query is Ping)
                {
                    handle.Set();
                }

                if (!e.TimedOut || !(e.Query is Ping))
                {
                    return;
                }

                PingResponse response = new PingResponse(node.Id, e.Query.TransactionId);
                listener.RaiseMessageReceived(response, e.EndPoint);
            };

            Assert.AreEqual(NodeState.Unknown, node.State, "#1");

            DateTime lastSeen = node.LastSeen;

            Assert.IsTrue(handle.WaitOne(1000, false), "#1a");
            Node nnnn = node;

            node = engine.RoutingTable.FindNode(nnnn.Id);
            Assert.IsTrue(lastSeen < node.LastSeen, "#2");
            Assert.AreEqual(NodeState.Good, node.State, "#3");
        }
예제 #2
0
        public async System.Threading.Tasks.Task BucketRefreshTestAsync()
        {
            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.TimeOut = TimeSpan.FromMilliseconds(25);
            engine.BucketRefreshTimeout   = TimeSpan.FromMilliseconds(75);
            engine.MessageLoop.QuerySent += delegate(object o, SendQueryEventArgs e)
            {
                DhtEngine.MainLoop.Queue(delegate
                {
                    if (!e.TimedOut)
                    {
                        return;
                    }

                    Node current = nodes.Find(delegate(Node n) { return(n.EndPoint.Port.Equals(e.EndPoint.Port)); });
                    if (current == null)
                    {
                        return;
                    }

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

            await engine.AddAsync(nodes);

            engine.Start();

            System.Threading.Thread.Sleep(500);
            foreach (Bucket b in engine.RoutingTable.Buckets)
            {
                Assert.IsTrue(b.LastChanged > DateTime.UtcNow.AddSeconds(-2));
                Assert.IsTrue(b.Nodes.Exists(delegate(Node n) { return(n.LastSeen > DateTime.UtcNow.AddMilliseconds(-900)); }));
            }
        }