public void CalculateLeasesToTake_OwnLeasesOnly_ReturnsEmpty()
        {
            EqualPartitionsBalancingStrategy   strategy     = this.CreateStrategy();
            IEnumerable <DocumentServiceLease> leasesToTake = strategy.SelectLeasesToTake(new[] { this.CreateLease(ownerSelf, "1"), this.CreateLease(ownerSelf, "2") });

            Assert.IsTrue(leasesToTake.Count() == 0);
        }
Example #2
0
        public void CalculateLeasesToTake_NoLeases_ReturnsEmpty()
        {
            EqualPartitionsBalancingStrategy strategy = CreateStrategy();
            var leasesToTake = strategy.SelectLeasesToTake(Enumerable.Empty <ILease>());

            Assert.Empty(leasesToTake);
        }
        public void CalculateLeasesToTake_NoLeases_ReturnsEmpty()
        {
            EqualPartitionsBalancingStrategy   strategy     = this.CreateStrategy();
            IEnumerable <DocumentServiceLease> leasesToTake = strategy.SelectLeasesToTake(Enumerable.Empty <DocumentServiceLease>());

            Assert.IsTrue(leasesToTake.Count() == 0);
        }
Example #4
0
        public void CalculateLeasesToTake_OwnLeasesOnly_ReturnsEmpty()
        {
            EqualPartitionsBalancingStrategy strategy = CreateStrategy();
            var leasesToTake = strategy.SelectLeasesToTake(new [] { CreateLease(ownerSelf, "1"), CreateLease(ownerSelf, "2") });

            Assert.Empty(leasesToTake);
        }
Example #5
0
        public void CalculateLeasesToTake_ExpiredLeasesOnly_ReturnsAll()
        {
            EqualPartitionsBalancingStrategy strategy = CreateStrategy();
            var allLeases = new HashSet <ILease> {
                CreateExpiredLease(ownerSelf, "1"), CreateExpiredLease(owner1, "2")
            };
            var leasesToTake = strategy.SelectLeasesToTake(allLeases);

            Assert.Equal(allLeases, new HashSet <ILease>(leasesToTake));
        }
Example #6
0
        public void CalculateLeasesToTake_NotOwnedLeasesOnly_ReturnsAll()
        {
            EqualPartitionsBalancingStrategy strategy  = CreateStrategy();
            HashSet <DocumentServiceLease>   allLeases = new HashSet <DocumentServiceLease> {
                CreateLease(ownerNone, "1"), CreateLease(ownerNone, "2")
            };
            IEnumerable <DocumentServiceLease> leasesToTake = strategy.SelectLeasesToTake(allLeases);

            CollectionAssert.AreEqual(allLeases.ToList(), (new HashSet <DocumentServiceLease>(leasesToTake)).ToList());
        }
        public void CalculateLeasesToTake_ExpiredLeasesOnly_ReturnsAll()
        {
            EqualPartitionsBalancingStrategy strategy  = this.CreateStrategy();
            HashSet <DocumentServiceLease>   allLeases = new HashSet <DocumentServiceLease> {
                this.CreateExpiredLease(ownerSelf, "1"), this.CreateExpiredLease(owner1, "2")
            };
            IEnumerable <DocumentServiceLease> leasesToTake = strategy.SelectLeasesToTake(allLeases);

            CollectionAssert.AreEqual(allLeases.ToList(), new HashSet <DocumentServiceLease>(leasesToTake).ToList());
        }
        public void CalculateLeasesToTake_OtherSingleOwnerTwoLeasesOnly_ReturnsOne()
        {
            EqualPartitionsBalancingStrategy strategy  = this.CreateStrategy();
            HashSet <DocumentServiceLease>   allLeases = new HashSet <DocumentServiceLease> {
                this.CreateLease(owner1, "1"), this.CreateLease(owner1, "2")
            };
            List <DocumentServiceLease> leasesToTake = strategy.SelectLeasesToTake(allLeases).ToList();

            Assert.IsTrue(leasesToTake.Count == 1);
            CollectionAssert.IsSubsetOf(new HashSet <DocumentServiceLease>(leasesToTake).ToList(), allLeases.ToList());
        }
        public void CalculateLeasesToTake_NotOwnedLeasesOnly_ReturnsAllWithNoOwnerReason()
        {
            EqualPartitionsBalancingStrategy strategy = CreateStrategy();
            var allLeases = new HashSet <ILease> {
                CreateLease(ownerNone, "1"), CreateLease(ownerNone, "2")
            };
            var leasesToTake = strategy.SelectLeasesToTake(allLeases);

            Assert.Equal(allLeases, new HashSet <ILease>(leasesToTake));
            Assert.All(leasesToTake, l => AssertAcquireReason(l, LeaseAcquireReason.NotOwned));
        }
