예제 #1
0
        public void MultipleTasksCanMakeChangesConcurrently()
        {
            var diffSet = new DiffSet <int>(Enumerable.Range(0, 100));

            var diffPair = diffSet.GetDiff();

            Assert.Equal(100, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);
            Assert.True(diffPair.Reset);

            var tasks = new[] {
                Task.Factory.StartNew(() =>
                {
                    for (int i = 0; i < 50; i++)
                    {
                        Assert.True(diffSet.Remove(i));
                    }
                }),
                Task.Factory.StartNew(() =>
                {
                    for (int i = 100; i < 150; i++)
                    {
                        Assert.True(diffSet.Add(i));
                    }
                })
            };

            Task.WaitAll(tasks);

            Assert.Equal(100, diffSet.GetSnapshot().Count);
            for (int i = 50; i < 150; i++)
            {
                Assert.True(diffSet.Contains(i));
            }

            diffPair = diffSet.GetDiff();

            Assert.Equal(50, diffPair.Added.Count);
            Assert.Equal(50, diffPair.Removed.Count);
            Assert.False(diffPair.Reset);
            for (int i = 0; i < 50; i++)
            {
                Assert.False(diffSet.Contains(i));
                Assert.False(diffPair.Added.Contains(i));
                Assert.True(diffPair.Removed.Contains(i));
            }
            for (int i = 50; i < 100; i++)
            {
                Assert.True(diffSet.Contains(i));
                Assert.False(diffPair.Added.Contains(i));
                Assert.False(diffPair.Removed.Contains(i));
            }
            for (int i = 100; i < 50; i++)
            {
                Assert.True(diffSet.Contains(i));
                Assert.True(diffPair.Added.Contains(i));
                Assert.False(diffPair.Removed.Contains(i));
            }
        }
예제 #2
0
        public void AddingAndRemovingSameItemMultipleTimesShowsUpOnceInTheDiff()
        {
            var diffSet = new DiffSet <int>(Enumerable.Range(0, 100));

            Assert.False(diffSet.Add(0));     // no-op
            Assert.True(diffSet.Remove(99));
            Assert.False(diffSet.Remove(99)); // no-op

            Assert.Equal(99, diffSet.GetSnapshot().Count);

            var diffPair = diffSet.GetDiff();

            Assert.Equal(99, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);
            Assert.True(diffPair.Reset);
            for (int i = 0; i < 99; i++)
            {
                Assert.True(diffPair.Added.Contains(i));
                Assert.True(diffSet.Contains(i));
            }

            Assert.False(diffSet.Add(1));      // no-op
            Assert.True(diffSet.Add(99));
            Assert.False(diffSet.Add(99));     // no-op
            Assert.False(diffSet.Remove(101)); // no-op
            Assert.True(diffSet.Remove(0));
            Assert.False(diffSet.Remove(0));   // no-op


            Assert.Equal(99, diffSet.GetSnapshot().Count);

            diffPair = diffSet.GetDiff();
            Assert.Equal(1, diffPair.Added.Count);
            Assert.Equal(1, diffPair.Removed.Count);
            Assert.False(diffPair.Reset);
            Assert.True(diffPair.Added.Contains(99));
            Assert.True(diffPair.Removed.Contains(0));
            for (int i = 1; i < 100; i++)
            {
                Assert.True(diffSet.Contains(i));
            }
        }
예제 #3
0
        public void AddingAndRemovingSameItemDoesNotShowUpInDiff()
        {
            var diffSet = new DiffSet <int>(Enumerable.Range(0, 100));

            Assert.True(diffSet.Add(100));
            Assert.True(diffSet.Remove(98));
            Assert.True(diffSet.Remove(99));
            Assert.True(diffSet.Remove(100));
            Assert.True(diffSet.Add(99));
            Assert.True(diffSet.Add(98));

            Assert.Equal(100, diffSet.GetSnapshot().Count);

            var diffPair = diffSet.GetDiff();

            Assert.Equal(100, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);
            Assert.True(diffPair.Reset);
            for (int i = 0; i < 100; i++)
            {
                Assert.True(diffPair.Added.Contains(i));
                Assert.True(diffSet.Contains(i));
            }

            Assert.True(diffSet.Add(150));
            Assert.True(diffSet.Add(200));
            Assert.True(diffSet.Remove(50));
            Assert.True(diffSet.Remove(200));
            Assert.True(diffSet.Remove(150));
            Assert.True(diffSet.Add(50));

            Assert.Equal(100, diffSet.GetSnapshot().Count);

            diffPair = diffSet.GetDiff();
            Assert.Equal(0, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);
            Assert.False(diffPair.Reset);
            for (int i = 0; i < 100; i++)
            {
                Assert.True(diffSet.Contains(i));
            }
        }
