From 38a26ff30483f4de1d7cdd6ad0653e0237d2e8ab Mon Sep 17 00:00:00 2001 From: Badrish Chandramouli Date: Thu, 12 Sep 2019 18:30:01 -0700 Subject: [PATCH] Added tests --- cs/test/AsyncLargeObjectTests.cs | 138 +++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 cs/test/AsyncLargeObjectTests.cs diff --git a/cs/test/AsyncLargeObjectTests.cs b/cs/test/AsyncLargeObjectTests.cs new file mode 100644 index 000000000..b26a0f791 --- /dev/null +++ b/cs/test/AsyncLargeObjectTests.cs @@ -0,0 +1,138 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. + +using System; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using System.Collections.Generic; +using System.Linq; +using FASTER.core; +using System.IO; +using NUnit.Framework; + +namespace FASTER.test.async +{ + + [TestFixture] + internal class LargeObjectTests + { + private FasterKV fht1; + private FasterKV fht2; + private IDevice log, objlog; + private string test_path; + + [SetUp] + public void Setup() + { + if (test_path == null) + { + test_path = TestContext.CurrentContext.TestDirectory + "\\" + Path.GetRandomFileName(); + if (!Directory.Exists(test_path)) + Directory.CreateDirectory(test_path); + } + } + + [TearDown] + public void TearDown() + { + DeleteDirectory(test_path); + } + + public static void DeleteDirectory(string path) + { + foreach (string directory in Directory.GetDirectories(path)) + { + DeleteDirectory(directory); + } + + try + { + Directory.Delete(path, true); + } + catch (IOException) + { + Directory.Delete(path, true); + } + catch (UnauthorizedAccessException) + { + Directory.Delete(path, true); + } + } + + [TestCase(CheckpointType.FoldOver)] + [TestCase(CheckpointType.Snapshot)] + public async Task LargeObjectTest(CheckpointType checkpointType) + { + MyInput input = default(MyInput); + MyLargeOutput output = new MyLargeOutput(); + + log = Devices.CreateLogDevice(test_path + "\\LargeObjectTest.log"); + objlog = Devices.CreateLogDevice(test_path + "\\LargeObjectTest.obj.log"); + + fht1 = new FasterKV + (128, new MyLargeFunctions(), + new LogSettings { LogDevice = log, ObjectLogDevice = objlog, MutableFraction = 0.1, PageSizeBits = 21, MemorySizeBits = 26 }, + new CheckpointSettings { CheckpointDir = test_path, CheckPointType = checkpointType }, + new SerializerSettings { keySerializer = () => new MyKeySerializer(), valueSerializer = () => new MyLargeValueSerializer() } + ); + + int maxSize = 100; + int numOps = 5000; + Guid token; + + using (var s = fht1.StartClientSession()) + { + Random r = new Random(33); + for (int key = 0; key < numOps; key++) + { + var mykey = new MyKey { key = key }; + var value = new MyLargeValue(1 + r.Next(maxSize)); + s.Upsert(ref mykey, ref value, Empty.Default, 0); + } + + fht1.TakeFullCheckpoint(out token); + + await s.CompleteCheckpointAsync(); + } + fht1.Dispose(); + log.Close(); + objlog.Close(); + + log = Devices.CreateLogDevice(test_path + "\\LargeObjectTest.log"); + objlog = Devices.CreateLogDevice(test_path + "\\LargeObjectTest.obj.log"); + + fht2 = new FasterKV + (128, new MyLargeFunctions(), + new LogSettings { LogDevice = log, ObjectLogDevice = objlog, MutableFraction = 0.1, PageSizeBits = 21, MemorySizeBits = 26 }, + new CheckpointSettings { CheckpointDir = test_path, CheckPointType = checkpointType }, + new SerializerSettings { keySerializer = () => new MyKeySerializer(), valueSerializer = () => new MyLargeValueSerializer() } + ); + + fht2.Recover(token); + using (var s2 = fht2.StartClientSession()) + { + for (int keycnt = 0; keycnt < numOps; keycnt++) + { + var key = new MyKey { key = keycnt }; + var status = s2.Read(ref key, ref input, ref output, Empty.Default, 0); + + if (status == Status.PENDING) + await s2.CompletePendingAsync(); + else + { + for (int i = 0; i < output.value.value.Length; i++) + { + Assert.IsTrue(output.value.value[i] == (byte)(output.value.value.Length + i)); + } + } + } + } + + fht2.Dispose(); + + log.Close(); + objlog.Close(); + } + } +}