public async Task BasicTest() { IDbStore columnFamilyDbStore = this.rocksDbStoreProvider.GetColumnStoreFamily("test"); string key = "key1"; string value = "value1"; byte[] keyBytes = key.ToBytes(); byte[] valueBytes = value.ToBytes(); await columnFamilyDbStore.Put(keyBytes, valueBytes); Option <byte[]> returnedValueBytesOption = await columnFamilyDbStore.Get(keyBytes); Assert.True(returnedValueBytesOption.HasValue); byte[] returnedValueBytes = returnedValueBytesOption.OrDefault(); Assert.True(valueBytes.SequenceEqual(returnedValueBytes)); Assert.Equal(value, returnedValueBytes.FromBytes()); Assert.True(await columnFamilyDbStore.Contains(keyBytes)); Assert.False(await columnFamilyDbStore.Contains("key2".ToBytes())); await columnFamilyDbStore.Remove(keyBytes); returnedValueBytesOption = await columnFamilyDbStore.Get(keyBytes); Assert.False(returnedValueBytesOption.HasValue); }
public async Task MessageCountTest() { using (IDbStore columnFamilyDbStore = this.rocksDbStoreProvider.GetDbStore("test")) { Assert.Equal(0ul, await columnFamilyDbStore.Count()); for (int i = 0; i < 10; i++) { string key = $"key{i}"; string value = "$value{i}"; await columnFamilyDbStore.Put(key.ToBytes(), value.ToBytes()); } Assert.Equal(10ul, await columnFamilyDbStore.Count()); } using (IDbStore columnFamilyDbStore = this.rocksDbStoreProvider.GetDbStore("test")) { Assert.Equal(10ul, await columnFamilyDbStore.Count()); for (int i = 0; i < 10; i++) { string key = $"key{i}"; await columnFamilyDbStore.Remove(key.ToBytes()); } Assert.Equal(0ul, await columnFamilyDbStore.Count()); } }
public async Task SpaceCheckViolationsTest() { Option <long> maxStorageBytes = Option.Some(90L); IStorageSpaceChecker checker = new MemorySpaceChecker(() => 0L); checker.SetMaxSizeBytes(maxStorageBytes); InMemoryDbStoreProvider storeProvider = new InMemoryDbStoreProvider(Option.Some(checker)); string store1Name = "store1"; IDbStore store1 = storeProvider.GetDbStore(store1Name); string store2Name = "store2"; IDbStore store2 = storeProvider.GetDbStore(store2Name); byte[] message1 = new byte[10]; byte[] message2 = new byte[20]; await store1.Put(message1, message1); await store2.Put(message2, message2); // Current sizes - // store1 -> (message1 * 2) // store2 -> (message2 * 2) // Aggregated size is less than limit, adding another item should succeed. byte[] message3 = new byte[30]; await store1.Put(message3, message3, CancellationToken.None); // Current sizes - // store1 -> (message1 * 2) + (message3 * 2) // store2 -> (message2 * 2) // Aggregated size is greater than limit, adding another item should fail. byte[] message4 = new byte[40]; await Assert.ThrowsAsync <StorageFullException>(() => store2.Put(message4, message4)); await Assert.ThrowsAsync <StorageFullException>(() => store2.Put(message4, message4, CancellationToken.None)); // Remove store2. The usage of store1 alone should be less than the max limit. // Current sizes - // store1 -> (message1 * 2) + (message3 * 2) // store2 -> X // Aggregated size is less than limit, adding another item should succeed. storeProvider.RemoveDbStore(store2Name); await store1.Put(message4, message4); // Current sizes - // store1 -> (message1 * 2) + (message3 * 2) + (message4 * 2) // store2 -> X // Aggregated size is greater than limit, adding another item should fail. byte[] message5 = new byte[45]; await Assert.ThrowsAsync <StorageFullException>(() => store1.Put(message5, message5)); // Re-add store2. store2 = storeProvider.GetDbStore(store2Name); await store1.Remove(message4); // Current sizes - // store1 -> (message1 * 2) + (message3 * 2) // store2 -> X // Aggregated size is less than limit, adding another item should succeed. await store2.Put(message1, message1); // Current sizes - // store1 -> (message1 * 2) + (message3 * 2) // store2 -> (message1 * 2) // Aggregated size is greater than limit, adding another item should fail. await Assert.ThrowsAsync <StorageFullException>(() => store2.Put(message5, message5)); // Remove an item from store1 and then try adding a smaller item to store2 which should succeed. await store1.Remove(message3, CancellationToken.None); // Current sizes - // store1 -> (message1 * 2) // store2 -> (message1 * 2) // Aggregated size is less than limit, adding another item should succeed. await store2.Put(message3, message3, CancellationToken.None); // Current sizes - // store1 -> (message1 * 2) // store2 -> (message1 * 2) + (message3 * 2) // Aggregated size is greater than limit, adding another item should fail. await Assert.ThrowsAsync <StorageFullException>(() => store2.Put(message4, message4)); // Set max storage size to be greater than the current db size. Option <long> newMaxStorageBytes = Option.Some((message1.Length * 2) * 2L + (message3.Length * 2) + 10); checker.SetMaxSizeBytes(newMaxStorageBytes); // Adding another item should now succeed after the limits have been increased. await store1.Put(message4, message4); // Adding a new item to store1 should fail. // Current sizes - // store1 -> (message1 * 2) + (message4 * 2) // store2 -> (message1 * 2) + (message3 * 2) // Aggregated size is greater than limit, adding another item should fail. await Assert.ThrowsAsync <StorageFullException>(() => store1.Put(message5, message5)); // Updating the message4 item in store1 should succeed as the size difference between // the existing and updated value is zero bytes which doesn't lead to the limit being breached. await store1.Put(message4, message4); }