Skip to content

Commit

Permalink
try #3
Browse files Browse the repository at this point in the history
  • Loading branch information
nirinchev committed Apr 3, 2018
1 parent 604d74e commit 3ecd3c5
Showing 1 changed file with 93 additions and 56 deletions.
149 changes: 93 additions & 56 deletions Tests/Tests.Shared/ListOfPrimitivesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Nito.AsyncEx;
Expand All @@ -27,20 +28,30 @@
namespace Tests.Database
{
[TestFixture, Preserve(AllMembers = true)]
public class ListOfPrimitivesTests : RealmInstanceTest
public class ListOfPrimitivesTests
{
private static readonly Random _random = new Random();
private Lazy<Realm> _lazyRealm;

private ListsObject _managedListsObject;
private Realm _realm => _lazyRealm.Value;

protected override void CustomSetUp()
// We capture the current SynchronizationContext when opening a Realm.
// However, NUnit replaces the SynchronizationContext after the SetUp method and before the async test method.
// That's why we make sure we open the Realm in the test method by accessing it lazily.

[SetUp]
public void SetUp()
{
base.CustomSetUp();
_lazyRealm = new Lazy<Realm>(() => Realm.GetInstance(Path.GetTempFileName()));
}

_realm.Write(() =>
[TearDown]
public void TearDown()
{
if (_lazyRealm.IsValueCreated)
{
_managedListsObject = _realm.Add(new ListsObject());
});
Realm.DeleteRealm(_realm.Config);
}
}

#region TestCaseSources
Expand Down Expand Up @@ -288,176 +299,181 @@ public static IEnumerable<object> ByteArrayTestValues()
[TestCaseSource(nameof(BooleanTestValues))]
public void Test_ManagedBooleanList(bool[] values)
{
RunManagedTests(_managedListsObject.BooleanList, values);
RunManagedTests(obj => obj.BooleanList, values);
}

[TestCaseSource(nameof(ByteTestValues))]
public void Test_ManagedByteCounterList(byte[] values)
{
RunManagedTests(_managedListsObject.ByteCounterList, values.ToInteger());
RunManagedTests(obj => obj.ByteCounterList, values.ToInteger());
}

[TestCaseSource(nameof(ByteTestValues))]
public void Test_ManagedByteList(byte[] values)
{
RunManagedTests(_managedListsObject.ByteList, values);
RunManagedTests(obj => obj.ByteList, values);
}

[TestCaseSource(nameof(CharTestValues))]
public void Test_ManagedCharList(char[] values)
{
RunManagedTests(_managedListsObject.CharList, values);
RunManagedTests(obj => obj.CharList, values);
}

[TestCaseSource(nameof(DoubleTestValues))]
public void Test_ManagedDoubleList(double[] values)
{
RunManagedTests(_managedListsObject.DoubleList, values);
RunManagedTests(obj => obj.DoubleList, values);
}

[TestCaseSource(nameof(Int16TestValues))]
public void Test_ManagedInt16CounterList(short[] values)
{
RunManagedTests(_managedListsObject.Int16CounterList, values.ToInteger());
RunManagedTests(obj => obj.Int16CounterList, values.ToInteger());
}

[TestCaseSource(nameof(Int16TestValues))]
public void Test_ManagedInt16List(short[] values)
{
RunManagedTests(_managedListsObject.Int16List, values);
RunManagedTests(obj => obj.Int16List, values);
}

[TestCaseSource(nameof(Int32TestValues))]
public void Test_ManagedInt32CounterList(int[] values)
{
RunManagedTests(_managedListsObject.Int32CounterList, values.ToInteger());
RunManagedTests(obj => obj.Int32CounterList, values.ToInteger());
}

[TestCaseSource(nameof(Int32TestValues))]
public void Test_ManagedInt32List(int[] values)
{
RunManagedTests(_managedListsObject.Int32List, values);
RunManagedTests(obj => obj.Int32List, values);
}

[TestCaseSource(nameof(Int64TestValues))]
public void Test_ManagedInt64CounterList(long[] values)
{
RunManagedTests(_managedListsObject.Int64CounterList, values.ToInteger());
RunManagedTests(obj => obj.Int64CounterList, values.ToInteger());
}

[TestCaseSource(nameof(Int64TestValues))]
public void Test_ManagedInt64List(long[] values)
{
RunManagedTests(_managedListsObject.Int64List, values);
RunManagedTests(obj => obj.Int64List, values);
}

[TestCaseSource(nameof(DateTestValues))]
public void Test_ManagedDateTimeOffsetList(DateTimeOffset[] values)
{
RunManagedTests(_managedListsObject.DateTimeOffsetList, values);
RunManagedTests(obj => obj.DateTimeOffsetList, values);
}

[TestCaseSource(nameof(NullableBooleanTestValues))]
public void Test_ManagedNullableBooleanList(bool?[] values)
{
RunManagedTests(_managedListsObject.NullableBooleanList, values);
RunManagedTests(obj => obj.NullableBooleanList, values);
}

[TestCaseSource(nameof(NullableByteTestValues))]
public void Test_ManagedNullableByteCounterList(byte?[] values)
{
RunManagedTests(_managedListsObject.NullableByteCounterList, values.ToInteger());
RunManagedTests(obj => obj.NullableByteCounterList, values.ToInteger());
}

[TestCaseSource(nameof(NullableByteTestValues))]
public void Test_ManagedNullableByteList(byte?[] values)
{
RunManagedTests(_managedListsObject.NullableByteList, values);
RunManagedTests(obj => obj.NullableByteList, values);
}

[TestCaseSource(nameof(NullableCharTestValues))]
public void Test_ManagedNullableCharList(char?[] values)
{
RunManagedTests(_managedListsObject.NullableCharList, values);
RunManagedTests(obj => obj.NullableCharList, values);
}

[TestCaseSource(nameof(NullableDoubleTestValues))]
public void Test_ManagedNullableDoubleList(double?[] values)
{
RunManagedTests(_managedListsObject.NullableDoubleList, values);
RunManagedTests(obj => obj.NullableDoubleList, values);
}

[TestCaseSource(nameof(NullableInt16TestValues))]
public void Test_ManagedNullableInt16CounterList(short?[] values)
{
RunManagedTests(_managedListsObject.NullableInt16CounterList, values.ToInteger());
RunManagedTests(obj => obj.NullableInt16CounterList, values.ToInteger());
}

[TestCaseSource(nameof(NullableInt16TestValues))]
public void Test_ManagedNullableInt16List(short?[] values)
{
RunManagedTests(_managedListsObject.NullableInt16List, values);
RunManagedTests(obj => obj.NullableInt16List, values);
}

[TestCaseSource(nameof(NullableInt32TestValues))]
public void Test_ManagedNullableInt32CounterList(int?[] values)
{
RunManagedTests(_managedListsObject.NullableInt32CounterList, values.ToInteger());
RunManagedTests(obj => obj.NullableInt32CounterList, values.ToInteger());
}

[TestCaseSource(nameof(NullableInt32TestValues))]
public void Test_ManagedNullableInt32List(int?[] values)
{
RunManagedTests(_managedListsObject.NullableInt32List, values);
RunManagedTests(obj => obj.NullableInt32List, values);
}

[TestCaseSource(nameof(NullableInt64TestValues))]
public void Test_ManagedNullableInt64CounterList(long?[] values)
{
RunManagedTests(_managedListsObject.NullableInt64CounterList, values.ToInteger());
RunManagedTests(obj => obj.NullableInt64CounterList, values.ToInteger());
}

[TestCaseSource(nameof(NullableInt64TestValues))]
public void Test_ManagedNullableInt64List(long?[] values)
{
RunManagedTests(_managedListsObject.NullableInt64List, values);
RunManagedTests(obj => obj.NullableInt64List, values);
}

[TestCaseSource(nameof(NullableDateTestValues))]
public void Test_ManagedNullableDateTimeOffsetList(DateTimeOffset?[] values)
{
RunManagedTests(_managedListsObject.NullableDateTimeOffsetList, values);
RunManagedTests(obj => obj.NullableDateTimeOffsetList, values);
}

[TestCaseSource(nameof(StringTestValues))]
public void Test_ManagedStringList(string[] values)
{
RunManagedTests(_managedListsObject.StringList, values);
RunManagedTests(obj => obj.StringList, values);
}

[TestCaseSource(nameof(ByteArrayTestValues))]
public void Test_ManagedByteArrayList(byte[][] values)
{
RunManagedTests(_managedListsObject.ByteArrayList, values);
RunManagedTests(obj => obj.ByteArrayList, values);
}

private void RunManagedTests<T>(IList<T> items, T[] toAdd)
private void RunManagedTests<T>(Func<ListsObject, IList<T>> itemsGetter, T[] toAdd)
{
var reference = ThreadSafeReference.Create(items);
AsyncContext.Run(async () =>
Task.Run(() =>
{
await Task.Run(() =>
AsyncContext.Run(async () =>
{
using (var realm = Realm.GetInstance(_realm.Config))
try
{
var backgroundList = realm.ResolveReference(reference);
RunManagedTestsCore(backgroundList, toAdd);
var listObject = new ListsObject();
_realm.Write(() => _realm.Add(listObject));
var items = itemsGetter(listObject);
await RunManagedTestsCore(items, toAdd);
}
}).Timeout(5000);
});
finally
{
_realm.Dispose();
}
});
}).Timeout(105000).Wait();
}

