From 411f147d36cc297f26731c48174bfab4a4586d44 Mon Sep 17 00:00:00 2001 From: Robert Vazan Date: Fri, 26 Jan 2018 01:12:04 +0100 Subject: [PATCH] Test cleanup: project name, mono. linux, NUnit, logging, code style, tolerated packet loss --- GuerrillaNtp.sln | 6 ++ GuerrillaNtpTests/GuerrillaNtpTests.csproj | 46 +++++++++++++++ GuerrillaNtpTests/NtpClientTests.cs | 68 ++++++++++++++++++++++ GuerrillaNtpTests/packages.config | 4 ++ Tests/NtpClientTests.cs | 45 -------------- Tests/Tests.csproj | 19 ------ 6 files changed, 124 insertions(+), 64 deletions(-) create mode 100644 GuerrillaNtpTests/GuerrillaNtpTests.csproj create mode 100644 GuerrillaNtpTests/NtpClientTests.cs create mode 100644 GuerrillaNtpTests/packages.config delete mode 100644 Tests/NtpClientTests.cs delete mode 100644 Tests/Tests.csproj diff --git a/GuerrillaNtp.sln b/GuerrillaNtp.sln index 8fb77e7..fc25461 100644 --- a/GuerrillaNtp.sln +++ b/GuerrillaNtp.sln @@ -5,6 +5,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GuerrillaNtp", "GuerrillaNt EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NtpCommand", "NtpCommand\NtpCommand.csproj", "{5B76615E-2B5E-45B7-9850-20D05AEFCEAD}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GuerrillaNtpTests", "GuerrillaNtpTests\GuerrillaNtpTests.csproj", "{4969DA50-E64D-42E6-823C-3383EDA51E8A}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -19,5 +21,9 @@ Global {5B76615E-2B5E-45B7-9850-20D05AEFCEAD}.Debug|Any CPU.Build.0 = Debug|Any CPU {5B76615E-2B5E-45B7-9850-20D05AEFCEAD}.Release|Any CPU.ActiveCfg = Release|Any CPU {5B76615E-2B5E-45B7-9850-20D05AEFCEAD}.Release|Any CPU.Build.0 = Release|Any CPU + {4969DA50-E64D-42E6-823C-3383EDA51E8A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4969DA50-E64D-42E6-823C-3383EDA51E8A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4969DA50-E64D-42E6-823C-3383EDA51E8A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4969DA50-E64D-42E6-823C-3383EDA51E8A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/GuerrillaNtpTests/GuerrillaNtpTests.csproj b/GuerrillaNtpTests/GuerrillaNtpTests.csproj new file mode 100644 index 0000000..9257f7c --- /dev/null +++ b/GuerrillaNtpTests/GuerrillaNtpTests.csproj @@ -0,0 +1,46 @@ + + + + Debug + AnyCPU + {4969DA50-E64D-42E6-823C-3383EDA51E8A} + Library + GuerrillaNtpTests + GuerrillaNtpTests + v4.6.1 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + + + true + bin\Release + prompt + 4 + + + + + ..\packages\NUnit.3.9.0\lib\net45\nunit.framework.dll + + + + + + + + {33509321-B1A1-41C0-9578-D7D50904462E} + GuerrillaNtp + + + + + + + \ No newline at end of file diff --git a/GuerrillaNtpTests/NtpClientTests.cs b/GuerrillaNtpTests/NtpClientTests.cs new file mode 100644 index 0000000..d74db19 --- /dev/null +++ b/GuerrillaNtpTests/NtpClientTests.cs @@ -0,0 +1,68 @@ +using System; +using System.Diagnostics; +using System.Net; +using System.Net.Sockets; +using NUnit.Framework; +using GuerrillaNtp; + +namespace Tests +{ + [TestFixture] + public class NtpClientTests + { + private IPAddress server = Dns.GetHostEntry("pool.ntp.org").AddressList[0]; + + [Test] + public void Test_can_get_correction_offset() + { + const int tries = 10; + int hits = 0; + using (var client = new NtpClient(server)) + { + for (int i = 0; i < tries; i++) + { + try + { + Console.WriteLine($"Offset #{i + 1}: {client.GetCorrectionOffset()}"); + ++hits; + } + catch (Exception ex) + { + Console.WriteLine($"Offset #{i + 1}: {ex}"); + } + } + } + Console.WriteLine($"Got {hits} of {tries} replies"); + Assert.GreaterOrEqual(2 * hits, tries); + } + + [Test] + public void Test_Timeout_expires() + { + var timeout = TimeSpan.FromMilliseconds(500); + + // Note: pick a host that *drops* packets. The test will fail if the host merely *rejects* packets. + using (var client = new NtpClient(IPAddress.Parse("8.8.8.8"))) + { + client.Timeout = timeout; + + var timer = Stopwatch.StartNew(); + + try + { + client.GetCorrectionOffset(); + Assert.Fail("Shouldn't get here. Expecting timeout!"); + } + catch (SocketException ex) when (ex.ErrorCode == 10060 || ex.ErrorCode == 10035) + { + // We expect a socket timeout error + } + + timer.Stop(); + + Assert.IsTrue(timer.Elapsed >= timeout, timer.Elapsed.ToString()); + Assert.IsTrue(timer.Elapsed < timeout + timeout, timer.Elapsed.ToString()); + } + } + } +} diff --git a/GuerrillaNtpTests/packages.config b/GuerrillaNtpTests/packages.config new file mode 100644 index 0000000..b93fa21 --- /dev/null +++ b/GuerrillaNtpTests/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Tests/NtpClientTests.cs b/Tests/NtpClientTests.cs deleted file mode 100644 index 46307e4..0000000 --- a/Tests/NtpClientTests.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; -using System.Diagnostics; -using System.Net; -using System.Net.Sockets; - -using GuerrillaNtp; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace Tests { - [TestClass] - public class NtpClientTests { - private IPAddress server = Dns.GetHostEntry("pool.ntp.org").AddressList[0]; - - [TestMethod] - public void Test_can_get_correction_offset() { - using (var client = new NtpClient(server)) { - for (int i = 0; i < 10; i++) - Trace.WriteLine($"Offset #{i+1}: {client.GetCorrectionOffset()}"); - } - } - - [TestMethod] - public void Test_Timeout_expires() { - var timeout = TimeSpan.FromMilliseconds(500); - - // Note: pick a host that *drops* packets. The test will fail if the host merely *rejects* packets. - using (var client = new NtpClient(IPAddress.Parse("8.8.8.8"))) { - client.Timeout = timeout; - - var timer = Stopwatch.StartNew(); - - try { - client.GetCorrectionOffset(); - Assert.Fail("Shouldn't get here. Expecting timeout!"); - } - catch (SocketException ex) when (ex.ErrorCode == 10060) { /* We expect a socket timeout error */ } - - timer.Stop(); - - Assert.IsTrue(timer.Elapsed >= timeout, timer.Elapsed.ToString()); - Assert.IsTrue(timer.Elapsed < 2 * timeout, timer.Elapsed.ToString()); - } - } - } -} diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj deleted file mode 100644 index f919eac..0000000 --- a/Tests/Tests.csproj +++ /dev/null @@ -1,19 +0,0 @@ - - - - netcoreapp2.0 - - false - - - - - - - - - - - - -