Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Neo Plugin Store] Unit test #3399

Merged
merged 14 commits into from
Jul 11, 2024
7 changes: 4 additions & 3 deletions src/Plugins/LevelDBStore/Plugins/Storage/Snapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public void Dispose()

public IEnumerable<(byte[] Key, byte[] Value)> Seek(byte[] prefix, SeekDirection direction = SeekDirection.Forward)
{
return db.Seek(options, prefix, direction, (k, v) => (k, v));
return db.Seek(options, prefix, direction, (k, v) => (k, v))
shargon marked this conversation as resolved.
Show resolved Hide resolved
?? db.Seek(ReadOptions.Default, prefix, direction, (k, v) => (k, v));
}

public void Put(byte[] key, byte[] value)
Expand All @@ -58,12 +59,12 @@ public void Put(byte[] key, byte[] value)

public bool Contains(byte[] key)
{
return db.Contains(options, key);
return db.Contains(options, key) || db.Contains(ReadOptions.Default, key);
}

public byte[] TryGet(byte[] key)
{
return db.Get(options, key);
return db.Get(options, key) ?? db.Get(ReadOptions.Default, key);
}
}
}
4 changes: 2 additions & 2 deletions src/Plugins/RocksDBStore/Plugins/Storage/Snapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ public void Put(byte[] key, byte[] value)

public bool Contains(byte[] key)
{
return db.Get(key, Array.Empty<byte>(), 0, 0, readOptions: options) >= 0;
return db.Get(key, Array.Empty<byte>(), 0, 0, readOptions: options) >= 0 || db.Get(key, Array.Empty<byte>(), 0, 0) >= 0;
}

public byte[] TryGet(byte[] key)
{
return db.Get(key, readOptions: options);
return db.Get(key, readOptions: options) ?? db.Get(key);
}

