コード例 #1
0
        public async Task TryReacquireLease_WhenLeaseExistsForAnotherInstance_ShouldNotReacquireLease()
        {
            // Arrange
            DateTime currentDateTime = new DateTime(2000, 1, 1, 12, 0, 0);

            ServerDateTime.UtcNowFunc = () => currentDateTime;
            var    instanceId = Guid.NewGuid();
            ILease lease      = new TestLease
            {
                Priority    = _options.Priority,
                LeaseType   = _options.WorkerType,
                InstanceId  = Guid.NewGuid(),
                LeasedUntil = currentDateTime.AddMinutes(5)
            };

            _mockStore.Setup(m => m.ReadByLeaseTypeAsync(It.IsAny <string>()))
            .ReturnsAsync((ILease)null);

            // Act
            var result = await _leaseAllocator.TryReacquireLease(instanceId, CancellationToken.None);

            // Assert
            result.Should().BeNull();
            _mockStore.Verify(m => m.ReadByLeaseTypeAsync(It.IsAny <string>()), Times.Once);
        }
コード例 #2
0
        public async Task TestAllocateLeaseAsync_WhenExistingExpiredLease_ShouldSelectWinner()
        {
            // Arrange
            DateTime currentDateTime = new DateTime(2000, 1, 1, 12, 0, 0);

            ServerDateTime.UtcNowFunc = () => currentDateTime;

            var lease = new TestLease
            {
                Id          = "abc",
                InstanceId  = Guid.NewGuid(),
                LeasedUntil = currentDateTime.Subtract(new TimeSpan(1, 0, 0)),
                Priority    = _options.Priority + 1,
                LeaseType   = _options.WorkerType
            };

            _mockStore.Setup(m => m.ReadByLeaseTypeAsync(It.IsAny <string>())).ReturnsAsync(lease);

            // Act

            await _leaseAllocator.AllocateLeaseAsync(lease.InstanceId.Value, It.IsAny <CancellationToken>());

            // Assert
            _mockStore.Verify(m => m.ReadByLeaseTypeAsync(It.IsAny <string>()), Times.Once);
            _mockStore.Verify(m => m.AddLeaseRequestAsync(It.Is <LeaseRequest>(req => req.LeaseType == lease.LeaseType &&
                                                                               req.Priority == _options.Priority && req.InstanceId == lease.InstanceId.Value &&
                                                                               req.TimeToLive == 2 * _options.ElectionDelay.Seconds)), Times.Once);
            _mockStore.Verify(m => m.SelectWinnerRequestAsync(lease.LeaseType), Times.Once);
        }
コード例 #3
0
        public async Task TestAllocateLeaseAsync_OnExistingLeaseWithSameInstanceId_ShouldReturnExistingLease()
        {
            // Arrange
            var currentDateTime = new DateTime(2000, 1, 1, 12, 0, 0);

            ServerDateTime.UtcNowFunc = () => currentDateTime;

            var instanceId = Guid.NewGuid();
            var testLease  = new TestLease
            {
                LeasedUntil = currentDateTime.Add(_options.LeaseInterval),
                Priority    = 2,
                InstanceId  = instanceId
            };

            _mockStore.Setup(m => m.ReadByLeaseTypeAsync(It.IsAny <string>()))
            .ReturnsAsync(testLease);

            // Act
            var result = await _leaseAllocator.AllocateLeaseAsync(instanceId, It.IsAny <CancellationToken>());

            // Assert
            result.Should().Be(testLease);
            _mockStore.Verify(m => m.ReadByLeaseTypeAsync(It.IsAny <string>()), Times.Once);
            _mockStore.VerifyNoOtherCalls();
        }
コード例 #4
0
        public async Task TestAllocateLeaseAsync_WhenExistingExpiredLease_ShouldUpdateLease()
        {
            // Arrange
            DateTime currentDateTime = new DateTime(2000, 1, 1, 12, 0, 0);

            ServerDateTime.UtcNowFunc = () => currentDateTime;

            ILease lease = new TestLease
            {
                Id         = "Abc",
                Priority   = _options.Priority,
                LeaseType  = _options.WorkerType,
                InstanceId = Guid.NewGuid()
            };

            _mockStore.Setup(m => m.ReadByLeaseTypeAsync(It.IsAny <string>()))
            .ReturnsAsync(lease);

            _mockStore.Setup(m =>
                             m.SelectWinnerRequestAsync(It.IsAny <string>()))
            .ReturnsAsync(lease.InstanceId);


            _mockStore.Setup(m => m.TryUpdateLeaseAsync(It.IsAny <ILease>()))
            .ReturnsAsync(new LeaseStoreResult(lease, true));
            // Act
            await _leaseAllocator.AllocateLeaseAsync(lease.InstanceId.Value, It.IsAny <CancellationToken>());

            //Assert

            _mockStore.Verify(m => m.TryCreateLeaseAsync(It.IsAny <ILease>()), Times.Never);
            _mockStore.Verify(m => m.SelectWinnerRequestAsync(_options.WorkerType), Times.Once);
            _mockStore.Verify(m => m.TryUpdateLeaseAsync(lease), Times.Once);
        }
