Skip to content

Commit 6de1a93

Browse files
committed
radical idea ... write unit tests so code I check in does not act in all sorts of random ways
1 parent c9968b3 commit 6de1a93

File tree

3 files changed

+84
-13
lines changed

3 files changed

+84
-13
lines changed

StackExchange.Profiling.Tests/StackExchange.Profiling.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
<Compile Include="Properties\AssemblyInfo.cs" />
7575
<Compile Include="MiniProfilerTest.cs" />
7676
<Compile Include="Storage\SqlServerStorageTest.cs" />
77+
<Compile Include="Storage\TestHttpRuntimeCacheStorage.cs" />
7778
</ItemGroup>
7879
<ItemGroup>
7980
<None Include="packages.config" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Text;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using StackExchange.Profiling.Storage;
6+
using NUnit.Framework;
7+
8+
namespace StackExchange.Profiling.Tests.Storage
9+
{
10+
[TestFixture]
11+
public class TestHttpRuntimeCacheStorage
12+
{
13+
[Test]
14+
public void TestWeCanSaveTheSameProfilerTwice()
15+
{
16+
var profiler = new MiniProfiler();
17+
profiler.Started = DateTime.UtcNow;
18+
profiler.Id = Guid.NewGuid();
19+
var storage = new HttpRuntimeCacheStorage(new TimeSpan(1,0,0));
20+
storage.Save(profiler);
21+
storage.Save(profiler);
22+
var guids = storage.List(100);
23+
Assert.AreEqual(profiler.Id, guids.First());
24+
Assert.AreEqual(1, guids.Count());
25+
}
26+
27+
[Test]
28+
public void TestRangeQueries()
29+
{
30+
var now = DateTime.UtcNow;
31+
var inASec = now.AddSeconds(1);
32+
var in2Secs = now.AddSeconds(2);
33+
var in3Secs = now.AddSeconds(3);
34+
var profiler = new MiniProfiler { Started = now, Id = Guid.NewGuid() };
35+
var profiler1 = new MiniProfiler { Started = inASec, Id = Guid.NewGuid() };
36+
var profiler2 = new MiniProfiler { Started = in2Secs, Id = Guid.NewGuid() };
37+
var profiler3 = new MiniProfiler { Started = in3Secs, Id = Guid.NewGuid() };
38+
39+
var storage = new HttpRuntimeCacheStorage(new TimeSpan(1, 0, 0));
40+
41+
storage.Save(profiler);
42+
storage.Save(profiler3);
43+
storage.Save(profiler2);
44+
storage.Save(profiler1);
45+
46+
var guids = storage.List(100);
47+
Assert.AreEqual(4, guids.Count());
48+
49+
guids = storage.List(1);
50+
Assert.AreEqual(1, guids.Count());
51+
52+
guids = storage.List(2, now, in2Secs, ListResultsOrder.Decending);
53+
Assert.AreEqual(profiler2.Id, guids.First());
54+
Assert.AreEqual(profiler1.Id, guids.Skip(1).First());
55+
Assert.AreEqual(2, guids.Count());
56+
}
57+
}
58+
}

StackExchange.Profiling/Storage/HttpRuntimeCacheStorage.cs

+25-13
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,16 @@ public void Save(MiniProfiler profiler)
5757

5858
lock (profiles)
5959
{
60-
profiles.Add(new ProfileInfo { Id = profiler.Id, Started = profiler.Started }, null);
60+
var profileInfo = new ProfileInfo { Id = profiler.Id, Started = profiler.Started };
61+
if (profiles.IndexOfKey(profileInfo) < 0)
62+
{
63+
profiles.Add(profileInfo, null);
64+
}
6165

6266
while (profiles.Count > 0)
6367
{
6468
var first = profiles.Keys[0];
65-
if (first.Started < DateTime.Now.Add(-CacheDuration))
69+
if (first.Started < DateTime.UtcNow.Add(-CacheDuration))
6670
{
6771
profiles.RemoveAt(0);
6872
}
@@ -182,24 +186,32 @@ public IEnumerable<Guid> List(int maxResults, DateTime? start = null, DateTime?
182186
lock (profiles)
183187
{
184188
int idxStart = 0;
185-
int idxFinish = 0;
189+
int idxFinish = profiles.Count - 1;
186190
if (start != null) idxStart = BinaryClosestSearch(start.Value);
187191
if (finish != null) idxFinish = BinaryClosestSearch(finish.Value);
188192

189-
int delta = 1;
190-
if (orderBy == ListResultsOrder.Decending)
191-
{
192-
delta = -1;
193-
int tmp = idxStart;
194-
idxStart = idxFinish;
195-
idxFinish = idxStart;
196-
}
193+
if (idxStart < 0) idxStart = 0;
194+
if (idxFinish >= profiles.Count) idxFinish = profiles.Count - 1;
195+
197196
var keys = profiles.Keys;
198197

199-
for (int i = idxStart; i < idxFinish; i+=delta)
198+
if (orderBy == ListResultsOrder.Ascending)
199+
{
200+
for (int i = idxStart; i <= idxFinish; i++)
201+
{
202+
guids.Add(keys[i].Id);
203+
if (guids.Count == maxResults) break;
204+
}
205+
}
206+
else
200207
{
201-
guids.Add(keys[i].Id);
208+
for (int i = idxFinish; i >= idxStart; i--)
209+
{
210+
guids.Add(keys[i].Id);
211+
if (guids.Count == maxResults) break;
212+
}
202213
}
214+
203215
}
204216
return guids;
205217
}

0 commit comments

Comments
 (0)