Skip to content

Commit d0c0249

Browse files
committed
Fixed all test warnings
1 parent 458cede commit d0c0249

File tree

4 files changed

+30
-28
lines changed

4 files changed

+30
-28
lines changed

UnitTests/EvictionCallbackTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ namespace UnitTests;
66
[TestClass]
77
public class EvictionCallbackTests
88
{
9-
private List<string> _evictedKeys;
10-
private FastCache<string, string> _cache;
9+
private List<string> _evictedKeys = new();
10+
private FastCache<string, string> _cache = new();
1111

1212
[TestInitialize]
1313
public void Setup()
@@ -29,7 +29,7 @@ public async Task WhenItemExpires_EvictionCallbackFires()
2929
await Task.Delay(120); // Wait for expiration background job
3030

3131
// Assert
32-
Assert.AreEqual(1, _evictedKeys.Count);
32+
Assert.HasCount(1, _evictedKeys);
3333
Assert.AreEqual(key, _evictedKeys[0]);
3434
}
3535

@@ -62,7 +62,7 @@ public void WhenItemNotExpired_CallbackDoesNotFire()
6262
_cache.EvictExpired();
6363

6464
// Assert
65-
Assert.AreEqual(0, _evictedKeys.Count);
65+
Assert.IsEmpty(_evictedKeys);
6666
}
6767

6868
[TestMethod]
@@ -75,6 +75,6 @@ public async Task AutomaticCleanup_FiresCallback()
7575
await Task.Delay(120); // Wait for cleanup job
7676

7777
// Assert
78-
Assert.AreEqual(1, _evictedKeys.Count);
78+
Assert.HasCount(1, _evictedKeys);
7979
}
8080
}

UnitTests/TestHelper.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace UnitTests
1+
[assembly: Parallelize(Workers = 3, Scope = ExecutionScope.ClassLevel)]
2+
3+
namespace UnitTests
24
{
35
internal class TestHelper
46
{

UnitTests/UnitTests.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ public async Task TestGetSetCleanup()
1212
using var _cache = new FastCache<int, int>(cleanupJobInterval: 200); //add "using" to stop cleanup timer, to prevent cleanup job from clashing with other tests
1313
_cache.AddOrUpdate(42, 42, TimeSpan.FromMilliseconds(100));
1414
Assert.IsTrue(_cache.TryGet(42, out int v));
15-
Assert.IsTrue(v == 42);
15+
Assert.AreEqual(42, v);
1616

1717
await Task.Delay(300);
18-
Assert.IsTrue(_cache.Count == 0); //cleanup job has run?
18+
Assert.AreEqual(0, _cache.Count); //cleanup job has run?
1919
}
2020

2121
[TestMethod]
@@ -32,7 +32,7 @@ public async Task TestEviction()
3232

3333
for (int i = 0; i < 20; i++)
3434
{
35-
Assert.IsTrue(list[i].Count == 0); //cleanup job has run?
35+
Assert.AreEqual(0, list[i].Count); //cleanup job has run?
3636
}
3737

3838
//cleanup
@@ -51,7 +51,7 @@ public async Task Shortdelay()
5151
await Task.Delay(50);
5252

5353
Assert.IsTrue(cache.TryGet(42, out int result)); //not evicted
54-
Assert.IsTrue(result == 42);
54+
Assert.AreEqual(42, result);
5555
}
5656

5757
[TestMethod]
@@ -85,7 +85,7 @@ public void TestTryRemove()
8585
//now try remove non-existing item
8686
res = cache.TryRemove("blabblah", out value);
8787
Assert.IsFalse(res);
88-
Assert.IsTrue(value == 0);
88+
Assert.AreEqual(0, value);
8989
}
9090

9191
[TestMethod]
@@ -97,7 +97,7 @@ public async Task TestTryRemoveWithTtl()
9797

9898
var res = cache.TryRemove("42", out int value);
9999
Assert.IsFalse(res);
100-
Assert.IsTrue(value == 0);
100+
Assert.AreEqual(0, value);
101101
}
102102

103103
[TestMethod]
@@ -117,17 +117,17 @@ public async Task TestGetOrAdd()
117117
{
118118
var cache = new FastCache<string, int>();
119119
cache.GetOrAdd("key", k => 1024, TimeSpan.FromMilliseconds(100));
120-
Assert.AreEqual(cache.GetOrAdd("key", k => 1025, TimeSpan.FromMilliseconds(100)), 1024); //old value
120+
Assert.AreEqual(1024, cache.GetOrAdd("key", k => 1025, TimeSpan.FromMilliseconds(100))); //old value
121121
Assert.IsTrue(cache.TryGet("key", out int res) && res == 1024); //another way to retrieve
122122
await Task.Delay(110);
123123

124124
Assert.IsFalse(cache.TryGet("key", out _)); //expired
125125

126126
//now try non-factory overloads
127-
Assert.IsTrue(cache.GetOrAdd("key123", 123321, TimeSpan.FromMilliseconds(100)) == 123321);
128-
Assert.IsTrue(cache.GetOrAdd("key123", -1, TimeSpan.FromMilliseconds(100)) == 123321); //still old value
127+
Assert.AreEqual(123321, cache.GetOrAdd("key123", 123321, TimeSpan.FromMilliseconds(100)));
128+
Assert.AreEqual(123321, cache.GetOrAdd("key123", -1, TimeSpan.FromMilliseconds(100))); //still old value
129129
await Task.Delay(110);
130-
Assert.IsTrue(cache.GetOrAdd("key123", -1, TimeSpan.FromMilliseconds(100)) == -1); //new value
130+
Assert.AreEqual(-1, cache.GetOrAdd("key123", -1, TimeSpan.FromMilliseconds(100))); //new value
131131
}
132132

