From 484bea169c6978a78f54b98b0ab71749d4600770 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Tue, 1 Apr 2014 10:27:58 -0700 Subject: [PATCH] Attempt to make threading tests more robust --- MaxMind.Db.Test/ThreadingTest.cs | 49 ++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/MaxMind.Db.Test/ThreadingTest.cs b/MaxMind.Db.Test/ThreadingTest.cs index 66ddd32..8f4bdff 100644 --- a/MaxMind.Db.Test/ThreadingTest.cs +++ b/MaxMind.Db.Test/ThreadingTest.cs @@ -16,34 +16,36 @@ public class ThreadingTest [TestCase(FileAccessMode.Memory)] public void TestParallelFor(FileAccessMode mode) { - var reader = new Reader(Path.Combine("..", "..", "TestData", "GeoLite2-City.mmdb"), mode); var count = 0; var ipsAndResults = new Dictionary(); var rand = new Random(); - while (count < 10000) + using (var reader = new Reader(Path.Combine("..", "..", "TestData", "GeoLite2-City.mmdb"), mode)) { - var ip = new IPAddress(rand.Next(int.MaxValue)); - var resp = reader.Find(ip); - if (resp != null) + while (count < 10000) { - ipsAndResults.Add(ip, resp.ToString()); - count++; + var ip = new IPAddress(rand.Next(int.MaxValue)); + var resp = reader.Find(ip); + if (resp != null) + { + ipsAndResults.Add(ip, resp.ToString()); + count++; + } } - } - var ips = ipsAndResults.Keys.ToArray(); - var start = DateTime.Now; - Parallel.For(0, ips.Length, i => - { - var ipAddress = ips[i]; - var result = reader.Find(ipAddress); - var resultString = result.ToString(); - var expectedString = ipsAndResults[ipAddress]; - if (resultString != expectedString) - throw new Exception(string.Format("Non-matching result. Expected {0}, found {1}", expectedString, resultString)); - }); - var stop = DateTime.Now; - Console.WriteLine("Requests per second: " + count / (stop - start).TotalSeconds); + var ips = ipsAndResults.Keys.ToArray(); + var start = DateTime.Now; + Parallel.For(0, ips.Length, i => + { + var ipAddress = ips[i]; + var result = reader.Find(ipAddress); + var resultString = result.ToString(); + var expectedString = ipsAndResults[ipAddress]; + if (resultString != expectedString) + throw new Exception(string.Format("Non-matching result. Expected {0}, found {1}", expectedString, resultString)); + }); + var stop = DateTime.Now; + Console.WriteLine("Requests per second: " + count / (stop - start).TotalSeconds); + } } [Test] @@ -54,7 +56,10 @@ public void TestManyOpens(FileAccessMode mode) { Parallel.For(0, 1000, i => { - var reader = new Reader(Path.Combine("..", "..", "TestData", "GeoLite2-City.mmdb"), mode); + using (var reader = new Reader(Path.Combine("..", "..", "TestData", "GeoLite2-City.mmdb"), mode)) + { + reader.Find("1.1.1.1"); + } }); } }