Skip to content

Commit

Permalink
make it more clear
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim8y committed Jul 8, 2024
1 parent 6d045bc commit 38a915c
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/Neo.UnitTests/Persistence/UT_MemoryClonedCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@

namespace Neo.UnitTests.Persistence;

/// <summary>
/// When adding data to `datacache` <see cref="DataCache"/>,
/// it gets passed to `snapshotcache` <see cref="SnapshotCache"/> during commit.
/// If `snapshotcache` <see cref="SnapshotCache"/>commits, the data is then passed
/// to the underlying store <see cref="IStore"/>.
/// However, because snapshots <see cref="ISnapshot"/> are immutable, the new data
/// cannot be retrieved from the snapshot <see cref="ISnapshot"/>.
///
/// When deleting data from `datacache` <see cref="DataCache"/>,
/// it won't exist in `datacache` upon commit, and therefore will be removed from `snapshotcache` <see cref="SnapshotCache"/>.
/// Upon `snapshotcache` <see cref="SnapshotCache"/>commit, the data is deleted from the store <see cref="IStore"/>.
/// However, since the snapshot <see cref="ISnapshot"/> remains unchanged, the data still exists in the snapshot.
/// If you attempt to read this data from `datacache` <see cref="DataCache"/> or `snapshotcache` <see cref="SnapshotCache"/>,
/// which do not have the data, they will retrieve it from the snapshot instead of the store.
/// Thus, they can still access data that has been deleted.
/// </summary>
[TestClass]
public class UT_MemoryClonedCache
{
Expand Down Expand Up @@ -46,6 +62,7 @@ public void SingleSnapshotCacheTest()
var key1 = new KeyBuilder(0, 1);
var value1 = new StorageItem([0x03, 0x04]);

Assert.IsFalse(_dataCache.Contains(key1));
_dataCache.Add(key1, value1);

Assert.IsTrue(_dataCache.Contains(key1));
Expand Down Expand Up @@ -78,6 +95,7 @@ public void SingleSnapshotCacheTest()
_snapshotCache = new SnapshotCache(_snapshot);
_dataCache = _snapshotCache.CreateSnapshot();

Assert.IsTrue(_dataCache.Contains(key1));
_dataCache.Delete(key1);

Assert.IsFalse(_dataCache.Contains(key1));
Expand All @@ -99,6 +117,8 @@ public void SingleSnapshotCacheTest()
// so its value after the commit is meaningless and should not be used.
_snapshotCache.Commit();

// The reason that datacache, snapshotcache still contains key1 is because
// they can not find the value from its cache, so they fetch it from the snapshot of the store.
Assert.IsTrue(_dataCache.Contains(key1));
Assert.IsTrue(_snapshotCache.Contains(key1));
Assert.IsTrue(_snapshot.Contains(key1.ToArray()));
Expand Down

0 comments on commit 38a915c

Please sign in to comment.