Beispiel #1
0
        public async Task UpdateAsyncUsesIdentityComparerGivenAtConstruction()
        {
            IEnumerable <IntWrapper> originalItems         = new IntWrapper[] { 1, 2, 3 };
            IEnumerable <ItemChange <IntWrapper> > changes = new ItemChange <IntWrapper>[]
            {
                new ItemChange <IntWrapper>(ChangeType.Updated, 2)
            };

            var loader = new AsyncLoader <IntWrapper>(
                Seq.ListBased,
                loadDataAsync: _ => Task.FromResult(originalItems),
                fetchUpdatesAsync: (_, __) => Task.FromResult(changes),
                identityComparer: new IntWrapperComparer(),
                eventContext: new RunInlineSynchronizationContext());
            await loader.LoadAsync();  // load original items

            loader.CollectionChanged += (s, e) =>
            {
                // Verify that the resulting change was exactly the incoming change
                e.Should().Equal(changes);
            };

            await loader.UpdateAsync();  // --- Perform ---

            // Verify that no changes to the int values in the collection were made
            loader.Should().Equal(new IntWrapper[] { 1, 2, 3 }, (x, y) => x.Value == y.Value);
        }
Beispiel #2
0
        public async Task UpdateAsyncDoesNotAddOrRemoveItemsForUpdateItemChange()
        {
            IEnumerable <int> originalItems         = new int[] { 1 };
            IEnumerable <ItemChange <int> > changes = new ItemChange <int>[] { new ItemChange <int>(ChangeType.Updated, 1) };

            var loader = new AsyncLoader <int>(
                Seq.ListBased,
                loadDataAsync: tok => Task.FromResult(originalItems),
                fetchUpdatesAsync: (items, tok) => Task.FromResult(changes));
            await loader.LoadAsync();

            loader.Should().Equal(new[] { 1 }); // sanity check

            await loader.UpdateAsync();         // --- Perform ---

            loader.Should().Equal(new[] { 1 });
        }
Beispiel #3
0
        public async Task UpdateAsyncCanRemoveSingleItemFromLoader()
        {
            IEnumerable <int> originalItems         = new int[] { 1 };
            IEnumerable <ItemChange <int> > changes = new ItemChange <int>[] { new ItemChange <int>(ChangeType.Removed, 1) };

            var loader = new AsyncLoader <int>(
                Seq.ListBased,
                loadDataAsync: tok => Task.FromResult(originalItems),
                fetchUpdatesAsync: (items, tok) => Task.FromResult(changes));
            await loader.LoadAsync();

            loader.Should().NotBeEmpty(); // sanity check

            await loader.UpdateAsync();   // --- Perform ---

            loader.Should().BeEmpty();
        }
Beispiel #4
0
        public void LoadAsyncClearsPreviousContents()
        {
            IEnumerable <int> originalItems = new[] { 1, 2, 3 };
            IEnumerable <int> loadedItems   = new[] { 4, 5, 6 };

            var loadFunc = Substitute.For <Func <CancellationToken, Task <IEnumerable <int> > > >();

            loadFunc.Invoke(Arg.Any <CancellationToken>()).Returns(Task.FromResult(originalItems), Task.FromResult(loadedItems));

            var loader = new AsyncLoader <int>(seqFactory: Seq.ListBased, loadDataAsync: loadFunc);

            loader.LoadAsync();                            // initial load
            loader.Should().BeEquivalentTo(originalItems); // sanity check


            loader.LoadAsync();  // --- Perform ---
            loader.Should().BeEquivalentTo(loadedItems);
        }
Beispiel #5
0
        public async Task CanUpdateEmptyLoaderWithEmptyChanges()
        {
            var loader = new AsyncLoader <int>(
                seqFactory: Seq.ListBased,
                fetchUpdatesAsync: (_, __) => Task.FromResult(Enumerable.Empty <ItemChange <int> >()));

            await loader.UpdateAsync();  // --- Perform ---

            loader.Should().BeEmpty();
        }
Beispiel #6
0
        public async Task LoadAsyncPreservesItemsAddedDuringLoad()
        {
            IEnumerable <int> itemsToLoad = new[] { 2, 3, 4 };
            var loadTask = new TaskCompletionSource <IEnumerable <int> >();
            var loader   = new AsyncLoader <int>(seqFactory: Seq.ListBased, loadDataAsync: tok => loadTask.Task);


            // --- Perform ---
            var loadComplete = loader.LoadAsync();  // Will get stuck, waiting for loadTask to complete

            loader.Conj(1);


            loader.Should().BeEquivalentTo(new[] { 1 }); // sanity check
            loadTask.SetResult(itemsToLoad);             // complete loading
            await loadComplete;                          // wait for LoadAsync to finish

            loader.Should().BeEquivalentTo(new[] { 1, 2, 3, 4 });
        }
Beispiel #7
0
        public async Task UpdateAsyncRetainsOrder()
        {
            IEnumerable <int> originalItems         = new int[] { 1, 2, 3 };
            IEnumerable <ItemChange <int> > changes = new ItemChange <int>[]
            {
                new ItemChange <int>(ChangeType.Updated, 2),
                new ItemChange <int>(ChangeType.Added, 4)
            };

            var loader = new AsyncLoader <int>(
                Seq.ListBased,
                loadDataAsync: tok => Task.FromResult(originalItems),
                fetchUpdatesAsync: (items, tok) => Task.FromResult(changes));
            await loader.LoadAsync();   // load original items

            await loader.UpdateAsync(); // --- Perform ---

            loader.Should().Equal(new[] { 1, 2, 3, 4 });
        }