public async Task Controller_ShouldCopyParentLeaseProperties_IfPartitionSplitHappened()
        {
            //arrange
            var customProperties = new Dictionary <string, string> {
                { "key", "value" }
            };
            var    lease        = Mock.Of <ILease>(l => l.PartitionId == PartitionId && l.Properties == customProperties);
            var    synchronizer = Mock.Of <IPartitionSynchronizer>();
            ILease leaseChild1  = CreateMockLease();
            ILease leaseChild2  = CreateMockLease();

            Mock.Get(synchronizer)
            .Setup(s => s.SplitPartitionAsync(It.Is <ILease>(l => l.PartitionId == PartitionId && l.ContinuationToken == LastContinuationToken)))
            .ReturnsAsync(new[] { leaseChild1, leaseChild2 });

            var partitionSupervisor        = Mock.Of <IPartitionSupervisor>(o => o.RunAsync(It.IsAny <CancellationToken>()) == Task.FromException(new PartitionSplitException("message", LastContinuationToken)));
            var partitionSupervisorFactory = Mock.Of <IPartitionSupervisorFactory>(f => f.Create(lease) == partitionSupervisor);
            var leaseManager   = Mock.Of <ILeaseManager>(manager => manager.AcquireAsync(lease) == Task.FromResult(lease));
            var leaseContainer = Mock.Of <ILeaseContainer>();

            var sut = new PartitionController(leaseContainer, leaseManager, partitionSupervisorFactory, synchronizer);

            //act
            await sut.AddOrUpdateLeaseAsync(lease).ConfigureAwait(false);

            await sut.ShutdownAsync().ConfigureAwait(false);

            Mock.Get(leaseChild1)
            .VerifySet(l => l.Properties = customProperties, Times.Once);
            Mock.Get(leaseChild2)
            .VerifySet(l => l.Properties = customProperties, Times.Once);
        }
        public async Task Controller_ShouldPassLastKnownContinuationTokenToSynchronizer_IfPartitionSplitHappened()
        {
            //arrange
            var lease        = Mock.Of <ILease>(l => l.PartitionId == PartitionId && l.ContinuationToken == InitialContinuationToken);
            var synchronizer = Mock.Of <IPartitionSynchronizer>();

            Mock.Get(synchronizer)
            .Setup(s => s.SplitPartitionAsync(It.Is <ILease>(l => l.PartitionId == PartitionId && l.ContinuationToken == LastContinuationToken)))
            .ReturnsAsync(new[] { CreateMockLease(), CreateMockLease() });

            var partitionSupervisor        = Mock.Of <IPartitionSupervisor>(o => o.RunAsync(It.IsAny <CancellationToken>()) == Task.FromException(new PartitionSplitException("message", LastContinuationToken)));
            var partitionSupervisorFactory = Mock.Of <IPartitionSupervisorFactory>(f => f.Create(lease) == partitionSupervisor);
            var leaseManager   = Mock.Of <ILeaseManager>(manager => manager.AcquireAsync(lease) == Task.FromResult(lease));
            var leaseContainer = Mock.Of <ILeaseContainer>();

            var sut = new PartitionController(leaseContainer, leaseManager, partitionSupervisorFactory, synchronizer);

            //act
            await sut.AddOrUpdateLeaseAsync(lease).ConfigureAwait(false);

            //assert
            await sut.ShutdownAsync().ConfigureAwait(false);

            Mock.Get(synchronizer).VerifyAll();
        }
        public async Task Controller_ShouldDeleteParentLease_IfChildLeaseAcquireThrows()
        {
            //arrange
            var    lease        = CreateMockLease(PartitionId);
            var    synchronizer = Mock.Of <IPartitionSynchronizer>();
            ILease leaseChild2  = CreateMockLease();

            Mock.Get(synchronizer)
            .Setup(s => s.SplitPartitionAsync(lease))
            .ReturnsAsync(new[] { CreateMockLease(), leaseChild2 });

            var partitionSupervisor        = Mock.Of <IPartitionSupervisor>(o => o.RunAsync(It.IsAny <CancellationToken>()) == Task.FromException(new PartitionSplitException("message", LastContinuationToken)));
            var partitionSupervisorFactory = Mock.Of <IPartitionSupervisorFactory>(f => f.Create(lease) == partitionSupervisor);
            var leaseManager = Mock.Of <ILeaseManager>(manager =>
                                                       manager.AcquireAsync(lease) == Task.FromResult(lease) &&
                                                       manager.AcquireAsync(leaseChild2) == Task.FromException <ILease>(new LeaseLostException())
                                                       );
            var leaseContainer = Mock.Of <ILeaseContainer>();

            var sut = new PartitionController(leaseContainer, leaseManager, partitionSupervisorFactory, synchronizer);

            //act
            await sut.AddOrUpdateLeaseAsync(lease).ConfigureAwait(false);

            //assert
            await sut.ShutdownAsync().ConfigureAwait(false);

            Mock.Get(leaseManager).Verify(manager => manager.DeleteAsync(lease), Times.Once);
        }
        public async Task Controller_ShouldIgnoreProcessingChildPartition_IfPartitionAlreadyAdded()
        {
            //arrange
            var    lease        = CreateMockLease(PartitionId);
            var    synchronizer = Mock.Of <IPartitionSynchronizer>();
            ILease leaseChild1  = CreateMockLease();
            ILease leaseChild2  = CreateMockLease();

            Mock.Get(synchronizer)
            .Setup(s => s.SplitPartitionAsync(It.Is <ILease>(l => l.PartitionId == PartitionId && l.ContinuationToken == LastContinuationToken)))
            .ReturnsAsync(new[] { leaseChild1, leaseChild2 });

            var partitionSupervisor  = Mock.Of <IPartitionSupervisor>(o => o.RunAsync(It.IsAny <CancellationToken>()) == Task.FromException(new PartitionSplitException("message", LastContinuationToken)));
            var partitionSupervisor1 = Mock.Of <IPartitionSupervisor>();

            Mock.Get(partitionSupervisor1).Setup(o => o.RunAsync(It.IsAny <CancellationToken>())).Returns <CancellationToken>(token => Task.Delay(TimeSpan.FromHours(1), token));
            var partitionSupervisor2 = Mock.Of <IPartitionSupervisor>();

            Mock.Get(partitionSupervisor2).Setup(o => o.RunAsync(It.IsAny <CancellationToken>())).Returns <CancellationToken>(token => Task.Delay(TimeSpan.FromHours(1), token));

            var partitionSupervisorFactory = Mock.Of <IPartitionSupervisorFactory>(f =>
                                                                                   f.Create(lease) == partitionSupervisor && f.Create(leaseChild1) == partitionSupervisor1 && f.Create(leaseChild2) == partitionSupervisor2);
            var leaseManager   = Mock.Of <ILeaseManager>(manager => manager.AcquireAsync(lease) == Task.FromResult(lease));
            var leaseContainer = Mock.Of <ILeaseContainer>();

            var sut = new PartitionController(leaseContainer, leaseManager, partitionSupervisorFactory, synchronizer);

            //act
            await sut.AddOrUpdateLeaseAsync(lease).ConfigureAwait(false);

            await sut.AddOrUpdateLeaseAsync(leaseChild2).ConfigureAwait(false);

            await sut.AddOrUpdateLeaseAsync(leaseChild2).ConfigureAwait(false);

            await sut.AddOrUpdateLeaseAsync(leaseChild2).ConfigureAwait(false);

            await sut.AddOrUpdateLeaseAsync(leaseChild2).ConfigureAwait(false);

            await sut.AddOrUpdateLeaseAsync(leaseChild2).ConfigureAwait(false);

            //assert
            await sut.ShutdownAsync().ConfigureAwait(false);

            Mock.Get(leaseManager)
            .Verify(manager => manager.AcquireAsync(leaseChild2), Times.Once);

            Mock.Get(leaseManager)
            .Verify(manager => manager.UpdatePropertiesAsync(leaseChild2), Times.Exactly(5));

            Mock.Get(partitionSupervisorFactory)
            .Verify(f => f.Create(leaseChild2), Times.Once);
        }