133133

@@ -137,12 +137,12 @@ public async Task TestGetOrAddExpiration()
137137
var cache = new FastCache<string, int>();
138138
cache.GetOrAdd("key", k => 1024, TimeSpan.FromMilliseconds(100));
139139

140-
Assert.AreEqual(cache.GetOrAdd("key", k => 1025, TimeSpan.FromMilliseconds(100)), 1024); //old value
140+
Assert.AreEqual(1024, cache.GetOrAdd("key", k => 1025, TimeSpan.FromMilliseconds(100))); //old value
141141
Assert.IsTrue(cache.TryGet("key", out int res) && res == 1024); //another way to retrieve
142142

143143
await Task.Delay(110); //let the item expire
144144

145-
Assert.AreEqual(cache.GetOrAdd("key", k => 1025, TimeSpan.FromMilliseconds(100)), 1025); //new value
145+
Assert.AreEqual(1025, cache.GetOrAdd("key", k => 1025, TimeSpan.FromMilliseconds(100))); //new value
146146
Assert.IsTrue(cache.TryGet("key", out res) && res == 1025); //another way to retrieve
147147
}
148148

@@ -158,10 +158,10 @@ public async Task TestGetOrAddWithArg()
158158
Assert.IsFalse(cache.TryGet("key", out _));
159159

160160
//now try without "TryGet"
161-
Assert.IsTrue(cache.GetOrAdd("key2", (k, arg) => 21 + arg.Length, TimeSpan.FromMilliseconds(100), "123") == 24);
162-
Assert.IsTrue(cache.GetOrAdd("key2", (k, arg) => 2211 + arg.Length, TimeSpan.FromMilliseconds(100), "123") == 24);
161+
Assert.AreEqual(24, cache.GetOrAdd("key2", (k, arg) => 21 + arg.Length, TimeSpan.FromMilliseconds(100), "123"));
162+
Assert.AreEqual(24, cache.GetOrAdd("key2", (k, arg) => 2211 + arg.Length, TimeSpan.FromMilliseconds(100), "123"));
163163
await Task.Delay(110);
164-
Assert.IsTrue(cache.GetOrAdd("key2", (k, arg) => 2211 + arg.Length, TimeSpan.FromMilliseconds(100), "123") == 2214);
164+
Assert.AreEqual(2214, cache.GetOrAdd("key2", (k, arg) => 2211 + arg.Length, TimeSpan.FromMilliseconds(100), "123"));
165165
}
166166

167167
[TestMethod]
@@ -172,7 +172,7 @@ public void TestClear()
172172

173173
cache.Clear();
174174

175-
Assert.IsTrue(!cache.TryGet("key", out int res));
175+
Assert.IsFalse(cache.TryGet("key", out int res));
176176
}
177177

178178
[TestMethod]
@@ -190,7 +190,7 @@ await TestHelper.RunConcurrently(20, () => {
190190
i++;
191191
});
192192

193-
Assert.IsTrue(i == 1, i.ToString());
193+
Assert.AreEqual(1, i);
194194
}
195195

196196
//this text can occasionally fail becasue factory is not guaranteed to be called only once. only panic if it fails ALL THE TIME
@@ -211,7 +211,7 @@ await TestHelper.RunConcurrently(20, () => {
211211

212212
//test that only the first value was added
213213
cache.TryGet(42, out i);
214-
Assert.IsTrue(i == 1, i.ToString());
214+
Assert.AreEqual(1, i);
215215
}
216216

217217
[TestMethod]
@@ -220,7 +220,7 @@ public async Task Enumerator()
220220
var cache = new FastCache<string, int>(); //now with default cleanup interval
221221
cache.GetOrAdd("key", k => 1024, TimeSpan.FromMilliseconds(100));
222222

223-
Assert.IsTrue(cache.FirstOrDefault().Value == 1024);
223+
Assert.AreEqual(1024, cache.FirstOrDefault().Value);
224224

225225
await Task.Delay(110);
226226

@@ -235,14 +235,14 @@ public async Task TestTtlExtended()
235235

236236
await Task.Delay(50);
237237
Assert.IsTrue(_cache.TryGet(42, out int result)); //not evicted
238-
Assert.IsTrue(result == 42);
238+
Assert.AreEqual(42, result);
239239

240240
_cache.AddOrUpdate(42, 42, TimeSpan.FromMilliseconds(300));
241241

242242
await Task.Delay(250);
243243

244244
Assert.IsTrue(_cache.TryGet(42, out int result2)); //still not evicted
245-
Assert.IsTrue(result2 == 42);
245+
Assert.AreEqual(42, result2);
246246
}
247247
}
248248
}

UnitTests/UnitTests2.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public void Enumeration_ReturnsOnlyNonExpiredItems()
203203
var items = cache.ToList();
204204

205205
// Assert
206-
Assert.AreEqual(1, items.Count);
206+
Assert.HasCount(1, items);
207207
Assert.AreEqual(42, items[0].Value);
208208
Assert.AreEqual("key1", items[0].Key);
209209
}

0 commit comments

Comments
 (0)