コード例 #1
0
        public void Session_ProgressObservable_IntegrationTests(ProgressMode mode)
        {
            const int ObjectSize      = 1000000;
            const int ObjectsToRecord = 2;

            AsyncContext.Run(async() =>
            {
                var realm            = await SyncTestHelpers.GetIntegrationRealm("progress");
                var completionTCS    = new TaskCompletionSource <ulong>();
                var callbacksInvoked = 0;

                var session    = realm.GetSession();
                var observable = session.GetProgressObservable(ProgressDirection.Upload, mode);

                for (var i = 0; i < ObjectsToRecord; i++)
                {
                    realm.Write(() =>
                    {
                        realm.Add(new HugeSyncObject(ObjectSize));
                    });
                }
                var token = observable.Subscribe(p =>
                {
                    try
                    {
                        callbacksInvoked++;

                        Assert.That(p.TransferredBytes, Is.LessThanOrEqualTo(p.TransferableBytes));

                        if (mode == ProgressMode.ForCurrentlyOutstandingWork)
                        {
                            Assert.That(p.TransferableBytes, Is.GreaterThan(ObjectSize));
                            Assert.That(p.TransferableBytes, Is.LessThan((ObjectsToRecord + 1) * ObjectSize));
                        }
                    }
                    catch (Exception e)
                    {
                        completionTCS.TrySetException(e);
                    }

                    if (p.TransferredBytes == p.TransferableBytes)
                    {
                        completionTCS.TrySetResult(p.TransferredBytes);
                    }
                });

                realm.Write(() =>
                {
                    realm.Add(new HugeSyncObject(ObjectSize));
                });

                var totalTransferred = await completionTCS.Task;

                if (mode == ProgressMode.ForCurrentlyOutstandingWork)
                {
                    Assert.That(totalTransferred, Is.GreaterThanOrEqualTo(ObjectSize));

                    // We add ObjectsToRecord + 1 items, but the last item is added after subscribing
                    // so in the fixed mode, we should not get updates for it.
                    Assert.That(totalTransferred, Is.LessThan((ObjectsToRecord + 1) * ObjectSize));
                }
                else
                {
                    Assert.That(totalTransferred, Is.GreaterThanOrEqualTo((ObjectsToRecord + 1) * ObjectSize));
                }

                Assert.That(callbacksInvoked, Is.GreaterThan(1));

                token.Dispose();
                realm.Dispose();
                Realm.DeleteRealm(realm.Config);
            });
        }