Skip to content

Commit 1a69acf

Browse files
authored
Lru OnRemove locks value instead of item (#442)
* handle null * lfu test ---------
1 parent 4d52e6f commit 1a69acf

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,16 @@ public void WhenRemovedInWriteBuffer()
672672
public void WhenItemDoesNotExistTryUpdateIsFalse()
673673
{
674674
cache.TryUpdate(1, 2).Should().BeFalse();
675+
}
676+
677+
[Fact]
678+
public void WhenAddingNullValueCanBeAddedAndRemoved()
679+
{
680+
// use foreground so that any null ref exceptions will surface
681+
var lfu = new ConcurrentLfu<int, string>(1, 20, new ForegroundScheduler(), EqualityComparer<int>.Default);
682+
lfu.GetOrAdd(1, _ => null).Should().BeNull();
683+
lfu.AddOrUpdate(1, null);
684+
lfu.TryRemove(1).Should().BeTrue();
675685
}
676686

677687
[Fact]

BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,13 @@ public void WhenValueExpiresItIsDisposed()
574574
disposableValueFactory.Items[6].IsDisposed.Should().BeFalse();
575575
}
576576

577+
[Fact]
578+
public void WhenAddingNullValueCanBeAddedAndRemoved()
579+
{
580+
lru.GetOrAdd(1, _ => null).Should().BeNull();
581+
lru.AddOrUpdate(1, null);
582+
lru.TryRemove(1).Should().BeTrue();
583+
}
577584

578585
[Fact]
579586
public void WhenValueEvictedItemRemovedEventIsFired()

BitFaster.Caching/Lru/ConcurrentLruCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ private void OnRemove(K key, I item)
359359
this.telemetryPolicy.OnItemRemoved(key, item.Value, ItemRemovedReason.Removed);
360360

361361
// serialize dispose (common case dispose not thread safe)
362-
lock (item.Value)
362+
lock (item)
363363
{
364364
Disposer<V>.Dispose(item.Value);
365365
}

0 commit comments

Comments
 (0)