public void TryLoadContact_ContactExistsAlreadyLockedByDifferentOwner_NotLocked(
     IElasticAnalyticsContactService contactService,
     ElasticLeaseOwner us,
     ElasticContact eContact,
     ISystemContext ctx)
 {
     var res = contactService.TryLoadAndLock(eContact.Id, us, TimeSpan.FromMinutes(1), ctx);
     res.Should().NotBeNull();
     res.LockedObject.Id.Should().Be(eContact.Id);
     res.Status.Should().Be(LockAttemptStatus.AlreadyLocked);
 }
 public void TryLoadContact_ContactDoesntExist_ReturnsNull(
     ElasticLeaseOwner us,
     IElasticAnalyticsContactService contactService,
     ISystemContext ctx)
 {
     var res = contactService.TryLoadAndLock(Guid.NewGuid(), us, TimeSpan.FromMinutes(1), ctx);
     res.LockedObject.Should().BeNull();
     res.Status.Should().Be(LockAttemptStatus.NotFound);
 }
        public void TryLoadAndLockById_ContactExistsAndNotLockedWithNoSuccessors_ReturnsContact(
            IElasticAnalyticsContactService contactService,
            ElasticContact contact,
            ElasticLeaseOwner us,
            ISystemContext ctx,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                contactService.Save(contact, us, true, ctx)
                    .Should().Be(true);

                var res = contactService.TryLoadAndLock(contact.Id, us, TimeSpan.FromMinutes(1), ctx);

                res.Status.Should().Be(LockAttemptStatus.Success);
                res.LockedObject.ShouldBeEquivalentToContact(contact, l => l.Expires > DateTime.UtcNow && l.IsOwnedBy(us) && l.Version == 3);
            }
        }
        public void ReleaseContact_NoLockExistsForContact_ContactReleased(
            IElasticAnalyticsContactService contactService,
            ElasticContact contact,
            ElasticLeaseOwner us,
            ISystemContext ctx,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                // contact exists, no lock
                contactService.Save(contact, us, true, ctx).Should().BeTrue();

                // release
                contactService.Release(contact.Id, us, ctx);

                // should be able to lock it again
                contactService.TryLoadAndLock(contact.Id, us, TimeSpan.FromHours(1), ctx)
                    .Status.Should()
                    .Be(LockAttemptStatus.Success);
            }
        }
        public void SaveContact_ContactDoesntExistShouldNotBeReleased_ContactCreatedWithLock(
            IElasticAnalyticsContactService contactService,
            ElasticContact contact,
            ElasticLeaseOwner us,
            ElasticLeaseOwner them,
            ISystemContext ctx,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                contactService.Save(contact, them, false, ctx)
                    .Should().Be(true);

                var res = contactService.TryLoadAndLock(contact.Id, us, TimeSpan.FromMinutes(1), ctx);

                res.Status.Should().Be(LockAttemptStatus.AlreadyLocked);
                res.LockedObject.ShouldBeEquivalentToContact(contact);

                contactService.LoadForReadOnly(contact.Identification.Identity, ctx)
                    .ShouldBeEquivalentToContact(contact, l => l == null);
            }
        }
        public void SaveContact_ContactExistsWithIdentityNotLocked_ContactUpdated(
            IElasticAnalyticsContactService contactService,
            ElasticContact contact,
            ElasticLeaseOwner us,
            ElasticLeaseOwner them,
            ISystemContext ctx,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                contact.Identification.IdentityLevel.Should().Be(IdentificationLevel.Known);

                contactService.Save(contact, them, true, ctx)
                    .Should().Be(true);

                //contact.SystemInfo.VisitCount = 999; // make a change

                contactService.Save(contact, us, false, ctx)
                    .Should().Be(true);

                // Assert
                var res = contactService.TryLoadAndLock(contact.Id, us, TimeSpan.FromMinutes(1), ctx);
                res.Status.Should().Be(LockAttemptStatus.Success);
                res.LockedObject.ShouldBeEquivalentToContact(contact, l => l.Expires > DateTime.UtcNow && l.IsOwnedBy(us) && l.Version == 4);

                contactService.LoadForReadOnly(contact.Identification.Identity, ctx)
                    .ShouldBeEquivalentToContact(contact, l => l == null);
            }
        }
        public void TryLoadAndLockByIdentity_ContactObsolete_NullReturned(
            IElasticAnalyticsContactService contactService,
            ElasticContact contact,
            ElasticContact successor,
            ElasticLeaseOwner owner,
            ISystemContext ctx,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                contactService.Save(contact, owner, true, ctx)
                    .Should().Be(true);

                contactService.Obsolete(contact.Id, successor.Id, owner, ctx);
                contactService.Save(successor, owner, true, ctx);

                var res = contactService.TryLoadAndLock(contact.Identification.Identity, owner, TimeSpan.FromMinutes(1), ctx);
                res.Status.Should().Be(LockAttemptStatus.NotFound);
                res.LockedObject.Should().BeNull();
            }
        }
        public void SaveContact_ContactDoesntExistShouldBeReleased_ContactCreatedLockFree(
            IElasticAnalyticsContactService contactService,
            ElasticContact contact,
            ElasticLeaseOwner them,
            ElasticLeaseOwner us,
            ISystemContext ctx,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                contactService.Save(contact, them, true, ctx)
                    .Should().Be(true);

                var res = contactService.TryLoadAndLock(contact.Id, us, TimeSpan.FromMinutes(1), ctx);

                res.Status.Should().Be(LockAttemptStatus.Success);
                res.LockedObject.ShouldBeEquivalentToContact(contact, l => l.Expires > DateTime.UtcNow && l.IsOwnedBy(us) && l.Version == 3);

                contactService.LoadForReadOnly(contact.Identification.Identity, ctx)
                    .ShouldBeEquivalentToContact(contact);
            }
        }
 public void TryLoadAndLockByIdentity_ContactDoesntExist_NullReturned(
     IElasticAnalyticsContactService contactService,
     ElasticLeaseOwner us,
     string identity,
     ISystemContext ctx,
     TestIndexUtils contactIndex)
 {
     using (contactIndex)
     {
         var res = contactService.TryLoadAndLock(identity, us, TimeSpan.FromMinutes(1), ctx);
         res.Status.Should().Be(LockAttemptStatus.NotFound);
         res.LockedObject.Should().BeNull();
     }
 }
        public void TryLoadAndLockById_ContactObsolete_SuccessorReturned(
            IElasticAnalyticsContactService contactService,
            ElasticContact contact,
            ElasticContact successor,
            ElasticLeaseOwner owner,
            ISystemContext ctx,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                contactService.Save(contact, owner, true, ctx)
                    .Should().Be(true);

                contactService.Obsolete(contact.Id, successor.Id, owner, ctx);
                contactService.Save(successor, owner, true, ctx);

                var res = contactService.TryLoadAndLock(contact.Id, owner, TimeSpan.FromMinutes(1), ctx);

                res.Status.Should().Be(LockAttemptStatus.Success);
                res.LockedObject.ShouldBeEquivalentToContact(successor, l => l.Expires > DateTime.UtcNow && l.IsOwnedBy(owner) && l.Version == 3);
            }
        }
        public void TryLoadAndLockByIdentity_ContactExistsAlreadyLockedByDifferentOwner_NotLoaded(
            IElasticAnalyticsContactService contactService,
            ElasticLeaseOwner us,
            ElasticLeaseOwner them,
            ElasticContact eContact,
            ISystemContext ctx,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                contactService.Save(eContact, them, false, ctx).Should().BeTrue();

                var res = contactService.TryLoadAndLock(eContact.Identification.Identity, us, TimeSpan.FromMinutes(1), ctx);
                res.LockedObject.Id.Should().Be(eContact.Id);
                res.Status.Should().Be(LockAttemptStatus.AlreadyLocked);
            }
        }
        public void TryLoadAndLockByIdentity_ContactExistsAlreadyLockedBySameOwner_ReturnsContact(
            IElasticAnalyticsContactService contactService,
            ElasticLeaseOwner us,
            ElasticContact eContact,
            ISystemContext ctx,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                contactService.Save(eContact, us, false, ctx).Should().BeTrue();

                var res = contactService.TryLoadAndLock(eContact.Identification.Identity, us, TimeSpan.FromMinutes(1), ctx);

                eContact.Lease.IncrementVersion();
                res.LockedObject.ShouldBeEquivalentToContact(eContact);
                res.Status.Should().Be(LockAttemptStatus.Success);
            }
        }
        public void TryLoadAndLockByIdentity_ContactExistsWithSuccessor_ContactReturned(
            TestIndexUtils contactIndex,
            IElasticAnalyticsContactService contactService,
            ElasticContact firstContact,
            ElasticContact secondContact,
            ElasticLeaseOwner us,
            ISystemContext ctx)
        {
            using (contactIndex)
            {
                firstContact.Successor = secondContact.Id;
                contactService.Save(firstContact, us, true, ctx);
                contactService.Save(secondContact, us, true, ctx);

                var res = contactService.TryLoadAndLock(firstContact.Identification.Identity, us, TimeSpan.FromMinutes(1), ctx);

                res.Status.Should().Be(LockAttemptStatus.Success);
                res.LockedObject.ShouldBeEquivalentToContact(secondContact, l => l.Expires > DateTime.UtcNow && l.IsOwnedBy(us) && l.Version == 3);
            }
        }
        public void ReleaseContact_CurrentLockByDifferentOwner_ReturnsFalse(
            IElasticAnalyticsContactService contactService,
            IRepository<ElasticContact> contactRepo,
            ElasticContact contact,
            ElasticLeaseOwner us,
            ElasticLeaseOwner them,
            ISystemContext ctx,
            IDateTimeController dateTime,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                // contact exists and locked
                contactService.Save(contact, them, false, ctx).Should().BeTrue();

                // release
                contactService.Release(contact.Id, us, ctx).Should().BeFalse();

                // shouldn't be able to lock it again -- ??
                contactService.TryLoadAndLock(contact.Id, us, TimeSpan.FromHours(1), ctx)
                    .Status.Should()
                    .Be(LockAttemptStatus.AlreadyLocked);
            }
        }
        public void ReleaseContact_ExpiredLockExistsForDifferentOwner_NothingHappens(
            IElasticAnalyticsContactService contactService,
            ElasticContact contact,
            ElasticLeaseOwner us,
            ElasticLeaseOwner them,
            ISystemContext ctx,
            IDateTimeController dateTime,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                // contact exists, lock expired
                var timeTraveller = (DateTimeTimeTraveller)dateTime;
                using (timeTraveller.NewJourney(-24))
                {
                    contactService.Save(contact, them, false, ctx).Should().BeTrue();
                }

                // release
                contactService.Release(contact.Id, us, ctx);

                // should be able to lock it again
                contactService.TryLoadAndLock(contact.Id, us, TimeSpan.FromHours(1), ctx)
                    .Status.Should()
                    .Be(LockAttemptStatus.Success);
            }
        }