Skip to content

Commit 61508e2

Browse files
authored
Merge pull request #160 from DataObjects-NET/PR-159-code-fixes
Fixes the mistakes made in PR-159
2 parents a85a815 + 9fc588a commit 61508e2

File tree

5 files changed

+92
-119
lines changed

5 files changed

+92
-119
lines changed

Orm/Xtensive.Orm.Tests.Core/Caching/FastConcurrentLruCacheTest.cs

Lines changed: 58 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (C) 2021 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
15
using System;
26
using System.Threading;
37
using System.Threading.Tasks;
@@ -7,143 +11,117 @@
711
using Xtensive.Core;
812
using Xtensive.Orm.Tests;
913

10-
#pragma warning disable IDE0058
11-
1214
namespace Xtensive.Orm.Tests.Core.Caching
1315
{
1416
[TestFixture]
1517
public class FastConcurrentLruCacheTest
1618
{
17-
private FastConcurrentLruCache<string, TestClass> globalCache;
18-
private readonly Random random = RandomManager.CreateRandom((int) DateTime.Now.Ticks);
19-
20-
private class BadTestClass :
21-
IIdentified<string>,
22-
IHasSize
19+
private class BadTestClass : IIdentified<string>, IHasSize
2320
{
24-
object IIdentified.Identifier
25-
{
26-
get { return Identifier; }
27-
}
21+
object IIdentified.Identifier => Identifier;
22+
public string Identifier => null;
23+
public long Size => 1;
24+
}
2825

29-
public string Identifier
30-
{
31-
get { return null; }
32-
}
26+
private const int TestCacheCapacity = 6; // divides by 3
3327

34-
public long Size
35-
{
36-
get { return 1; }
37-
}
38-
}
28+
private FastConcurrentLruCache<string, TestClass> globalCache;
29+
private readonly Random random = RandomManager.CreateRandom((int) DateTime.Now.Ticks);
3930

4031
[Test]
4132
public void ConstructorsTest()
4233
{
43-
var cache = new FastConcurrentLruCache<string, TestClass>(
44-
1000,
45-
value => value.Text);
34+
const int maxCount = 1000;
4635

47-
var cache1 = new FastConcurrentLruCache<string, TestClass>(
48-
1000,
49-
(value) => value.Text
50-
);
36+
var cache = CreateCacheInstance(maxCount);
5137

38+
var cache1 = CreateCacheInstance(maxCount);
5239

53-
TestClass item = new TestClass("1");
40+
var item = new TestClass("1");
5441
cache.Add(item);
5542
cache1.Add(item);
5643
Assert.AreEqual(1, cache1.Count);
5744

58-
for (int i = 0; i < 100000; i++) {
59-
TestClass test = new TestClass("" + i);
60-
cache1.Add(test);
45+
for (var i = 0; i < 100000; i++) {
46+
cache1.Add(new TestClass("" + i));
6147
}
6248
}
6349

64-
[Test]
65-
public void ConstructorDenyTest()
50+
[TestCase(-1)]
51+
[TestCase(0)]
52+
[TestCase(1)]
53+
[TestCase(2)]
54+
public void ConstructorDenyTest(int deniedCapacity)
6655
{
67-
Assert.Throws<ArgumentOutOfRangeException>(() => {
68-
var cache =
69-
new FastConcurrentLruCache<string, TestClass>(
70-
-1,
71-
value => value.Text
72-
);
56+
_ = Assert.Throws<ArgumentOutOfRangeException>(() => {
57+
_ = CreateCacheInstance(deniedCapacity);
7358
});
7459
}
7560

7661
[Test]
7762
public void AddRemoveTest()
7863
{
79-
var cache = new FastConcurrentLruCache<string, TestClass>(
80-
100,
81-
value => value.Text);
64+
var cache = CreateCacheInstance(TestCacheCapacity);
8265

83-
TestClass item = new TestClass("1");
66+
var item = new TestClass("1");
8467
cache.Add(item);
8568
Assert.AreEqual(1, cache.Count);
69+
8670
item = new TestClass("2");
8771
cache.Add(item);
8872
Assert.AreEqual(2, cache.Count);
8973
Assert.AreEqual(item, cache[item.Text, false]);
74+
9075
ICache<string, TestClass> icache = cache;
9176
Assert.AreEqual(item, icache[item.Text, false]);
9277
Assert.AreEqual(null, icache["3", false]);
78+
9379
cache.Remove(item);
9480
Assert.AreEqual(1, cache.Count);
81+
9582
cache.Clear();
9683
Assert.AreEqual(0, cache.Count);
9784
}
9885

9986
[Test]
10087
public void AddDenyTest1()
10188
{
102-
var cache = new FastConcurrentLruCache<string, TestClass>(
103-
100,
104-
value => value.Text);
105-
Assert.Throws<ArgumentNullException>(() => cache.Add(null));
89+
var cache = CreateCacheInstance(TestCacheCapacity);
90+
_ = Assert.Throws<ArgumentNullException>(() => cache.Add(null));
10691
}
10792

10893
[Test]
10994
public void AddDenyTest3()
11095
{
111-
var cache =
112-
new FastConcurrentLruCache<string, BadTestClass>(
113-
100,
96+
var cache = new FastConcurrentLruCache<string, BadTestClass>(
97+
TestCacheCapacity,
11498
value => value.Identifier);
115-
Assert.Throws<ArgumentNullException>(() => cache.Add(new BadTestClass()));
99+
_ = Assert.Throws<ArgumentNullException>(() => cache.Add(new BadTestClass()));
116100
}
117101

118102
[Test]
119103
public void RemoveDenyTest1()
120104
{
121-
var cache =
122-
new FastConcurrentLruCache<string, TestClass>(
123-
100,
124-
value => value.Text);
125-
Assert.Throws<ArgumentNullException>(() => cache.Remove(null));
105+
var cache = CreateCacheInstance(TestCacheCapacity);
106+
_ = Assert.Throws<ArgumentNullException>(() => cache.Remove(null));
126107
}
127108

128109
[Test]
129110
public void RemoveDenyTest2()
130111
{
131-
var cache =
132-
new FastConcurrentLruCache<string, TestClass>(
133-
100,
134-
value => value.Text);
135-
Assert.Throws<ArgumentNullException>(() => cache.RemoveKey(null));
112+
var cache = CreateCacheInstance(TestCacheCapacity);
113+
_ = Assert.Throws<ArgumentNullException>(() => cache.RemoveKey(null));
136114
}
137115

138116
[Test]
139117
public void RemoveDenyTest3()
140118
{
141119
var cache =
142120
new FastConcurrentLruCache<string, BadTestClass>(
143-
100,
121+
TestCacheCapacity,
144122
value => value.Identifier);
145-
BadTestClass test1 = new BadTestClass();
146-
Assert.Throws<ArgumentNullException>(() => cache.Remove(test1));
123+
var test1 = new BadTestClass();
124+
_ = Assert.Throws<ArgumentNullException>(() => cache.Remove(test1));
147125
}
148126

149127
private static readonly bool canFinish = true;
@@ -161,18 +139,18 @@ public void SynchronizationTest()
161139
var removeThreads = new Task[10];
162140
var cancellationTokenSource = new CancellationTokenSource();
163141

164-
for (int i = 0; i < 10; i++) {
142+
for (var i = 0; i < 10; i++) {
165143
addThreads[i] = new Task(() => AddItem(cancellationTokenSource.Token), cancellationTokenSource.Token);
166144
removeThreads[i] = new Task(() => RemoveItem(cancellationTokenSource.Token), cancellationTokenSource.Token);
167145
}
168146

169147
try {
170-
for (int i = 0; i < 10; i++) {
148+
for (var i = 0; i < 10; i++) {
171149
addThreads[i].Start();
172150
}
173151
Thread.Sleep(10);
174152

175-
for (int i = 0; i < 10; i++) {
153+
for (var i = 0; i < 10; i++) {
176154
removeThreads[i].Start();
177155
}
178156
Thread.Sleep(200);
@@ -189,9 +167,7 @@ public void SynchronizationTest()
189167

190168
private void AddItem(CancellationToken cancellationToken)
191169
{
192-
int count = random.Next(100000);
193-
int counter = 0;
194-
bool whileCondition = (counter++) < 10 || !cancellationToken.IsCancellationRequested;
170+
var count = random.Next(100000);
195171
while (!cancellationToken.IsCancellationRequested) {
196172
globalCache.Add(new TestClass("item " + count));
197173
count++;
@@ -201,19 +177,23 @@ private void AddItem(CancellationToken cancellationToken)
201177

202178
private void RemoveItem(CancellationToken cancellationToken)
203179
{
204-
int counter = 0;
205180
while (!cancellationToken.IsCancellationRequested) {
206181
TestClass test = null;
207182
foreach (TestClass testClass in globalCache) {
208183
test = testClass;
209184
break;
210185
}
211-
if (test != null)
186+
if (test != null) {
212187
globalCache.Remove(test);
188+
}
213189
}
214190
cancellationToken.ThrowIfCancellationRequested();
215191
}
216192

193+
private FastConcurrentLruCache<string, TestClass> CreateCacheInstance(int maxCount) =>
194+
new FastConcurrentLruCache<string, TestClass>(maxCount, value => value.Text);
195+
196+
217197
private class ThreadPoolThreadsIncreaser : Disposable
218198
{
219199
private int previousWorkingThreadsCount;
@@ -224,29 +204,21 @@ private class ThreadPoolThreadsIncreaser : Disposable
224204

225205
private void Increase(int workingThreadsCount, int ioThreadsCount)
226206
{
227-
int minWorkingThreads;
228-
int minIOTheads;
229-
ThreadPool.GetMinThreads(out minWorkingThreads, out minIOTheads);
207+
ThreadPool.GetMinThreads(out var minWorkingThreads, out var minIOTheads);
230208
previousWorkingThreadsCount = minWorkingThreads;
231209
previousIOThreadsCount = minIOTheads;
232210

233-
ThreadPool.SetMinThreads(workingThreadsCount, ioThreadsCount);
211+
_ = ThreadPool.SetMinThreads(workingThreadsCount, ioThreadsCount);
234212
}
235213

236214
private static void Decrease(Func<int> workingThreadsCountAcccessor, Func<int> ioThreadsCountAcccessor)
237215
{
238-
ThreadPool.SetMinThreads(workingThreadsCountAcccessor(), ioThreadsCountAcccessor());
216+
_ = ThreadPool.SetMinThreads(workingThreadsCountAcccessor(), ioThreadsCountAcccessor());
239217
}
240218

241-
private int Aa()
242-
{
243-
return previousWorkingThreadsCount;
244-
}
219+
private int Aa() => previousWorkingThreadsCount;
245220

246-
private int Bb()
247-
{
248-
return previousIOThreadsCount;
249-
}
221+
private int Bb() => previousIOThreadsCount;
250222

251223

252224
public ThreadPoolThreadsIncreaser(int workingThreadsCount, int ioThreadsCount)

Orm/Xtensive.Orm.Tests.Core/Caching/LruCacheTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2021 Xtensive LLC.
1+
// Copyright (C) 2009-2021 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44

Orm/Xtensive.Orm/Caching/CacheBase.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2007-2021 Xtensive LLC.
1+
// Copyright (C) 2021 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44

@@ -16,25 +16,25 @@ public abstract class CacheBase<TKey, TItem> : ICache<TKey, TItem>
1616
public virtual Converter<TItem, TKey> KeyExtractor { [DebuggerStepThrough]get; protected set; }
1717

1818
/// <inheritdoc/>
19-
public virtual TItem this[TKey key, bool markAsHit] => TryGetItem(key, markAsHit, out var item) ? item : default;
19+
public abstract int Count { get; }
2020

2121
/// <inheritdoc/>
22-
public abstract int Count { get; }
22+
public virtual TItem this[TKey key, bool markAsHit] => TryGetItem(key, markAsHit, out var item) ? item : default;
2323

2424
/// <inheritdoc/>
25-
public abstract TItem Add(TItem item, bool replaceIfExists);
25+
public abstract bool TryGetItem(TKey key, bool markAsHit, out TItem item);
2626

2727
/// <inheritdoc/>
28-
public virtual void Add(TItem item) => Add(item, true);
28+
public abstract bool ContainsKey(TKey key);
2929

3030
/// <inheritdoc/>
31-
public virtual void Clear() => throw new NotImplementedException();
31+
public abstract TItem Add(TItem item, bool replaceIfExists);
3232

3333
/// <inheritdoc/>
34-
public virtual bool ContainsKey(TKey key) => throw new NotImplementedException();
34+
public virtual void Add(TItem item) => Add(item, true);
3535

3636
/// <inheritdoc/>
37-
public virtual IEnumerator<TItem> GetEnumerator() => throw new NotImplementedException();
37+
public abstract void Clear();
3838

3939
/// <inheritdoc/>
4040
public virtual void Remove(TItem item)
@@ -50,7 +50,7 @@ public virtual void Remove(TItem item)
5050
public abstract void RemoveKey(TKey key, bool removeCompletely);
5151

5252
/// <inheritdoc/>
53-
public abstract bool TryGetItem(TKey key, bool markAsHit, out TItem item);
53+
public abstract IEnumerator<TItem> GetEnumerator();
5454
}
5555
}
5656

0 commit comments

Comments
 (0)