Example #10
0
        public void CalculateLeasesToTake_AllOtherOwnersEqualTargetCount_ReturnsEmpty()
        {
            EqualPartitionsBalancingStrategy strategy = CreateStrategy();
            var allLeases = new List <ILease>();

            allLeases.AddRange(Enumerable.Range(1, 4).Select(index => CreateLease(owner1, "A" + index.ToString())));
            allLeases.AddRange(Enumerable.Range(1, 3).Select(index => CreateLease(ownerSelf, "B" + index.ToString())));
            var leasesToTake = strategy.SelectLeasesToTake(allLeases).ToList();

            Assert.Empty(leasesToTake);
        }
Example #11
0
        public void CalculateLeasesToTake_OtherSingleOwnerTwoLeasesOnly_ReturnsOne()
        {
            EqualPartitionsBalancingStrategy strategy = CreateStrategy();
            var allLeases = new HashSet <ILease> {
                CreateLease(owner1, "1"), CreateLease(owner1, "2")
            };
            var leasesToTake = strategy.SelectLeasesToTake(allLeases).ToList();

            Assert.Single(leasesToTake);
            Assert.Subset(allLeases, new HashSet <ILease>(leasesToTake));
        }
        public void CalculateLeasesToTake_AllOtherOwnersEqualTargetCount_ReturnsEmpty()
        {
            EqualPartitionsBalancingStrategy strategy  = this.CreateStrategy();
            List <DocumentServiceLease>      allLeases = new List <DocumentServiceLease>();

            allLeases.AddRange(Enumerable.Range(1, 4).Select(index => this.CreateLease(owner1, "A" + index.ToString())));
            allLeases.AddRange(Enumerable.Range(1, 3).Select(index => this.CreateLease(ownerSelf, "B" + index.ToString())));
            List <DocumentServiceLease> leasesToTake = strategy.SelectLeasesToTake(allLeases).ToList();

            Assert.IsTrue(leasesToTake.Count == 0);
        }
        public void CalculateLeasesToTake_MinPartitionsSet_ReturnsMinCountOfPartitions()
        {
            EqualPartitionsBalancingStrategy strategy   = this.CreateStrategy(minPartitionCount: 7);
            List <DocumentServiceLease>      allLeases1 = new List <DocumentServiceLease>();

            allLeases1.Add(this.CreateLease(owner1, "0"));
            allLeases1.AddRange(Enumerable.Range(1, 10).Select(index => this.CreateExpiredLease(owner1, index.ToString())));
            List <DocumentServiceLease> allLeases    = allLeases1;
            List <DocumentServiceLease> leasesToTake = strategy.SelectLeasesToTake(allLeases).ToList();

            Assert.AreEqual(7, leasesToTake.Count);
        }
Example #14
0
        public void CalculateLeasesToTake_NeedTwoAndOtherOwnersEqualThanTargetCount_ReturnsLease()
        {
            EqualPartitionsBalancingStrategy strategy = CreateStrategy();
            var allLeases = new List <ILease>();

            allLeases.AddRange(Enumerable.Range(1, 10).Select(index => CreateLease(owner1, "A" + index.ToString())));
            allLeases.AddRange(Enumerable.Range(1, 10).Select(index => CreateLease(owner2, "B" + index.ToString())));
            allLeases.AddRange(Enumerable.Range(1, 8).Select(index => CreateLease(ownerSelf, "C" + index.ToString())));
            var leasesToTake = strategy.SelectLeasesToTake(allLeases).ToList();

            Assert.Single(leasesToTake);
        }
Example #15
0
        public void CalculateLeasesToTake_OtherOwnerGreaterThanTargetCount_ReturnsLease()
        {
            EqualPartitionsBalancingStrategy strategy = CreateStrategy();
            var allLeases = new List <ILease>();

            allLeases.AddRange(Enumerable.Range(1, 4).Select(index => CreateLease(owner1, "A" + index.ToString())));
            allLeases.AddRange(Enumerable.Range(1, 2).Select(index => CreateLease(ownerSelf, "B" + index.ToString())));
            var    leasesToTake = strategy.SelectLeasesToTake(allLeases).ToList();
            ILease stolenLease  = Assert.Single(leasesToTake);

            Assert.StartsWith("A", stolenLease.PartitionId);
        }
Example #16
0
        public void CalculateLeasesToTake_TwoOwners_ReturnsStolenFromLargerOwner()
        {
            EqualPartitionsBalancingStrategy strategy = CreateStrategy();
            var allLeases = new List <ILease>();

            allLeases.AddRange(Enumerable.Range(1, 5).Select(index => CreateLease(owner1, "A" + index.ToString())));
            allLeases.AddRange(Enumerable.Range(1, 10).Select(index => CreateLease(owner2, "B" + index.ToString())));
            var    leasesToTake = strategy.SelectLeasesToTake(allLeases).ToList();
            ILease stolenLease  = Assert.Single(leasesToTake);

            Assert.StartsWith("B", stolenLease.PartitionId);
        }