예제 #4
0
        public void AddingAndRemovingSameItemDoesNotShowUpInDiff()
        {
            // (0-100)
            var diffSet = new DiffSet <int>(Enumerable.Range(0, 100));

            Assert.True(diffSet.Add(100));
            Assert.True(diffSet.Remove(98));
            Assert.True(diffSet.Remove(99));
            Assert.True(diffSet.Remove(100));
            Assert.True(diffSet.Add(99));
            Assert.True(diffSet.Add(98));

            Assert.Equal(100, diffSet.GetSnapshot().Count);

            // (0-100)
            var changes = diffSet.DetectChanges();

            Assert.False(changes);

            for (int i = 0; i < 100; i++)
            {
                Assert.True(diffSet.Contains(i));
            }

            Assert.True(diffSet.Add(150));
            Assert.True(diffSet.Add(200));
            Assert.True(diffSet.Remove(50));
            Assert.True(diffSet.Remove(200));
            Assert.True(diffSet.Remove(150));
            Assert.True(diffSet.Add(50));

            Assert.Equal(100, diffSet.GetSnapshot().Count);

            changes = diffSet.DetectChanges();
            Assert.False(changes);

            for (int i = 0; i < 100; i++)
            {
                Assert.True(diffSet.Contains(i));
            }
        }
예제 #5
0
        public void AddingAndRemovingSameItemDoesNotShowUpInDiff()
        {
            var diffSet = new DiffSet<int>(Enumerable.Range(0, 100));

            Assert.True(diffSet.Add(100));
            Assert.True(diffSet.Remove(98));
            Assert.True(diffSet.Remove(99));
            Assert.True(diffSet.Remove(100));
            Assert.True(diffSet.Add(99));
            Assert.True(diffSet.Add(98));

            Assert.Equal(100, diffSet.GetSnapshot().Count);

            var diffPair = diffSet.GetDiff();
            Assert.Equal(100, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);
            Assert.True(diffPair.Reset);
            for (int i = 0; i < 100; i++)
            {
                Assert.True(diffPair.Added.Contains(i));
                Assert.True(diffSet.Contains(i));
            }

            Assert.True(diffSet.Add(150));
            Assert.True(diffSet.Add(200));
            Assert.True(diffSet.Remove(50));
            Assert.True(diffSet.Remove(200));
            Assert.True(diffSet.Remove(150));
            Assert.True(diffSet.Add(50));

            Assert.Equal(100, diffSet.GetSnapshot().Count);

            diffPair = diffSet.GetDiff();
            Assert.Equal(0, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);
            Assert.False(diffPair.Reset);
            for (int i = 0; i < 100; i++)
            {
                Assert.True(diffSet.Contains(i));
            }
        }
