protected async Task <Realm> GetRealmAsync(RealmConfigurationBase config, bool openAsync = true, CancellationToken cancellationToken = default(CancellationToken)) { Realm result; if (openAsync) { result = await Realm.GetInstanceAsync(config, cancellationToken); } else { result = Realm.GetInstance(config); await SyncTestHelpers.WaitForDownloadAsync(result); } CleanupOnTearDown(result); return(result); }
public void WhenObjectHasPK_ShouldNotCreateDuplicates(Type objectType, object pkValue, Func <dynamic, bool> pkValueChecker) { SyncTestHelpers.RunRosTestAsync(async() => { var pkProperty = objectType.GetProperties(BindingFlags.Instance | BindingFlags.Public) .Single(p => p.GetCustomAttribute <PrimaryKeyAttribute>() != null); for (var i = 0; i < 5; i++) { var instance = (RealmObject)Activator.CreateInstance(objectType); pkProperty.SetValue(instance, (dynamic)pkValue); using (var realm = await GetSyncedRealm(objectType)) { try { realm.Write(() => realm.Add(instance)); } catch (RealmDuplicatePrimaryKeyValueException) { // Sync went through too quickly (that's why we do 5 attempts) } await SyncTestHelpers.WaitForUploadAsync(realm); } } using (var realm = await GetSyncedRealm(objectType)) { await SyncTestHelpers.WaitForDownloadAsync(realm); var allObjects = realm.All(objectType.Name).ToArray(); Assert.That(allObjects.Count(pkValueChecker), Is.EqualTo(1)); } }); }