예제 #5
0
        public async Task Controller_ShouldKeepParentLease_IfSplitThrows()
        {
            //arrange
            var lease                      = CreateMockLease(PartitionId);
            var synchronizer               = Mock.Of <IPartitionSynchronizer>(s => s.SplitPartitionAsync(lease) == Task.FromException <IEnumerable <ILease> >(new InvalidOperationException()));
            var partitionSupervisor        = Mock.Of <IPartitionSupervisor>(o => o.RunAsync(It.IsAny <CancellationToken>()) == Task.FromException(new PartitionSplitException("message", LastContinuationToken)));
            var partitionSupervisorFactory = Mock.Of <IPartitionSupervisorFactory>(f => f.Create(lease) == partitionSupervisor);
            var leaseManager               = Mock.Of <ILeaseManager>();

            var sut = new PartitionController(leaseManager, partitionSupervisorFactory, synchronizer);

            //act
            await sut.AddOrUpdateLeaseAsync(lease).ConfigureAwait(false);

            //assert
            await sut.ShutdownAsync().ConfigureAwait(false);

            Mock.Get(leaseManager).Verify(manager => manager.DeleteAsync(lease), Times.Never);
        }
        public async Task Controller_ShouldSignalSynchronizerSplitPartition_IfObserverThrowsPartitionSplitException()
        {
            Mock.Get(partitionSupervisorFactory)
            .Setup(f => f.Create(leaseChild))
            .Returns(new PartitionSupervisor(leaseChild, MockObserver(), MockPartitionProcessor(), MockRenewer()));

            Mock.Get(partitionSupervisorFactory)
            .Setup(f => f.Create(leaseChild2))
            .Returns <ILease>(l => new PartitionSupervisor(l, MockObserver(), MockPartitionProcessor(), MockRenewer()));

            Mock.Get(leaseManager)
            .Setup(manager => manager.AcquireAsync(leaseChild))
            .ReturnsAsync(leaseChild);

            Mock.Get(leaseManager)
            .Setup(manager => manager.AcquireAsync(leaseChild2))
            .ReturnsAsync(leaseChild2);

            Mock.Get(leaseManager)
            .Setup(manager => manager.DeleteAsync(lease))
            .Returns(Task.FromResult(false));

            Mock.Get(leaseManager)
            .Setup(manager => manager.ReleaseAsync(leaseChild))
            .Returns(Task.FromResult(false));

            Mock.Get(leaseManager)
            .Setup(manager => manager.ReleaseAsync(leaseChild2))
            .Returns(Task.FromResult(false));

            Mock.Get(synchronizer)
            .Setup(s => s.SplitPartitionAsync(lease))
            .ReturnsAsync(new[] { leaseChild, leaseChild2 });

            await sut.AddOrUpdateLeaseAsync(lease).ConfigureAwait(false);
        }
        public async Task AddLease_ShouldAcquireLease_WhenCalled()
        {
            await sut.AddOrUpdateLeaseAsync(lease).ConfigureAwait(false);

            Mock.Get(leaseManager)
            .Verify(manager => manager.AcquireAsync(lease), Times.Once);
        }