예제 #6
0
        public void AddingAndRemovingSameItemMultipleTimesShowsUpOnceInTheDiff()
        {
            var diffSet = new DiffSet<int>(Enumerable.Range(0, 100));

            Assert.False(diffSet.Add(0)); // no-op
            Assert.True(diffSet.Remove(99));
            Assert.False(diffSet.Remove(99)); // no-op

            Assert.Equal(99, diffSet.GetSnapshot().Count);

            var diffPair = diffSet.GetDiff();
            Assert.Equal(99, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);

            for (int i = 0; i < 99; i++)
            {
                Assert.True(diffPair.Added.Contains(i));
                Assert.True(diffSet.Contains(i));
            }

            Assert.False(diffSet.Add(1)); // no-op
            Assert.True(diffSet.Add(99));
            Assert.False(diffSet.Add(99)); // no-op
            Assert.False(diffSet.Remove(101)); // no-op
            Assert.True(diffSet.Remove(0));
            Assert.False(diffSet.Remove(0)); // no-op

            Assert.Equal(99, diffSet.GetSnapshot().Count);

            diffPair = diffSet.GetDiff();
            Assert.Equal(1, diffPair.Added.Count);
            Assert.Equal(1, diffPair.Removed.Count);

            Assert.True(diffPair.Added.Contains(99));
            Assert.True(diffPair.Removed.Contains(0));
            for (int i = 1; i < 100; i++)
            {
                Assert.True(diffSet.Contains(i));
            }
        }
예제 #7
0
        public void DetectChangesReturnsFalseNotChangedAgain()
        {
            var diffSet = new DiffSet<int>(new[] { 1 });
            diffSet.Remove(1);
            diffSet.Add(1);

            var result = diffSet.DetectChanges();
            var items = diffSet.GetSnapshot().ToList();

            Assert.False(result);
            Assert.Equal(1, items[0]);
            Assert.Equal(1, items.Count);
        }
예제 #8
0
        public void DetectChangesReturnsTrueIfNoneToSome()
        {
            var diffSet = new DiffSet<int>(Enumerable.Empty<int>());
            diffSet.Add(1);
            diffSet.Remove(1);
            diffSet.Add(5);

            var result = diffSet.DetectChanges();
            var items = diffSet.GetSnapshot().ToList();

            Assert.True(result);
            Assert.Equal(5, items[0]);
        }
예제 #9
0
        public void DetectChangesReturnsTrueIfNoneToSome()
        {
            var diffSet = new DiffSet <int>(Enumerable.Empty <int>());

            diffSet.Add(1);
            diffSet.Remove(1);
            diffSet.Add(5);

            var result = diffSet.DetectChanges();
            var items  = diffSet.GetSnapshot().ToList();

            Assert.True(result);
            Assert.Equal(5, items[0]);
        }
예제 #10
0
        public void DetectChangesReturnsFalseNotChangedAgain()
        {
            var diffSet = new DiffSet <int>(new[] { 1 });

            diffSet.Remove(1);
            diffSet.Add(1);

            var result = diffSet.DetectChanges();
            var items  = diffSet.GetSnapshot().ToList();

            Assert.False(result);
            Assert.Equal(1, items[0]);
            Assert.Equal(1, items.Count);
        }
예제 #11
0
        public void InitializeDiffSetWithChangingIEnumerable()
        {
            var diffSet = new DiffSet <int>(new NonResettingEnumerator().Ints(5));

            Assert.Equal(5, diffSet.GetSnapshot().Count);
            for (int i = 0; i < 5; i++)
            {
                Assert.True(diffSet.Contains(i));
            }

            var diffPair = diffSet.GetDiff();

            Assert.Equal(5, diffSet.GetSnapshot().Count);
            Assert.Equal(5, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);
            Assert.True(diffPair.Reset);
            for (int i = 0; i < 5; i++)
            {
                Assert.True(diffSet.Contains(i));
                Assert.True(diffPair.Added.Contains(i));
                Assert.False(diffPair.Removed.Contains(i));
            }
        }
예제 #12
0
        public void InitialValueCombineWithChangesInFirstDiff()
        {
            var diffSet = new DiffSet <int>(Enumerable.Range(0, 100));

            Assert.True(diffSet.Add(-1));
            Assert.True(diffSet.Add(100));
            Assert.True(diffSet.Remove(0));
            Assert.True(diffSet.Remove(-1));

            Assert.Equal(100, diffSet.GetSnapshot().Count);

            var changes = diffSet.DetectChanges();
            var items   = diffSet.GetSnapshot();

            Assert.True(changes);
            Assert.Equal(100, items.Count);
            Assert.False(diffSet.Contains(0));

            for (int i = 1; i <= 100; i++)
            {
                Assert.True(diffSet.Contains(i));
            }
        }