public void Dispose()
Expand Down
152 changes: 136 additions & 16 deletions tests/Neo.Plugins.Storage.Tests/StoreTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,21 @@ public class StoreTest
{
private const string path_leveldb = "Data_LevelDB_UT";
private const string path_rocksdb = "Data_RocksDB_UT";
private static LevelDBStore levelDbStore;
private static RocksDBStore rocksDBStore;

[TestInitialize]
public void OnStart()
{
if (Directory.Exists(path_leveldb)) Directory.Delete(path_leveldb, true);
if (Directory.Exists(path_rocksdb)) Directory.Delete(path_rocksdb, true);
levelDbStore ??= new LevelDBStore();
rocksDBStore ??= new RocksDBStore();
ReSetStore();
}

[TestCleanup]
public void OnEnd()
{
ReSetStore();
}

[TestMethod]
Expand All @@ -49,35 +58,140 @@ public void TestMemory()
[TestMethod]
public void TestLevelDb()
{
using var plugin = new LevelDBStore();
TestPersistenceDelete(plugin.GetStore(path_leveldb));
ReSetStore();
Jim8y marked this conversation as resolved.
Show resolved Hide resolved
TestPersistenceDelete(levelDbStore.GetStore(path_leveldb));
// Test all with the same store

TestStorage(plugin.GetStore(path_leveldb));
TestStorage(levelDbStore.GetStore(path_leveldb));

// Test with different storages

TestPersistenceWrite(plugin.GetStore(path_leveldb));
TestPersistenceRead(plugin.GetStore(path_leveldb), true);
TestPersistenceDelete(plugin.GetStore(path_leveldb));
TestPersistenceRead(plugin.GetStore(path_leveldb), false);
TestPersistenceWrite(levelDbStore.GetStore(path_leveldb));
TestPersistenceRead(levelDbStore.GetStore(path_leveldb), true);
TestPersistenceDelete(levelDbStore.GetStore(path_leveldb));
TestPersistenceRead(levelDbStore.GetStore(path_leveldb), false);
}

[TestMethod]
public void TestLevelDbSnapshot()
{
ReSetStore();
using var store = levelDbStore.GetStore(path_leveldb);

var snapshot = store.GetSnapshot();

var testKey = new byte[] { 0x01, 0x02, 0x03 };
var testValue = new byte[] { 0x04, 0x05, 0x06 };

snapshot.Put(testKey, testValue);
// Data saved to the leveldb snapshot shall not be visible to the store
Assert.IsNull(snapshot.TryGet(testKey));

// Value is in the write batch, not visible to the store and snapshot
Assert.AreEqual(false, snapshot.Contains(testKey));
Assert.AreEqual(false, store.Contains(testKey));

snapshot.Commit();

// After commit, the data shall be visible to the store and to the snapshot
CollectionAssert.AreEqual(testValue, snapshot.TryGet(testKey));
CollectionAssert.AreEqual(testValue, store.TryGet(testKey));
Assert.AreEqual(true, snapshot.Contains(testKey));
Assert.AreEqual(true, store.Contains(testKey));

snapshot.Dispose();
}

[TestMethod]
public void TestLevelDbMultiSnapshot()
{
ReSetStore();
using var store = levelDbStore.GetStore(path_leveldb);

var snapshot = store.GetSnapshot();
var snapshot2 = store.GetSnapshot();

var testKey = new byte[] { 0x01, 0x02, 0x03 };
var testValue = new byte[] { 0x04, 0x05, 0x06 };

snapshot.Put(testKey, testValue);
snapshot.Commit();
CollectionAssert.AreEqual(testValue, store.TryGet(testKey));

// Data saved to the leveldb from snapshot1 shall also be visible to snapshot2
CollectionAssert.AreEqual(testValue, snapshot2.TryGet(testKey));

snapshot.Dispose();
snapshot2.Dispose();
}

[TestMethod]
public void TestRocksDb()
{
using var plugin = new RocksDBStore();
TestPersistenceDelete(plugin.GetStore(path_rocksdb));
TestPersistenceDelete(rocksDBStore.GetStore(path_rocksdb));
// Test all with the same store

TestStorage(plugin.GetStore(path_rocksdb));
TestStorage(rocksDBStore.GetStore(path_rocksdb));

// Test with different storages

TestPersistenceWrite(plugin.GetStore(path_rocksdb));
TestPersistenceRead(plugin.GetStore(path_rocksdb), true);
TestPersistenceDelete(plugin.GetStore(path_rocksdb));
TestPersistenceRead(plugin.GetStore(path_rocksdb), false);
TestPersistenceWrite(rocksDBStore.GetStore(path_rocksdb));
TestPersistenceRead(rocksDBStore.GetStore(path_rocksdb), true);
TestPersistenceDelete(rocksDBStore.GetStore(path_rocksdb));
TestPersistenceRead(rocksDBStore.GetStore(path_rocksdb), false);
}

[TestMethod]
public void TestRocksDbSnapshot()
{
ReSetStore();
using var store = rocksDBStore.GetStore(path_leveldb);

var snapshot = store.GetSnapshot();

var testKey = new byte[] { 0x01, 0x02, 0x03 };
var testValue = new byte[] { 0x04, 0x05, 0x06 };

snapshot.Put(testKey, testValue);
// Data saved to the leveldb snapshot shall not be visible to the store
Assert.IsNull(snapshot.TryGet(testKey));

// Value is in the write batch, not visible to the store and snapshot
Assert.AreEqual(false, snapshot.Contains(testKey));
Assert.AreEqual(false, store.Contains(testKey));

snapshot.Commit();

// After commit, the data shall be visible to the store and to the snapshot
CollectionAssert.AreEqual(testValue, snapshot.TryGet(testKey));
CollectionAssert.AreEqual(testValue, store.TryGet(testKey));
Assert.AreEqual(true, snapshot.Contains(testKey));
Assert.AreEqual(true, store.Contains(testKey));

snapshot.Dispose();
}

[TestMethod]
public void TestRocksDbMultiSnapshot()
{
ReSetStore();
using var store = rocksDBStore.GetStore(path_leveldb);

var snapshot = store.GetSnapshot();
var snapshot2 = store.GetSnapshot();

var testKey = new byte[] { 0x01, 0x02, 0x03 };
var testValue = new byte[] { 0x04, 0x05, 0x06 };

snapshot.Put(testKey, testValue);
snapshot.Commit();
CollectionAssert.AreEqual(testValue, store.TryGet(testKey));

// Data saved to the leveldb from snapshot1 shall also be visible to snapshot2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check to see if snapshot2 is visible to snapshot1

CollectionAssert.AreEqual(testValue, snapshot2.TryGet(testKey));

snapshot.Dispose();
snapshot2.Dispose();
}

/// <summary>
Expand Down Expand Up @@ -196,5 +310,11 @@ private void TestPersistenceRead(IStore store, bool shouldExist)
else Assert.IsNull(ret);
}
}

private void ReSetStore()
{
if (Directory.Exists(path_leveldb)) Directory.Delete(path_leveldb, true);
if (Directory.Exists(path_rocksdb)) Directory.Delete(path_rocksdb, true);
}
}
}
Loading