Skip to content

Commit 39086bb

Browse files
committed
And a few more unit tests
1 parent e6b7c05 commit 39086bb

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

tests/Agent/UnitTests/NewRelic.Agent.Extensions.Tests/Cache/SimpleCacheTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,49 @@ public void CacheMaintenanceThreadMaintainsCache()
273273
EvaluateCacheMetrics(cache, 0, 2, 2, 0);
274274
}
275275

276+
[Test]
277+
public void TryAdd_AddsNewItemToCache()
278+
{
279+
var val1 = "value1";
280+
int capacity = 5;
281+
var cache = new SimpleCache<string, string>(capacity);
282+
283+
var result = cache.TryAdd("key1", () => val1);
284+
285+
Assert.That(result, Is.True);
286+
Assert.That(cache.Peek("key1"), Is.SameAs(val1));
287+
}
288+
289+
[Test]
290+
public void TryAdd_DoesNotAddExistingItemToCache()
291+
{
292+
var val1 = "value1";
293+
var val2 = "value2";
294+
int capacity = 5;
295+
var cache = new SimpleCache<string, string>(capacity);
296+
297+
cache.TryAdd("key1", () => val1);
298+
var result = cache.TryAdd("key1", () => val2);
299+
300+
Assert.That(result, Is.False);
301+
Assert.That(cache.Peek("key1"), Is.SameAs(val1));
302+
}
303+
304+
[Test]
305+
public void Dispose_ClearsCache()
306+
{
307+
var val1 = "value1";
308+
int capacity = 5;
309+
var cache = new SimpleCache<string, string>(capacity);
310+
311+
cache.GetOrAdd("key1", () => val1);
312+
cache.Dispose();
313+
314+
Assert.That(cache.Size, Is.EqualTo(0));
315+
Assert.That(cache.Peek("key1"), Is.Null);
316+
}
317+
318+
276319
private void EvaluateCacheMetrics<T, V>(SimpleCache<T, V> cache, int expectedHits, int expectedMisses,
277320
int expectedEjections, int expectedSize) where V : class
278321
{

0 commit comments

Comments
 (0)