예제 #13
0
        public void DiffPairMatchesSnapshotAfterConcurrentChanges()
        {
            var    random    = new Random();
            var    diffSet   = new DiffSet <int>(Enumerable.Range(0, 5));
            var    localSet  = new HashSet <int>(Enumerable.Range(0, 5));
            Action updateSet = () =>
            {
                for (int i = 0; i < 100; i++)
                {
                    if (random.Next(2) == 1)
                    {
                        diffSet.Remove(random.Next(5));
                    }
                    else
                    {
                        diffSet.Add(random.Next(5));
                    }
                }
            };

            // Flush initial changes
            var diffPair = diffSet.GetDiff();

            Assert.Equal(5, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);
            Assert.True(diffPair.Reset);

            for (int i = 0; i < 10; i++)
            {
                Task.WaitAll(Enumerable.Repeat(updateSet, 10).Select(Task.Factory.StartNew).ToArray());
                var snapShot = diffSet.GetSnapshot();
                diffPair = diffSet.GetDiff();
                Assert.False(diffPair.Reset);
                Assert.Equal(0, diffPair.Added.Intersect(diffPair.Removed).Count());
                foreach (var addedItem in diffPair.Added)
                {
                    Assert.True(localSet.Add(addedItem));
                }
                foreach (var removedItem in diffPair.Removed)
                {
                    Assert.True(localSet.Remove(removedItem));
                }
                int numSharedItems = localSet.Intersect(snapShot).Count();
                Assert.Equal(numSharedItems, snapShot.Count);
                Assert.Equal(numSharedItems, localSet.Count);
            }
        }
예제 #14
0
        public void DetectChangesReturnsTrueIfItemsNotAlreadyInSet()
        {
            var diffSet = new DiffSet<int>(new[] { 0, 10, 20 });
            diffSet.Add(50);
            diffSet.Add(10);
            diffSet.Add(30);
            diffSet.Remove(10);

            var result = diffSet.DetectChanges();
            var list = diffSet.GetSnapshot().OrderBy(i => i).ToList();

            Assert.True(result);
            Assert.Equal(4, list.Count);
            Assert.Equal(0, list[0]);
            Assert.Equal(20, list[1]);
            Assert.Equal(30, list[2]);
            Assert.Equal(50, list[3]);
        }
예제 #15
0
        public void DetectChangesReturnsTrueIfItemsNotAlreadyInSet()
        {
            var diffSet = new DiffSet <int>(new[] { 0, 10, 20 });

            diffSet.Add(50);
            diffSet.Add(10);
            diffSet.Add(30);
            diffSet.Remove(10);

            var result = diffSet.DetectChanges();
            var list   = diffSet.GetSnapshot().OrderBy(i => i).ToList();

            Assert.True(result);
            Assert.Equal(4, list.Count);
            Assert.Equal(0, list[0]);
            Assert.Equal(20, list[1]);
            Assert.Equal(30, list[2]);
            Assert.Equal(50, list[3]);
        }
예제 #16
0
        public void InitialValueCombineWithChangesInFirstDiff()
        {
            var diffSet = new DiffSet <int>(Enumerable.Range(0, 100));

            Assert.True(diffSet.Add(-1));
            Assert.True(diffSet.Add(100));
            Assert.True(diffSet.Remove(0));
            Assert.True(diffSet.Remove(-1));

            Assert.Equal(100, diffSet.GetSnapshot().Count);

            var diffPair = diffSet.GetDiff();

            Assert.Equal(100, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);
            Assert.True(diffPair.Reset);
            for (int i = 1; i <= 100; i++)
            {
                Assert.True(diffPair.Added.Contains(i));
                Assert.True(diffSet.Contains(i));
            }
        }
