public static async Task SendAsyncs_ReuseInstance_Hostname()
{
IPAddress localIpAddress = await TestSettings.GetLocalIPAddress();
using (Ping p = new Ping())
{
TaskCompletionSource<bool> tcs = null;
PingCompletedEventArgs ea = null;
p.PingCompleted += (s, e) =>
{
ea = e;
tcs.TrySetResult(true);
};
Action reset = () =>
{
ea = null;
tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
};
// Several normal iterations
for (int i = 0; i < 3; i++)
{
reset();
p.SendAsync(TestSettings.LocalHost, null);
await tcs.Task;
Assert.NotNull(ea);
Assert.Equal(IPStatus.Success, ea.Reply.Status);
Assert.True(ea.Reply.Address.Equals(localIpAddress));
}
// Several canceled iterations
for (int i = 0; i < 3; i++)
{
reset();
p.SendAsync(TestSettings.LocalHost, null);
p.SendAsyncCancel(); // will block until operation can be started again
}
await tcs.Task;
Assert.True(ea.Cancelled ^ (ea.Error != null) ^ (ea.Reply != null));
}
}