Пример #1
0
        public void WriteAsync_ShouldReturnUnmanagedQueryable()
        {
            var pko1 = new IntPrimaryKeyWithValueObject
            {
                Id          = 1,
                StringValue = "bla"
            };

            var pko2 = new IntPrimaryKeyWithValueObject
            {
                Id          = 2,
                StringValue = "ble"
            };

            var queryable = new IntPrimaryKeyWithValueObject[] { pko1, pko2 }.AsQueryable();

            TestHelpers.RunAsyncTest(async() =>
            {
                var queryableResult = await _realm.WriteAsync(realm =>
                {
                    return(queryable);
                });

                Assert.That(queryable, Is.EqualTo(queryableResult));
            });
        }
Пример #2
0
        public void RefreshAsync_Tests()
        {
            TestHelpers.RunAsyncTest(async() =>
            {
                Assert.That(SynchronizationContext.Current != null);

                IntPrimaryKeyWithValueObject obj = null;
                _realm.Write(() =>
                {
                    obj = _realm.Add(new IntPrimaryKeyWithValueObject());
                });

                var reference = ThreadSafeReference.Create(obj);

                Task.Run(() =>
                {
                    using var realm = GetRealm(_realm.Config);
                    var bgObj       = realm.ResolveReference(reference);
                    realm.Write(() =>
                    {
                        bgObj.StringValue = "123";
                    });
                }).Wait(); // <- wait to avoid the main thread autoupdating while idle

                Assert.That(obj.StringValue, Is.Null);

                var changeTiming = await MeasureTiming(_realm.RefreshAsync);

                Assert.That(obj.StringValue, Is.EqualTo("123"));

                // Make sure when there are no changes RefreshAsync completes quickly
                var idleTiming = await MeasureTiming(_realm.RefreshAsync);

                Assert.That(changeTiming, Is.GreaterThan(idleTiming));
        public void AddAnObject_WhenRealmIsDifferentInstanceOnSameThread_ShouldSucceed()
        {
            var firstObject = new IntPrimaryKeyWithValueObject();

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

            using var realm2 = GetRealm(_realm.Config);
            Assert.That(firstObject.IsManaged);
            Assert.That(realm2.IsSameInstance(firstObject.Realm));
            realm2.Write(() => realm2.Add(firstObject));
        }
Пример #4
0
        public void RefreshAsync_Tests()
        {
            TestHelpers.RunAsyncTest(async() =>
            {
                Assert.That(SynchronizationContext.Current != null);

                IntPrimaryKeyWithValueObject obj = null;
                _realm.Write(() =>
                {
                    obj = _realm.Add(new IntPrimaryKeyWithValueObject());
                });

                var reference = ThreadSafeReference.Create(obj);

                Task.Run(() =>
                {
                    using (var realm = Realm.GetInstance(_realm.Config))
                    {
                        var bgObj = realm.ResolveReference(reference);
                        realm.Write(() =>
                        {
                            bgObj.StringValue = "123";
                        });
                    }
                }).Wait(); // <- wait to avoid the main thread autoupdating while idle

                Assert.That(obj.StringValue, Is.Null);

                var changeTiming = await measureTiming(_realm.RefreshAsync);

                Assert.That(obj.StringValue, Is.EqualTo("123"));

                // Make sure when there are no changes RefreshAsync completes quickly
                var idleTiming = await measureTiming(_realm.RefreshAsync);

                Assert.That(changeTiming, Is.GreaterThan(idleTiming));

                async Task <long> measureTiming(Func <Task> func)
                {
                    var sw = new Stopwatch();
                    sw.Start();
                    await func();
                    sw.Stop();
                    return(sw.ElapsedTicks);
                }
            });
        }
Пример #5
0
        public void WriteAsync_ShouldReturnUnmanagedRealmObject()
        {
            TestHelpers.RunAsyncTest(async() =>
            {
                var pko = new IntPrimaryKeyWithValueObject
                {
                    Id          = 1,
                    StringValue = "bla"
                };

                var writeResult = await _realm.WriteAsync(realm =>
                {
                    return(pko);
                });

                Assert.That(writeResult, Is.EqualTo(pko));
            });
        }
Пример #6
0
        public void ModifiedIndices_ReportCorrectlyForOldAndNewVersions()
        {
            ChangeSet changes = null;

            void cb(IRealmCollection <IntPrimaryKeyWithValueObject> s, ChangeSet c, Exception e) => changes = c;

            var toDelete = new IntPrimaryKeyWithValueObject {
                Id = 1
            };
            var toModify = new IntPrimaryKeyWithValueObject {
                Id = 2
            };

            _realm.Write(() =>
            {
                _realm.Add(toDelete);
                _realm.Add(toModify);
            });

            var query = _realm.All <IntPrimaryKeyWithValueObject>().OrderBy(i => i.Id);

            using (query.SubscribeForNotifications(cb))
            {
                Assert.That(query.ElementAt(0).Equals(toDelete));
                Assert.That(query.ElementAt(1).Equals(toModify));

                _realm.Write(() =>
                {
                    _realm.Remove(toDelete);
                    toModify.StringValue = "newValue";
                });

                _realm.Refresh();
                Assert.That(changes, Is.Not.Null);
                Assert.That(changes.DeletedIndices, Is.EquivalentTo(new int[] { 0 }));

                // Modified should be in the old collection
                Assert.That(changes.ModifiedIndices, Is.EquivalentTo(new int[] { 1 }));

                // NewModified should be in the new collection that is just 1 element
                Assert.That(changes.NewModifiedIndices, Is.EquivalentTo(new int[] { 0 }));
                Assert.That(query.ElementAt(changes.NewModifiedIndices[0]).Equals(toModify));
            }
        }
Пример #7
0
        public void AsyncWrite_UpdateViaPrimaryKey()
        {
            TestHelpers.RunAsyncTest(async() =>
            {
                IntPrimaryKeyWithValueObject obj = null;
                _realm.Write(() =>
                {
                    obj = _realm.Add(new IntPrimaryKeyWithValueObject {
                        Id = 123
                    });
                });

                await _realm.WriteAsync(realm =>
                {
                    var dataObj         = realm.Find <IntPrimaryKeyWithValueObject>(123);
                    dataObj.StringValue = "foobar";
                });

                // Make sure the changes are immediately visible on the caller thread
                Assert.That(obj.StringValue, Is.EqualTo("foobar"));
            });
        }