Skip to content

Commit

Permalink
Attempt to make threading tests more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
oschwald committed Apr 1, 2014
1 parent aea8dba commit 484bea1
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions MaxMind.Db.Test/ThreadingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IPAddress, string>();
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]
Expand All @@ -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");
}
});
}
}
Expand Down

0 comments on commit 484bea1

Please sign in to comment.