public void Create_NoLeaseExists_LeaseCreated(
     IConcurrencyControlRepository<ElasticLease> repo,
     ElasticLease lease,
     ISystemContext ctx,
     TestIndexUtils contactIndex)
 {
     using (contactIndex)
     {
         var actual = repo.Create(lease, ctx);
         actual.Should().BeTrue();
     }
 }
 public void Create_LeaseAlreadyExists_FalseReturned(
     IConcurrencyControlRepository<ElasticLease> repo,
     ElasticLease lease,
     ISystemContext ctx,
     TestIndexUtils contactIndex)
 {
     using (contactIndex)
     {
         repo.Create(lease, ctx);
         var actual = repo.Create(lease, ctx);
         actual.Should().BeFalse();
     }
 }
 public void Extend_LeaseExistsVersionNotChanged_LeaseExtended(
     IConcurrencyControlRepository<ElasticLease> repo,
     ElasticLease lease,
     ISystemContext ctx,
     TestIndexUtils contactIndex)
 {
     using (contactIndex)
     {
         repo.Create(lease, ctx);
         var extendedLease = new ElasticLease(
             lease.ResourceId,
             lease.Expires + TimeSpan.FromHours(2),
             lease.Owner,
             lease.Version);
         var actual = repo.SaveOptimistically(extendedLease, ctx);
         actual.Should().BeTrue();
     }
 }
        public void Extend_InactiveLeaseExistsThenLeaseGrabbedBySameOwner_LeaseNotExtended(
            IConcurrencyControlRepository<ElasticLease> repo,
            ElasticLease lease,
            ISystemContext ctx,
            TestIndexUtils contactIndex,
            IFixture fixture)
        {
            using (contactIndex)
            {
                // expired lease exists with owner1.
                repo.Create(lease, ctx);

                // simulate owner1 (in a different thread/process I guess) taking the lease...
                var changedLease = new ElasticLease(
                    lease.ResourceId,
                    DateTime.UtcNow + TimeSpan.FromHours(5),
                    lease.Owner,
                    lease.Version);
                repo.SaveOptimistically(changedLease, ctx); // version will get incremented

                // now owner 1 tries to extend what it thinks is it's lease, this should eventually be allowed...
                var actual = repo.SaveOptimistically(lease, ctx);
                actual.Should().BeFalse();
            }
        }
        public void Extend_InactiveLeaseExistsButLeaseGrabbedByDifferentOwner_LeaseNotExtended(
            IConcurrencyControlRepository<ElasticLease> repo,
            ElasticLease lease,
            ISystemContext ctx,
            TestIndexUtils contactIndex,
            IFixture fixture)
        {
            using (contactIndex)
            {
                // expired lease exists with owner1.
                repo.Create(lease, ctx);

                // simulate owner2 taking the lease...
                var changedLease = new ElasticLease(
                    lease.ResourceId,
                    DateTime.UtcNow + TimeSpan.FromHours(5),
                    fixture.Create<ElasticLeaseOwner>(),
                    lease.Version);
                repo.SaveOptimistically(changedLease, ctx);

                // now owner 1 tries to extend what it thinks is it's lease...
                var actual = repo.SaveOptimistically(lease, ctx);
                actual.Should().BeFalse();
            }
        }
 public void Delete_LeaseExists_LeaseDeleted(
     IConcurrencyControlRepository<ElasticLease> repo,
     ElasticLease lease,
     ISystemContext ctx,
     TestIndexUtils contactIndex)
 {
     using (contactIndex)
     {
         repo.Create(lease, ctx);
         repo.Delete(lease.ResourceId, ctx);
         repo.Get(lease.ResourceId, ctx).Should().BeNull();
     }
 }
 public void Get_LeaseAlreadyExists_LeaseIsReturned(
     IConcurrencyControlRepository<ElasticLease> repo,
     ElasticLease lease,
     ISystemContext ctx,
     TestIndexUtils contactIndex)
 {
     using (contactIndex)
     {
         repo.Create(lease, ctx);
         var actual = repo.Get(lease.ResourceId, ctx);
         actual.Should().NotBeNull();
         actual.ShouldBeEquivalentTo(lease);
     }
 }