Skip to content

Commit 393458b

Browse files
authored
soak lfu add + fix detached nodes (#432)
* soak lfu * background * getoradd * rename * soak lfu add * merge * handle removed ---------
1 parent 60e78bf commit 393458b

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuSoakTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,29 @@ await Threaded.RunAsync(4, async () => {
108108
scheduler.Dispose();
109109
await scheduler.Completion;
110110

111+
RunIntegrityCheck(lfu);
112+
}
113+
114+
[Theory]
115+
[Repeat(iterations)]
116+
public async Task WhenConcurrentGetAndUpdateCacheEndsInConsistentState(int iteration)
117+
{
118+
var scheduler = new BackgroundThreadScheduler();
119+
var lfu = new ConcurrentLfuBuilder<int, string>().WithCapacity(9).WithScheduler(scheduler).Build() as ConcurrentLfu<int, string>;
120+
121+
await Threaded.Run(4, () => {
122+
for (int i = 0; i < 100000; i++)
123+
{
124+
lfu.TryUpdate(i + 1, i.ToString());
125+
lfu.GetOrAdd(i + 1, i => i.ToString());
126+
}
127+
});
128+
129+
this.output.WriteLine($"iteration {iteration} keys={string.Join(" ", lfu.Keys)}");
130+
131+
scheduler.Dispose();
132+
await scheduler.Completion;
133+
111134
RunIntegrityCheck(lfu);
112135
}
113136

BitFaster.Caching/Lfu/ConcurrentLfu.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,13 @@ private void OnWrite(LfuNode<K, V> node)
691691
}
692692

693693
private void PromoteProbation(LfuNode<K, V> node)
694-
{
694+
{
695+
if (node.list == null)
696+
{
697+
// Ignore stale accesses for an entry that is no longer present
698+
return;
699+
}
700+
695701
this.probationLru.Remove(node);
696702
this.protectedLru.AddLast(node);
697703
node.Position = Position.Protected;

0 commit comments

Comments
 (0)