コード例 #5
0
        public async Task TestAllocateLeaseAsync_OnCreateLeaseFailWithTheSameInstanceId_ShouldReturnExistingLease()
        {
            // Arrange
            DateTime currentDateTime = new DateTime(2000, 1, 1, 12, 0, 0);

            ServerDateTime.UtcNowFunc = () => currentDateTime;
            var instanceId = Guid.NewGuid();
            var lease      = new TestLease
            {
                LeasedUntil = currentDateTime.Add(_options.LeaseInterval),
                Priority    = _options.Priority,
                InstanceId  = instanceId
            };

            _mockStore.Setup(m =>
                             m.TryCreateLeaseAsync(It.IsAny <ILease>()))
            .ReturnsAsync(new LeaseStoreResult(null, false));

            _mockStore.Setup(m =>
                             m.TryUpdateLeaseAsync(It.IsAny <ILease>()))
            .ReturnsAsync(new LeaseStoreResult(lease, true));

            var testLease = new TestLease
            {
                LeasedUntil = currentDateTime.Add(_options.LeaseInterval),
                Priority    = 2,
                InstanceId  = instanceId
            };

            _mockStore.SetupSequence(m => m.ReadByLeaseTypeAsync(It.IsAny <string>()))
            .ReturnsAsync((ILease)null)
            .ReturnsAsync(testLease);

            _mockStore.Setup(m =>
                             m.SelectWinnerRequestAsync(It.IsAny <string>()))
            .ReturnsAsync(instanceId);

            // Act
            var result = await _leaseAllocator.AllocateLeaseAsync(instanceId, It.IsAny <CancellationToken>());

            // Assert
            result.Should().Be(testLease);
            _mockStore.Verify(m => m.ReadByLeaseTypeAsync(It.IsAny <string>()), Times.Exactly(2));
            _mockStore.Verify(m => m.TryCreateLeaseAsync(It.Is <ILease>(l => l.InstanceId == instanceId)), Times.Once);
            _mockStore.Verify(m => m.SelectWinnerRequestAsync(It.IsAny <string>()), Times.Once);
            _mockStore.Verify(m => m.TryUpdateLeaseAsync(It.IsAny <ILease>()), Times.Never);
        }
コード例 #6
0
        public async Task TestReleaseLeaseAsync_OnUpdateSucceeded_ShouldSucceed()
        {
            var lease = new TestLease
            {
                InstanceId = Guid.NewGuid()
            };

            // Arrange
            _mockStore.Setup(m =>
                             m.TryUpdateLeaseAsync(It.Is <ILease>(l =>
                                                                  l.Priority == -1 && !l.LeasedUntil.HasValue && !l.InstanceId.HasValue)))
            .ReturnsAsync(new LeaseStoreResult(lease, true));

            // Act
            await _leaseAllocator.ReleaseLeaseAsync(lease);

            // Assert
            _mockStore.Verify(m =>
                              m.TryUpdateLeaseAsync(It.Is <ILease>(l =>
                                                                   l.Priority == -1 && !l.LeasedUntil.HasValue && !l.InstanceId.HasValue)));

            _mockTelemetry.Verify(t => t.Publish(It.IsAny <LeaseReleaseEvent>(), It.IsAny <string>(),
                                                 It.IsAny <string>(), It.IsAny <int>()), Times.Never);
        }
コード例 #7
0
        public async Task TestAllocateLeaseAsync_OnLeaseUpdateSucceeded_ShouldUpdateLease()
        {
            // Arrange
            DateTime currentDateTime = new DateTime(2000, 1, 1, 12, 0, 0);

            ServerDateTime.UtcNowFunc = () => currentDateTime;

            ILease lease = new TestLease
            {
                Id         = "Abc",
                Priority   = _options.Priority,
                LeaseType  = _options.WorkerType,
                InstanceId = Guid.NewGuid()
            };

            _mockStore.Setup(m => m.ReadByLeaseTypeAsync(It.IsAny <string>()))
            .ReturnsAsync(lease);

            _mockStore.Setup(m =>
                             m.SelectWinnerRequestAsync(It.IsAny <string>()))
            .ReturnsAsync(lease.InstanceId);


            _mockStore.Setup(m => m.TryUpdateLeaseAsync(It.IsAny <ILease>()))
            .ReturnsAsync(new LeaseStoreResult(lease, true));

            // Act
            var result = await _leaseAllocator.AllocateLeaseAsync(lease.InstanceId.Value, It.IsAny <CancellationToken>());

            // Assert
            result.Should().Be(lease);
            _mockStore.Verify(m => m.ReadByLeaseTypeAsync(It.IsAny <string>()), Times.Exactly(1));
            _mockStore.Verify(m => m.TryUpdateLeaseAsync(It.Is <ILease>(l => l.LeaseType == _options.WorkerType && l.Priority == _options.Priority && l.InstanceId == lease.InstanceId)), Times.Once);
            _mockStore.Verify(m => m.SelectWinnerRequestAsync(It.IsAny <string>()), Times.Once);
            _mockStore.Verify(m => m.TryCreateLeaseAsync(It.IsAny <ILease>()), Times.Never);
        }