This repository was archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathConcurrentLruCacheTest.cs
120 lines (98 loc) · 3.57 KB
/
ConcurrentLruCacheTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace Microsoft.AspNetCore.Razor.Tools
{
public class ConcurrentLruCacheTest
{
[Fact]
public void ConcurrentLruCache_HoldsCapacity()
{
// Arrange
var input = GetKeyValueArray(Enumerable.Range(1, 3));
var expected = input.Reverse();
// Act
var cache = new ConcurrentLruCache<int, int>(input);
// Assert
Assert.Equal(expected, cache.TestingEnumerable);
}
[Fact]
public void Add_ThrowsIfKeyExists()
{
// Arrange
var input = GetKeyValueArray(Enumerable.Range(1, 3));
var cache = new ConcurrentLruCache<int, int>(input);
// Act & Assert
var exception = Assert.Throws<ArgumentException>(() => cache.Add(1, 1));
Assert.StartsWith("Key already exists", exception.Message);
}
[Fact]
public void GetOrAdd_AddsIfKeyDoesNotExist()
{
// Arrange
var input = GetKeyValueArray(Enumerable.Range(1, 3));
var expected = GetKeyValueArray(Enumerable.Range(2, 3)).Reverse();
var cache = new ConcurrentLruCache<int, int>(input);
// Act
cache.GetOrAdd(4, 4);
// Assert
Assert.Equal(expected, cache.TestingEnumerable);
}
[Fact]
public void Remove_RemovesEntry()
{
// Arrange
var input = GetKeyValueArray(Enumerable.Range(1, 3));
var expected = GetKeyValueArray(Enumerable.Range(1, 2)).Reverse();
var cache = new ConcurrentLruCache<int, int>(input);
// Act
var result = cache.Remove(3);
// Assert
Assert.True(result);
Assert.Equal(expected, cache.TestingEnumerable);
}
[Fact]
public void Remove_KeyNotFound_ReturnsFalse()
{
// Arrange
var input = GetKeyValueArray(Enumerable.Range(1, 3));
var cache = new ConcurrentLruCache<int, int>(input);
// Act
var result = cache.Remove(4);
// Assert
Assert.False(result);
}
[Fact]
public void Add_NoRead_EvictsLastNode()
{
// Arrange
var input = GetKeyValueArray(Enumerable.Range(1, 3));
var expected = GetKeyValueArray(Enumerable.Range(2, 3)).Reverse();
var cache = new ConcurrentLruCache<int, int>(input);
// Act
cache.Add(4, 4);
// Assert
Assert.Equal(expected, cache.TestingEnumerable);
}
[Fact]
public void Add_ReadLastNode_EvictsSecondOldestNode()
{
// Arrange
var input = GetKeyValueArray(Enumerable.Range(1, 3));
var expected = GetKeyValueArray(new int[] { 4, 1, 3 });
var cache = new ConcurrentLruCache<int, int>(input);
// Act
cache.GetOrAdd(1, 1); // Read to make this MRU
cache.Add(4, 4); // Add a new node
// Assert
Assert.Equal(expected, cache.TestingEnumerable);
}
private KeyValuePair<int, int>[] GetKeyValueArray(IEnumerable<int> inputArray)
{
return inputArray.Select(v => new KeyValuePair<int, int>(v, v)).ToArray();
}
}
}