public void PingTimeout() { engine.TimeOut = TimeSpan.FromHours(1); // Send ping Ping ping = new Ping(node.Id); ping.TransactionId = transactionId; ManualResetEvent handle = new ManualResetEvent(false); var sendTask = engine.SendQueryAsync(ping, node); // Receive response PingResponse response = new PingResponse(node.Id, transactionId); listener.RaiseMessageReceived(response, node.EndPoint); Assert.IsTrue(sendTask.Wait(1000), "#0"); engine.TimeOut = TimeSpan.FromMilliseconds(75); DateTime lastSeen = node.LastSeen; // Time out a ping ping = new Ping(node.Id); ping.TransactionId = (BEncodedString)"ab"; sendTask = engine.SendQueryAsync(ping, node); sendTask.Wait(1000); Assert.AreEqual(4, node.FailedCount, "#1"); Assert.AreEqual(NodeState.Bad, node.State, "#2"); Assert.AreEqual(lastSeen, node.LastSeen, "#3"); }
public void SendQueryTaskTimeout() { engine.TimeOut = TimeSpan.FromMilliseconds(25); Ping ping = new Ping(engine.LocalId); ping.TransactionId = transactionId; engine.MessageLoop.QuerySent += delegate(object o, SendQueryEventArgs e) { if (e.TimedOut) { counter++; } }; Assert.IsTrue(engine.SendQueryAsync(ping, node).Wait(3000), "#1"); }
public async Task SendQueryTaskTimeout() { engine.MessageLoop.Timeout = TimeSpan.Zero; Ping ping = new Ping(engine.LocalId); ping.TransactionId = transactionId; engine.MessageLoop.QuerySent += delegate(object o, SendQueryEventArgs e) { if (e.TimedOut) { counter++; } }; Assert.IsTrue((await engine.SendQueryAsync(ping, node).WithTimeout(3000)).TimedOut, "#1"); Assert.AreEqual(4, counter, "#2"); }
public void ErrorReceived() { int failedCount = 0; var pingSuccessful = new TaskCompletionSource <bool> (); var ping = new Ping(node.Id) { TransactionId = transactionId }; engine.MessageLoop.QuerySent += (o, e) => { // This ping should not time out. if (e.Query.TransactionId.Equals(ping.TransactionId)) { pingSuccessful.TrySetResult(!e.TimedOut && e.Response == null && e.Error != null); } }; listener.MessageSent += (data, endpoint) => { engine.MessageLoop.DhtMessageFactory.TryDecodeMessage(BEncodedValue.Decode <BEncodedDictionary> (data), out DhtMessage message); // This TransactionId should be registered and it should be pending a response. if (!DhtMessageFactory.IsRegistered(ping.TransactionId) || engine.MessageLoop.PendingQueries != 1) { pingSuccessful.TrySetResult(false); } if (message.TransactionId.Equals(ping.TransactionId)) { listener.RaiseMessageReceived(new ErrorMessage(ping.TransactionId, ErrorCode.ServerError, "Ooops"), node.EndPoint); failedCount++; } }; // Send the ping var task = engine.SendQueryAsync(ping, node); // The query should complete, and the message should not have timed out. Assert.IsTrue(task.Wait(100000), "#1"); Assert.IsTrue(pingSuccessful.Task.Wait(1000), "#2"); Assert.IsTrue(pingSuccessful.Task.Result, "#3"); Assert.IsFalse(DhtMessageFactory.IsRegistered(ping.TransactionId), "#4"); Assert.AreEqual(0, engine.MessageLoop.PendingQueries, "#5"); Assert.AreEqual(1, failedCount, "#6"); }
public async void Execute() { if (bucket.Nodes.Count == 0) { return; } bucket.SortBySeen(); foreach (var node in bucket.Nodes.ToArray()) { var message = new FindNode(engine.LocalId, node.Id); var args = await engine.SendQueryAsync(message, node); if (!args.TimedOut) { return; } } }