public void RefreshCausesReplace()
        {
            // Arrange
            var sourceCache = new SourceCache <Item, Guid>(item => item.Id);

            var item1 = new Item("Old Name");

            sourceCache.AddOrUpdate(item1);

            var collection = new TestBindingList <Item>();

            var sourceCacheResults = sourceCache.Connect().AutoRefresh(item => item.Name).Bind(collection).AsAggregator();

            var collectionResults = collection.ToObservableChangeSet().AsAggregator();

            item1.Name = "New Name";

            // Source cache received add and refresh
            sourceCacheResults.Messages.Count.Should().Be(2);
            sourceCacheResults.Messages.First().Adds.Should().Be(1);
            sourceCacheResults.Messages.Last().Refreshes.Should().Be(1);

            // List receives add and replace instead of refresh
            collectionResults.Messages.Count.Should().Be(2);
            collectionResults.Messages.First().Adds.Should().Be(1);
            collectionResults.Messages.First().Refreshes.Should().Be(0);
            collectionResults.Messages.Last().Replaced.Should().Be(1);
            collectionResults.Messages.Last().Refreshes.Should().Be(0);

            sourceCache.Dispose();
            sourceCacheResults.Dispose();
            collectionResults.Dispose();
        }
        public void RefreshNotSupported()
        {
            // Arrange
            var sourceCache = new SourceCache <Item, Guid>(item => item.Id);

            var item1 = new Item(name: "Old Name");

            sourceCache.AddOrUpdate(item1);

            var sourceCacheResults = sourceCache
                                     .Connect()
                                     .AutoRefresh(item => item.Name)
                                     .Bind(out var collection)
                                     .AsAggregator();

            var collectionResults = collection.ToObservableChangeSet().AsAggregator();

            item1.Name = "New Name";

            // Source cache received add and refresh
            sourceCacheResults.Messages.Count.Should().Be(2);
            sourceCacheResults.Messages.First().Adds.Should().Be(1);
            sourceCacheResults.Messages.Last().Refreshes.Should().Be(1);

            // Collection only receives add and NOT refresh
            // System.Collections.Specialized.NotifyCollectionChangedAction does not have an enum to describe the same item being refreshed.
            // https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.notifycollectionchangedaction
            collectionResults.Messages.Count.Should().Be(1);
            collectionResults.Messages.First().Adds.Should().Be(1);

            sourceCache.Dispose();
            sourceCacheResults.Dispose();
            collectionResults.Dispose();
        }
Exemplo n.º 3
0
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         _internalSourceCache?.Dispose();
     }
 }
        public void InitialBindWithExistingData()
        {
            var source = new SourceCache <Person, string>(p => p.Name);

            // Populate source before binding
            var person1 = new Person("Adult1", 20);
            var person2 = new Person("Adult2", 30);

            source.AddOrUpdate(person2); // Add out of order to assert intial order
            source.AddOrUpdate(person1);

            var sourceCacheNotifications = source.Connect().AutoRefresh().Sort(_comparer, resetThreshold: 10).BindToObservableList(out var list).AsAggregator();

            var listNotifications = list.Connect().AsAggregator();

            // Assert
            listNotifications.Messages.Count().Should().Be(1);
            listNotifications.Messages.First().First().Reason.Should().Be(ListChangeReason.AddRange);
            list.Items.Should().Equal(person1, person2);

            // Clean up
            source.Dispose();
            sourceCacheNotifications.Dispose();
            listNotifications.Dispose();
            list.Dispose();
        }
Exemplo n.º 5
0
        protected override Task DestroyInternal()
        {
            _cancel.Cancel();

            _observeCacheState.Dispose();

            _state.OnCompleted();
            _state.Dispose();

            _sourceCache.Dispose();

            _cacheUpdateSocket.Close();
            _cacheUpdateSocket.Dispose();

            return(Task.CompletedTask);
        }
Exemplo n.º 6
0
        protected async override Task DestroyInternal()
        {
            _cancel.Cancel();

            _cleanup.Dispose();

            _state.OnCompleted();
            _state.Dispose();

            _sourceCache.Dispose();

            _cacheUpdatePoller.Stop();
            _heartbeatPoller.Stop();

            await WaitForWorkProceduresToComplete(_workProc, _heartbeatProc, _caughtUpWithStateOfTheWorldProc);
        }
