private DocumentServiceLeaseStoreManager CreateLeaseStoreManager( IChangeFeedDocumentClient documentClient, string hostName, IDocumentServiceLeaseUpdater leaseUpdater = null, IRequestOptionsFactory requestOptionsFactory = null) { var settings = new DocumentServiceLeaseStoreManagerSettings { LeaseCollectionInfo = collectionInfo, ContainerNamePrefix = storeNamePrefix, LeaseCollectionLink = collectionLink, HostName = hostName, }; // Make sure we test both constructors. return(leaseUpdater != null ? new DocumentServiceLeaseStoreManager( settings, documentClient, requestOptionsFactory ?? Mock.Of <IRequestOptionsFactory>(), leaseUpdater) : new DocumentServiceLeaseStoreManager( settings, documentClient, requestOptionsFactory ?? Mock.Of <IRequestOptionsFactory>())); }
private static void SetupUpdateLeaseAfterRead(IDocumentServiceLeaseUpdater leaseUpdater, string storedContinuationToken, MockLease storedLease) { Mock.Get(leaseUpdater) .Setup(u => u.UpdateLeaseAsync(It.Is <ILease>(l => l.ContinuationToken == storedContinuationToken), documentUri, It.IsAny <Func <ILease, ILease> >())) .Callback((ILease l, Uri uri, Func <ILease, ILease> callback) => callback(storedLease)) .ReturnsAsync(storedLease); }
public async Task UpdateAsync_ThrowsLeaseLost_WhenCachedLeaseTakenByOtherOwner() { var documentClient = Mock.Of <IChangeFeedDocumentClient>(); var cachedLease = CreateCachedLease("cached owner"); IDocumentServiceLeaseUpdater leaseUpdater = CreateLeaseUpdater(cachedLease); var leaseManager = CreateLeaseManager(documentClient, leaseUpdater, owner); var exception = await Record.ExceptionAsync(async() => await leaseManager.UpdatePropertiesAsync(cachedLease)); Assert.IsAssignableFrom <LeaseLostException>(exception); }
public async Task CheckpointAsync_ThrowsLeaseLost_WhenLeaseNotOwned() { var documentClient = Mock.Of <IChangeFeedDocumentClient>(); var cachedLease = CreateCachedLease("new owner"); IDocumentServiceLeaseUpdater leaseUpdater = CreateLeaseUpdater(cachedLease); var leaseManager = CreateLeaseManager(documentClient, leaseUpdater, owner); const string newToken = "newToken"; var exception = await Record.ExceptionAsync(async() => await leaseManager.CheckpointAsync(cachedLease, newToken)); Assert.IsAssignableFrom <LeaseLostException>(exception); }
public async Task CheckpointAsync_UpdatesLease_WhenLeaseOwned() { var documentClient = Mock.Of <IChangeFeedDocumentClient>(); var cachedLease = CreateCachedLease(owner); IDocumentServiceLeaseUpdater leaseUpdater = CreateLeaseUpdater(cachedLease); var leaseManager = CreateLeaseManager(documentClient, leaseUpdater, owner); const string newToken = "newToken"; var lease = await leaseManager.CheckpointAsync(cachedLease, newToken); Assert.Equal(newToken, lease.ContinuationToken); Assert.Equal(leaseId, lease.Id); }
public async Task UpdateAsync_UpdatesLease_WhenLeaseOwnershipDoesNotChange() { var documentClient = Mock.Of <IChangeFeedDocumentClient>(); var cachedLease = CreateCachedLease(owner); cachedLease.Properties["key"] = "value"; IDocumentServiceLeaseUpdater leaseUpdater = CreateLeaseUpdater(cachedLease); var leaseManager = CreateLeaseManager(documentClient, leaseUpdater, owner); var lease = await leaseManager.UpdatePropertiesAsync(cachedLease); Assert.Equal(leaseId, lease.Id); Assert.Equal("value", lease.Properties["key"]); }
public async Task AcquireAsync_RetryConflicts_WhenAcquireReasonIsNotExpired(LeaseAcquireReason leaseAcquireReason) { var documentClient = Mock.Of <IChangeFeedDocumentClient>(); var cachedLease = CreateCachedLease(owner); ((ILeaseAcquireReasonProvider)cachedLease).AcquireReason = leaseAcquireReason; IDocumentServiceLeaseUpdater leaseUpdater = CreateLeaseUpdater(cachedLease); var leaseStoreManager = CreateLeaseStoreManager(documentClient, owner, leaseUpdater); await leaseStoreManager.AcquireAsync(cachedLease); Mock.Get(leaseUpdater) .Verify(u => u.UpdateLeaseAsync(cachedLease, documentUri, null, It.IsAny <Func <ILease, ILease> >(), true), Times.Once); }
/// <summary> /// Initializes a new instance of the <see cref="DocumentServiceLeaseStoreManager"/> class. /// </summary> /// <remarks> /// Internal only for testing purposes, otherwise would be private. /// </remarks> internal DocumentServiceLeaseStoreManager( DocumentServiceLeaseStoreManagerSettings settings, IChangeFeedDocumentClient leaseDocumentClient, IRequestOptionsFactory requestOptionsFactory, IDocumentServiceLeaseUpdater leaseUpdater) // For testing purposes only. { if (settings == null) { throw new ArgumentNullException(nameof(settings)); } if (settings.LeaseCollectionInfo == null) { throw new ArgumentNullException(nameof(settings.LeaseCollectionInfo)); } if (settings.ContainerNamePrefix == null) { throw new ArgumentNullException(nameof(settings.ContainerNamePrefix)); } if (settings.LeaseCollectionLink == null) { throw new ArgumentNullException(nameof(settings.LeaseCollectionLink)); } if (string.IsNullOrEmpty(settings.HostName)) { throw new ArgumentNullException(nameof(settings.HostName)); } if (leaseDocumentClient == null) { throw new ArgumentNullException(nameof(leaseDocumentClient)); } if (requestOptionsFactory == null) { throw new ArgumentException(nameof(requestOptionsFactory)); } if (leaseUpdater == null) { throw new ArgumentException(nameof(leaseUpdater)); } this.settings = settings; this.client = leaseDocumentClient; this.requestOptionsFactory = requestOptionsFactory; this.leaseUpdater = leaseUpdater; this.leaseStore = new DocumentServiceLeaseStore( this.client, this.settings.LeaseCollectionInfo, this.settings.ContainerNamePrefix, this.settings.LeaseCollectionLink, this.requestOptionsFactory); }
public async Task AcquireAsync_UpdatesLease_WhenLeaseOwnershipDoesNotChange() { var documentClient = Mock.Of <IChangeFeedDocumentClient>(); var cachedLease = CreateCachedLease(owner); cachedLease.Properties["key"] = "value"; IDocumentServiceLeaseUpdater leaseUpdater = CreateLeaseUpdater(cachedLease); const string newOwner = "new owner"; var leaseStoreManager = CreateLeaseStoreManager(documentClient, newOwner, leaseUpdater); var lease = await leaseStoreManager.AcquireAsync(cachedLease); Assert.Equal(leaseId, lease.Id); Assert.Equal(newOwner, lease.Owner); Assert.Equal("value", lease.Properties["key"]); }
public DocumentServiceLeaseManager( IChangeFeedDocumentClient client, IDocumentServiceLeaseUpdater leaseUpdater, DocumentCollectionInfo leaseStoreCollectionInfo, string containerNamePrefix, string leaseStoreCollectionLink, string hostName) { if (client == null) { throw new ArgumentNullException(nameof(client)); } if (leaseUpdater == null) { throw new ArgumentNullException(nameof(leaseUpdater)); } if (leaseStoreCollectionInfo == null) { throw new ArgumentNullException(nameof(leaseStoreCollectionInfo)); } if (containerNamePrefix == null) { throw new ArgumentNullException(nameof(containerNamePrefix)); } if (leaseStoreCollectionLink == null) { throw new ArgumentNullException(nameof(leaseStoreCollectionLink)); } if (string.IsNullOrEmpty(hostName)) { throw new ArgumentNullException(nameof(hostName)); } this.leaseStoreCollectionInfo = leaseStoreCollectionInfo; this.containerNamePrefix = containerNamePrefix; this.leaseStoreCollectionLink = leaseStoreCollectionLink; this.client = client; this.leaseUpdater = leaseUpdater; this.hostName = hostName; }
private DocumentServiceLeaseManager CreateLeaseManager(IChangeFeedDocumentClient documentClient, IDocumentServiceLeaseUpdater leaseUpdater, string hostName) { return(new DocumentServiceLeaseManager(documentClient, leaseUpdater, collectionInfo, storeNamePrefix, collectionLink, hostName)); }