예제 #17
0
        internal static void PopulateResponseState(PersistentResponse response, 
                                                   DiffSet<string> groupSet, 
                                                   IJsonSerializer serializer, 
                                                   IProtectedData protectedData)
        {
            DiffPair<string> groupDiff = groupSet.GetDiff();

            if (groupDiff.AnyChanges)
            {
                // Create a protected payload of the sorted list
                IEnumerable<string> groups = groupSet.GetSnapshot();

                // No groups so do nothing
                if (groups.Any())
                {
                    // Remove group prefixes before any thing goes over the wire
                    string groupsString = serializer.Stringify(PrefixHelper.RemoveGroupPrefixes(groups));

                    // The groups token
                    response.GroupsToken = protectedData.Protect(groupsString, Purposes.Groups);
                }
            }
        }
예제 #18
0
        public void InitialValueCombineWithChangesInFirstDiff()
        {
            var diffSet = new DiffSet<int>(Enumerable.Range(0, 100));
            Assert.True(diffSet.Add(-1));
            Assert.True(diffSet.Add(100));
            Assert.True(diffSet.Remove(0));
            Assert.True(diffSet.Remove(-1));

            Assert.Equal(100, diffSet.GetSnapshot().Count);

            var changes = diffSet.DetectChanges();
            var items = diffSet.GetSnapshot();
            Assert.True(changes);
            Assert.Equal(100, items.Count);
            Assert.False(diffSet.Contains(0));

            for (int i = 1; i <= 100; i++)
            {
                Assert.True(diffSet.Contains(i));
            }
        }
예제 #19
0
        public void DiffPairMatchesSnapshotAfterConcurrentChanges()
        {
            var random = new Random();
            var diffSet = new DiffSet<int>(Enumerable.Range(0, 5));
            var localSet = new HashSet<int>(Enumerable.Range(0, 5));
            Action updateSet = () =>
            {
                for (int i = 0; i < 100; i++)
                {
                    if (random.Next(2) == 1)
                    {
                        diffSet.Remove(random.Next(5));
                    }
                    else
                    {
                        diffSet.Add(random.Next(5));
                    }
                }
            };

            // Flush initial changes
            var diffPair = diffSet.GetDiff();
            Assert.Equal(5, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);
            Assert.True(diffPair.Reset);

            for (int i = 0; i < 10; i++)
            {
                Task.WaitAll(Enumerable.Repeat(updateSet, 10).Select(Task.Factory.StartNew).ToArray());
                var snapShot = diffSet.GetSnapshot();
                diffPair = diffSet.GetDiff();
                Assert.False(diffPair.Reset);
                Assert.Equal(0, diffPair.Added.Intersect(diffPair.Removed).Count());
                foreach (var addedItem in diffPair.Added)
                {
                    Assert.True(localSet.Add(addedItem));
                }
                foreach (var removedItem in diffPair.Removed)
                {
                    Assert.True(localSet.Remove(removedItem));
                }
                int numSharedItems = localSet.Intersect(snapShot).Count();
                Assert.Equal(numSharedItems, snapShot.Count);
                Assert.Equal(numSharedItems, localSet.Count);
            }
        }
예제 #20
0
        public void AddingAndRemovingSameItemDoesNotShowUpInDiff()
        {
            // (0-100)
            var diffSet = new DiffSet<int>(Enumerable.Range(0, 100));

            Assert.True(diffSet.Add(100));
            Assert.True(diffSet.Remove(98));
            Assert.True(diffSet.Remove(99));
            Assert.True(diffSet.Remove(100));
            Assert.True(diffSet.Add(99));
            Assert.True(diffSet.Add(98));

            Assert.Equal(100, diffSet.GetSnapshot().Count);

            // (0-100)
            var changes = diffSet.DetectChanges();
            Assert.False(changes);

            for (int i = 0; i < 100; i++)
            {
                Assert.True(diffSet.Contains(i));
            }

            Assert.True(diffSet.Add(150));
            Assert.True(diffSet.Add(200));
            Assert.True(diffSet.Remove(50));
            Assert.True(diffSet.Remove(200));
            Assert.True(diffSet.Remove(150));
            Assert.True(diffSet.Add(50));

            Assert.Equal(100, diffSet.GetSnapshot().Count);

            changes = diffSet.DetectChanges();
            Assert.False(changes);

            for (int i = 0; i < 100; i++)
            {
                Assert.True(diffSet.Contains(i));
            }
        }
