public void NodeReplaceTest() { _engine.TimeOut = TimeSpan.FromMilliseconds(25); var handle = new ManualResetEvent(false); var b = new Bucket(); for (var i = 0; i < Bucket.MaxCapacity; i++) { var n = new Node(NodeId.Create(), new IPEndPoint(IPAddress.Any, i)) { LastSeen = DateTime.UtcNow }; b.Add(n); } b.Nodes[3].LastSeen = DateTime.UtcNow.AddDays(-5); b.Nodes[1].LastSeen = DateTime.UtcNow.AddDays(-4); b.Nodes[5].LastSeen = DateTime.UtcNow.AddDays(-3); _engine.MessageLoop.QuerySent += delegate(object o, SendQueryEventArgs e) { if (!e.TimedOut) return; b.Nodes.Sort(); if ((e.EndPoint.Port == 3 && _nodeCount == 0) || (e.EndPoint.Port == 1 && _nodeCount == 1) || (e.EndPoint.Port == 5 && _nodeCount == 2)) { var n = b.Nodes.Find(delegate(Node no) { return no.EndPoint.Port == e.EndPoint.Port; }); n.Seen(); var response = new PingResponse(n.Id, e.Query.TransactionId); DhtEngine.MainLoop.Queue(delegate { //System.Threading.Thread.Sleep(100); Console.WriteLine("Faking the receive"); _listener.RaiseMessageReceived(response, _node.EndPoint); }); _nodeCount++; } }; var task = new ReplaceNodeTask(_engine, b, null); // FIXME: Need to assert that node 0.0.0.0:0 is the one which failed - i.e. it should be replaced task.Completed += (o, e) => handle.Set(); task.Execute(); Assert.IsTrue(handle.WaitOne(4000, false), "#10"); }
public void ReplaceNodeTest() { _engine.TimeOut = TimeSpan.FromMilliseconds(25); var replacement = new Node(NodeId.Create(), new IPEndPoint(IPAddress.Loopback, 1337)); for(var i=0; i < 4; i++) { var node = new Node(NodeId.Create(), new IPEndPoint(IPAddress.Any, i)) {LastSeen = DateTime.UtcNow.AddMinutes(-i)}; _engine.RoutingTable.Add(node); } var nodeToReplace = _engine.RoutingTable.Buckets[0].Nodes[3]; var task = new ReplaceNodeTask(_engine, _engine.RoutingTable.Buckets[0], replacement); task.Completed += (sender, args) => _handle.Set(); task.Execute(); Assert.IsTrue(_handle.WaitOne(1000, true), "#a"); Assert.IsFalse(_engine.RoutingTable.Buckets[0].Nodes.Contains(nodeToReplace), "#1"); Assert.IsTrue(_engine.RoutingTable.Buckets[0].Nodes.Contains(replacement), "#2"); }