public async Task InitializeAsync_ShouldReleaseLock_OnException() { var synchronizer = Mock.Of <IPartitionSynchronizer>(); Mock.Get(synchronizer) .Setup(s => s.CreateMissingLeasesAsync()) .ThrowsAsync(DocumentExceptionHelpers.CreateConflictException()); var leaseStore = Mock.Of <ILeaseStore>(); Mock.Get(leaseStore) .Setup(store => store.IsInitializedAsync()) .ReturnsAsync(false); Mock.Get(leaseStore) .Setup(store => store.AcquireInitializationLockAsync(lockTime)) .ReturnsAsync(true); Mock.Get(leaseStore) .Setup(store => store.ReleaseInitializationLockAsync()) .ReturnsAsync(true); var bootstrapper = new Bootstrapper(synchronizer, leaseStore, lockTime, sleepTime); Exception exception = await Record.ExceptionAsync(async() => await bootstrapper.InitializeAsync()); Assert.IsAssignableFrom <DocumentClientException>(exception); Mock.Get(leaseStore).VerifyAll(); }
public async Task ReleaseAsync_ReThrows_WhenOtherDocumentException() { var documentClient = Mock.Of <IChangeFeedDocumentClient>(); var cachedLease = CreateCachedLease(owner); var leaseStoreManager = CreateLeaseStoreManager(documentClient, owner); Mock.Get(documentClient) .Setup(c => c.ReadDocumentAsync(It.IsAny <Uri>(), null, default(CancellationToken))) .ThrowsAsync(DocumentExceptionHelpers.CreateConflictException()); var exception = await Record.ExceptionAsync(async() => await leaseStoreManager.ReleaseAsync(cachedLease)); Assert.IsAssignableFrom <DocumentClientException>(exception); }
public async Task RenewAsync_ReThrows_WhenOtherDocumentException() { var documentClient = Mock.Of <IChangeFeedDocumentClient>(); var cachedLease = CreateCachedLease(owner); var leaseUpdater = Mock.Of <IDocumentServiceLeaseUpdater>(); var leaseManager = CreateLeaseManager(documentClient, leaseUpdater, owner); Mock.Get(documentClient) .Setup(c => c.ReadDocumentAsync(It.IsAny <Uri>())) .ThrowsAsync(DocumentExceptionHelpers.CreateConflictException()); var exception = await Record.ExceptionAsync(async() => await leaseManager.RenewAsync(cachedLease)); Assert.IsAssignableFrom <DocumentClientException>(exception); }
public async Task MarkInitializedAsync_ShouldSucceed_IfMarkerConflicts() { var client = Mock.Of <IChangeFeedDocumentClient>(); Mock.Get(client) .Setup(c => c.CreateDocumentAsync(It.IsAny <string>(), It.IsAny <object>())) .ThrowsAsync(DocumentExceptionHelpers.CreateConflictException()); var leaseStore = new LeaseStore(client, collectionInfo, containerNamePrefix, leaseStoreCollectionLink); await leaseStore.MarkInitializedAsync(); Mock.Get(client) .Verify(c => c.CreateDocumentAsync(leaseStoreCollectionLink, It.Is <Document>(d => d.Id == storeMarker)), Times.Once); }
public async Task DeleteAsync_Throws_IfDeleteFails() { var documentClient = Mock.Of <IChangeFeedDocumentClient>(); var cachedLease = CreateCachedLease(owner); var leaseStoreManager = CreateLeaseStoreManager(documentClient, owner); Mock.Get(documentClient) .Setup(c => c.DeleteDocumentAsync(documentUri, null, default(CancellationToken))) .ThrowsAsync(DocumentExceptionHelpers.CreateConflictException()) .Verifiable(); var exception = await Record.ExceptionAsync(async() => await leaseStoreManager.DeleteAsync(cachedLease)); Assert.IsAssignableFrom <DocumentClientException>(exception); }
public async Task LockInitializationAsync_ShouldReturnFalse_IfLockConflicts() { var client = Mock.Of <IChangeFeedDocumentClient>(); Mock.Get(client) .Setup(c => c.CreateDocumentAsync(It.IsAny <string>(), It.IsAny <object>())) .ThrowsAsync(DocumentExceptionHelpers.CreateConflictException()); var leaseStore = new LeaseStore(client, collectionInfo, containerNamePrefix, leaseStoreCollectionLink); bool isLocked = await leaseStore.LockInitializationAsync(lockTime); Assert.False(isLocked); Mock.Get(client) .Verify(c => c.CreateDocumentAsync(leaseStoreCollectionLink, It.Is <Document>(d => d.TimeToLive == (int)lockTime.TotalSeconds && d.Id == "prefix.lock")), Times.Once); }
public async Task CreateLeaseIfNotExistAsync_ReturnsNull_WhenDocumentExists() { var documentClient = Mock.Of <IChangeFeedDocumentClient>(); var leaseStoreManager = CreateLeaseStoreManager(documentClient, owner); Mock.Get(documentClient) .Setup(c => c.CreateDocumentAsync(collectionLink, It.Is <DocumentServiceLease>(d => d.PartitionId == partitionId && d.ContinuationToken == continuationToken && d.Id == $"{storeNamePrefix}..{partitionId}"), null, false, default(CancellationToken))) .ThrowsAsync(DocumentExceptionHelpers.CreateConflictException()) .Verifiable(); var lease = await leaseStoreManager.CreateLeaseIfNotExistAsync(partitionId, continuationToken); Mock.Get(documentClient).VerifyAll(); Assert.Null(lease); }
public async Task AcquireInitializationLockAsync_ShouldReturnFalse_IfLockConflicts() { var client = Mock.Of <IChangeFeedDocumentClient>(); Mock.Get(client) .Setup(c => c.CreateDocumentAsync(It.IsAny <string>(), It.IsAny <object>(), null, false, default(CancellationToken))) .ThrowsAsync(DocumentExceptionHelpers.CreateConflictException()); var leaseStore = new DocumentServiceLeaseStore(client, collectionInfo, containerNamePrefix, leaseCollectionLink, Mock.Of <IRequestOptionsFactory>()); bool isLocked = await leaseStore.AcquireInitializationLockAsync(lockTime); Assert.False(isLocked); Mock.Get(client) .Verify(c => c.CreateDocumentAsync(leaseCollectionLink, It.Is <Document>(d => d.TimeToLive == (int)lockTime.TotalSeconds && d.Id == "prefix.lock"), null, false, default(CancellationToken)), Times.Once); }
public async Task UpdateLeaseAsync_ShouldThrowLostLeaseException_WhenReplaceReturnsConflict() { Exception exception = await TestReplaceException(DocumentExceptionHelpers.CreateConflictException()); Assert.IsAssignableFrom <LeaseLostException>(exception); }