Example #17
0
        public void CalculateLeasesToTake_MaxPartitionsSet_ReturnsMaxCountOfPartitions()
        {
            EqualPartitionsBalancingStrategy strategy = CreateStrategy(maxPartitionCount: 3);
            var allLeases1 = new List <ILease>();

            allLeases1.Add(CreateLease(owner1, "0"));
            allLeases1.AddRange(Enumerable.Range(1, 10).Select(index => CreateExpiredLease(owner1, index.ToString())));
            var allLeases    = allLeases1;
            var leasesToTake = strategy.SelectLeasesToTake(allLeases).ToList();

            Assert.Equal(3, leasesToTake.Count);
        }
        public void CalculateLeasesToTake_TwoOwners_ReturnsLeaseWithStealReason()
        {
            EqualPartitionsBalancingStrategy strategy = CreateStrategy();
            var allLeases = new List <ILease>();

            allLeases.AddRange(Enumerable.Range(1, 5).Select(index => CreateLease(owner1, "A" + index.ToString())));
            allLeases.AddRange(Enumerable.Range(1, 10).Select(index => CreateLease(owner2, "B" + index.ToString())));
            var    leasesToTake = strategy.SelectLeasesToTake(allLeases).ToList();
            ILease stolenLease  = Assert.Single(leasesToTake);

            AssertAcquireReason(stolenLease, LeaseAcquireReason.Steal);
        }
        public void CalculateLeasesToTake_TwoOwners_ReturnsStolenFromLargerOwner()
        {
            EqualPartitionsBalancingStrategy strategy  = this.CreateStrategy();
            List <DocumentServiceLease>      allLeases = new List <DocumentServiceLease>();

            allLeases.AddRange(Enumerable.Range(1, 5).Select(index => this.CreateLease(owner1, "A" + index.ToString())));
            allLeases.AddRange(Enumerable.Range(1, 10).Select(index => this.CreateLease(owner2, "B" + index.ToString())));
            List <DocumentServiceLease> leasesToTake = strategy.SelectLeasesToTake(allLeases).ToList();

            Assert.IsTrue(leasesToTake.Count == 1);
            Assert.IsTrue(leasesToTake.First().CurrentLeaseToken.StartsWith("B"));
        }
        public void CalculateLeasesToTake_OtherOwnerGreaterThanTargetCount_ReturnsLease()
        {
            EqualPartitionsBalancingStrategy strategy  = this.CreateStrategy();
            List <DocumentServiceLease>      allLeases = new List <DocumentServiceLease>();

            allLeases.AddRange(Enumerable.Range(1, 4).Select(index => this.CreateLease(owner1, "A" + index.ToString())));
            allLeases.AddRange(Enumerable.Range(1, 2).Select(index => this.CreateLease(ownerSelf, "B" + index.ToString())));
            List <DocumentServiceLease> leasesToTake = strategy.SelectLeasesToTake(allLeases).ToList();

            Assert.IsTrue(leasesToTake.Count == 1);
            Assert.IsTrue(leasesToTake.First().CurrentLeaseToken.StartsWith("A"));
        }
Example #21
0
        public void CalculateLeasesToTake_ExpiredAndOtherSingleOwner_ReturnsHalfOfExpiredRoundedUp()
        {
            EqualPartitionsBalancingStrategy strategy = CreateStrategy();
            var allLeases1 = new List <ILease>();

            allLeases1.Add(CreateLease(owner1, "0"));
            allLeases1.AddRange(Enumerable.Range(1, 10).Select(index => CreateExpiredLease(owner1, index.ToString())));
            var allLeases    = allLeases1;
            var leasesToTake = strategy.SelectLeasesToTake(allLeases).ToList();

            Assert.Equal(6, leasesToTake.Count);
        }
        public void CalculateLeasesToTake_ExpiredAndOtherOwner_ReturnsExpiredOnly()
        {
            EqualPartitionsBalancingStrategy strategy     = this.CreateStrategy();
            DocumentServiceLease             expiredLease = this.CreateExpiredLease(owner1, "4");
            HashSet <DocumentServiceLease>   allLeases    = new HashSet <DocumentServiceLease>
            {
                this.CreateLease(owner1, "1"),
                this.CreateLease(owner1, "2"),
                this.CreateLease(owner1, "3"),
                expiredLease
            };
            List <DocumentServiceLease> leasesToTake = strategy.SelectLeasesToTake(allLeases).ToList();

            Assert.IsTrue(leasesToTake.Count == 1);
            CollectionAssert.Contains(leasesToTake, expiredLease);
        }
Example #23
0
        public void CalculateLeasesToTake_ExpiredAndOtherOwner_ReturnsExpiredOnly()
        {
            EqualPartitionsBalancingStrategy strategy = CreateStrategy();
            ILease expiredLease = CreateExpiredLease(owner1, "4");
            var    allLeases    = new HashSet <ILease>
            {
                CreateLease(owner1, "1"),
                CreateLease(owner1, "2"),
                CreateLease(owner1, "3"),
                expiredLease
            };
            var leasesToTake = strategy.SelectLeasesToTake(allLeases).ToList();

            Assert.Single(leasesToTake);
            Assert.Contains(expiredLease, leasesToTake);
        }