private static void RunManagedTestsCore<T>(IList<T> items, T[] toAdd)
private static async Task RunManagedTestsCore<T>(IList<T> items, T[] toAdd)
{
var realm = (items as RealmList<T>).Realm;

Expand Down Expand Up @@ -515,6 +531,20 @@ private static void RunManagedTestsCore<T>(IList<T> items, T[] toAdd)
Assert.That(items.IndexOf(item), Is.EqualTo(Array.IndexOf(toAdd, item)));
}

// Test threadsafe reference
var reference = ThreadSafeReference.Create(items);
await Task.Run(() =>
{
using (var bgRealm = Realm.GetInstance(realm.Config))
{
var backgroundList = bgRealm.ResolveReference(reference);
for (var i = 0; i < backgroundList.Count; i++)
{
Assert.That(backgroundList[i], Is.EqualTo(toAdd[i]));
}
}
});

if (toAdd.Any())
{
// Test insert
Expand Down Expand Up @@ -808,26 +838,33 @@ public void Test_UnmanagedByteArrayList(byte[][] values)

private void RunUnmanagedTests<T>(Func<ListsObject, IList<T>> accessor, T[] toAdd)
{
if (toAdd == null)
try
{
toAdd = new T[0];
}
if (toAdd == null)
{
toAdd = new T[0];
}

var listsObject = new ListsObject();
var list = accessor(listsObject);
var listsObject = new ListsObject();
var list = accessor(listsObject);

foreach (var item in toAdd)
{
list.Add(item);
}
foreach (var item in toAdd)
{
list.Add(item);
}

CollectionAssert.AreEqual(list, toAdd);
CollectionAssert.AreEqual(list, toAdd);

_realm.Write(() => _realm.Add(listsObject));
_realm.Write(() => _realm.Add(listsObject));

var managedList = accessor(listsObject);
var managedList = accessor(listsObject);

CollectionAssert.AreEqual(managedList, toAdd);
CollectionAssert.AreEqual(managedList, toAdd);
}
finally
{
_realm.Dispose();
}
}

#endregion
Expand Down

0 comments on commit 3ecd3c5

Please sign in to comment.