예제 #21
0
        internal static void PopulateResponseState(PersistentResponse response,
                                                   DiffSet<string> groupSet,
                                                   IJsonSerializer serializer,
                                                   IProtectedData protectedData,
                                                   string connectionId)
        {
            bool anyChanges = groupSet.DetectChanges();

            if (anyChanges)
            {
                // Create a protected payload of the sorted list
                IEnumerable<string> groups = groupSet.GetSnapshot();

                // Remove group prefixes before any thing goes over the wire
                string groupsString = connectionId + ':' + serializer.Stringify(PrefixHelper.RemoveGroupPrefixes(groups)); ;

                // The groups token
                response.GroupsToken = protectedData.Protect(groupsString, Purposes.Groups);
            }
        }
예제 #22
0
        public void MultipleTasksCanMakeChangesConcurrently()
        {
            var diffSet = new DiffSet<int>(Enumerable.Range(0, 100));

            var diffPair = diffSet.GetDiff();

            Assert.Equal(100, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);
            Assert.True(diffPair.Reset);

            var tasks = new[] {
                Task.Factory.StartNew(() =>
                {
                    for (int i = 0; i < 50; i++)
                    {
                        Assert.True(diffSet.Remove(i));
                    }
                }),
                Task.Factory.StartNew(() =>
                {
                    for (int i = 100; i < 150; i++)
                    {
                        Assert.True(diffSet.Add(i));
                    }
                })
            };

            Task.WaitAll(tasks);

            Assert.Equal(100, diffSet.GetSnapshot().Count);
            for (int i = 50; i < 150; i++)
            {
                Assert.True(diffSet.Contains(i));
            }

            diffPair = diffSet.GetDiff();

            Assert.Equal(50, diffPair.Added.Count);
            Assert.Equal(50, diffPair.Removed.Count);
            Assert.False(diffPair.Reset);
            for (int i = 0; i < 50; i++)
            {
                Assert.False(diffSet.Contains(i));
                Assert.False(diffPair.Added.Contains(i));
                Assert.True(diffPair.Removed.Contains(i));
            }
            for (int i = 50; i < 100; i++)
            {
                Assert.True(diffSet.Contains(i));
                Assert.False(diffPair.Added.Contains(i));
                Assert.False(diffPair.Removed.Contains(i));
            }
            for (int i = 100; i < 50; i++)
            {
                Assert.True(diffSet.Contains(i));
                Assert.True(diffPair.Added.Contains(i));
                Assert.False(diffPair.Removed.Contains(i));
            }
        }
예제 #23
0
        public void InitialValueCombineWithChangesInFirstDiff()
        {
            var diffSet = new DiffSet<int>(Enumerable.Range(0, 100));
            Assert.True(diffSet.Add(-1));
            Assert.True(diffSet.Add(100));
            Assert.True(diffSet.Remove(0));
            Assert.True(diffSet.Remove(-1));

            Assert.Equal(100, diffSet.GetSnapshot().Count);

            var diffPair = diffSet.GetDiff();
            Assert.Equal(100, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);
            Assert.True(diffPair.Reset);
            for (int i = 1; i <= 100; i++)
            {
                Assert.True(diffPair.Added.Contains(i));
                Assert.True(diffSet.Contains(i));
            }
        }
예제 #24
0
        public void InitializeDiffSetWithChangingIEnumerable()
        {
            var diffSet = new DiffSet<int>(new NonResettingEnumerator().Ints(5));

            Assert.Equal(5, diffSet.GetSnapshot().Count);
            for (int i = 0; i < 5; i++ )
            {
                Assert.True(diffSet.Contains(i));
            }

            var diffPair = diffSet.GetDiff();

            Assert.Equal(5, diffSet.GetSnapshot().Count);
            Assert.Equal(5, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);
            Assert.True(diffPair.Reset);
            for (int i = 0; i < 5; i++)
            {
                Assert.True(diffSet.Contains(i));
                Assert.True(diffPair.Added.Contains(i));
                Assert.False(diffPair.Removed.Contains(i));
            }
        }