Exemplo n.º 7
0
        public void CachePerformance(int n)
        {

            /*
                Tricks to make it fast = 

                1) use cache.AddRange(Enumerable.Range(0, n))
                instead of  for (int i = 0; i < n; i++) cache.AddOrUpdate(i);

                2)  Uncomment Buffer(n/10).FlattenBufferResult()
                or just use buffer by time functions

                With both of these the speed can be almost negligable

            */
            var cache = new SourceCache<int, int>(i => i);
            double calculated = 0;

            var sw = Stopwatch.StartNew();

            var summation = cache.Connect()
                .StdDev(i => i)
                .Subscribe(result => calculated = result);


            //1. this is very slow if there are loads of updates (each updates causes a new summation)
            for (int i = 1; i < n; i++)
                cache.AddOrUpdate(i);

            //2. much faster to to this (whole range is 1 update and 1 calculation):
            //  cache.AddOrUpdate(Enumerable.Range(0,n));

            sw.Stop();

            summation.Dispose();
            cache.Dispose();

            Console.WriteLine("Total items: {0}. Value = {1}", n, calculated);
            Console.WriteLine("Cache: {0} updates took {1} ms {2:F3} ms each. {3}", n, sw.ElapsedMilliseconds, sw.Elapsed.TotalMilliseconds / n, DateTime.Now.ToShortDateString());

        }
Exemplo n.º 8
0
        public void CachePerformance(int n)
        {
            /*
             *  Tricks to make it fast =
             *
             *  1) use cache.AddRange(Enumerable.Range(0, n))
             *  instead of  for (int i = 0; i < n; i++) cache.AddOrUpdate(i);
             *
             *  2)  Uncomment Buffer(n/10).FlattenBufferResult()
             *  or just use buffer by time functions
             *
             *  With both of these the speed can be almost negligable
             *
             */
            var    cache      = new SourceCache <int, int>(i => i);
            double runningSum = 0;

            var sw = Stopwatch.StartNew();

            var summation = cache.Connect()
                            .Minimum(i => i)
                            .Subscribe(result => runningSum = result);


            //1. this is very slow if there are loads of updates (each updates causes a new summation)
            for (int i = 1; i < n; i++)
            {
                cache.AddOrUpdate(i);
            }

            //2. much faster to to this (whole range is 1 update and 1 calculation):
            //  cache.AddOrUpdate(Enumerable.Range(0,n));

            sw.Stop();

            summation.Dispose();
            cache.Dispose();

            Console.WriteLine("Total items: {0}. Sum = {1}", n, runningSum);
            Console.WriteLine("Cache Summation: {0} updates took {1} ms {2:F3} ms each. {3}", n, sw.ElapsedMilliseconds, sw.Elapsed.TotalMilliseconds / n, DateTime.Now.ToShortDateString());
        }
Exemplo n.º 9
0
 public void Cleanup()
 {
     _source.Dispose();
 }
Exemplo n.º 10
0
 public void Dispose()
 {
     _people.Dispose();
     _result.Dispose();
 }
Exemplo n.º 11
0
 public void Dispose()
 {
     _connectionSoureCache.Dispose();
 }
Exemplo n.º 12
0
 public void CleanUp()
 {
     _people.Dispose();
     _result.Dispose();
 }
Exemplo n.º 13
0
 public void Dispose()
 {
     _source.Dispose();
     _results.Dispose();
 }
Exemplo n.º 14
0
 public void Dispose()
 {
     _source.Dispose();
 }
 public void Cleanup()
 {
     _source.Dispose();
     _results.Dispose();
 }
Exemplo n.º 16
0
 public void Dispose()
 {
     _cache.Dispose();
     _result.Dispose();
 }
Exemplo n.º 17
0
 public void Dispose()
 {
     _cache.Dispose();
     _cleanup.Dispose();
 }
Exemplo n.º 18
0
 public void Dispose()
 {
     _cache.Dispose();
     _subscription.Dispose();
 }
Exemplo n.º 19
0
 public void OnTestCompleted()
 {
     _cache.Dispose();
     _result.Dispose();
 }