public async Task IsInitializedAsync_ShouldReturnFalse_IfNoDocument() { var client = Mock.Of <IChangeFeedDocumentClient>(); Mock.Get(client) .Setup(c => c.ReadDocumentAsync(It.Is <Uri>(uri => uri.ToString().EndsWith(storeMarker)))) .ThrowsAsync(DocumentExceptionHelpers.CreateNotFoundException()); var leaseStore = new LeaseStore(client, collectionInfo, containerNamePrefix, leaseStoreCollectionLink); bool isInited = await leaseStore.IsInitializedAsync(); Assert.False(isInited); }
public async Task ReleaseAsync_ThrowsLeaseLost_WhenDocumentIsNotFound() { 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.CreateNotFoundException()); var exception = await Record.ExceptionAsync(async() => await leaseStoreManager.ReleaseAsync(cachedLease)); Assert.IsAssignableFrom <LeaseLostException>(exception); }
public async Task RenewAsync_ThrowsLeaseLost_WhenDocumentIsNotFound() { 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.CreateNotFoundException()); var exception = await Record.ExceptionAsync(async() => await leaseManager.RenewAsync(cachedLease)); Assert.IsAssignableFrom <LeaseLostException>(exception); }
public async Task DeleteAsync_ReturnsSuccess_IfDocumentDoesNotExist() { 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.CreateNotFoundException()) .Verifiable(); await leaseStoreManager.DeleteAsync(cachedLease); Mock.Get(documentClient).VerifyAll(); }
public async Task CreateLeaseIfNotExistAsync_Throws_WhenCreateFails() { 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.CreateNotFoundException()); var exception = await Record.ExceptionAsync(async() => await leaseStoreManager.CreateLeaseIfNotExistAsync(partitionId, continuationToken)); Assert.IsAssignableFrom <DocumentClientException>(exception); }
public async Task ReleaseInitializationLockAsync_ShouldReturnFalse_IfLockNotFound() { var client = Mock.Of <IChangeFeedDocumentClient>(); Mock.Get(client) .Setup(c => c.DeleteDocumentAsync(It.IsAny <Uri>(), It.IsAny <RequestOptions>(), default(CancellationToken))) .ThrowsAsync(DocumentExceptionHelpers.CreateNotFoundException()); var leaseStore = new DocumentServiceLeaseStore(client, collectionInfo, containerNamePrefix, leaseCollectionLink, Mock.Of <IRequestOptionsFactory>()); bool isLockFoundAndReleased = await leaseStore.ReleaseInitializationLockAsync(); Assert.False(isLockFoundAndReleased); Mock.Get(client) .Verify(c => c.DeleteDocumentAsync(It.Is <Uri>(uri => uri.OriginalString.EndsWith("prefix.lock")), It.IsAny <RequestOptions>(), default(CancellationToken)), Times.Once); }
public async Task BuildPassesPartitionKey_WhenLeaseCollectionIsPartitionedById() { var leaseCollection = MockHelpers.CreateCollection( "collectionId", "collectionRid", new PartitionKeyDefinition { Paths = { "/id" } }, collectionLink); var lease = Mock.Of <ILease>(); Mock.Get(lease) .SetupGet(l => l.Id) .Returns("leaseId"); var leaseClient = this.CreateMockDocumentClient(collection); Mock.Get(leaseClient) .Setup(c => c.ReadDocumentCollectionAsync( It.IsAny <Uri>(), It.IsAny <RequestOptions>())) .ReturnsAsync(new ResourceResponse <DocumentCollection>(leaseCollection)); Mock.Get(leaseClient) .Setup(c => c.ReadDocumentAsync( It.IsAny <Uri>(), It.IsAny <RequestOptions>(), It.IsAny <CancellationToken>())) .Callback((Uri uri, RequestOptions options, CancellationToken token) => { if (new PartitionKey(lease.Id).Equals(options.PartitionKey)) { throw DocumentExceptionHelpers.CreateNotFoundException(); // Success code path: cause lease lost. } throw new Exception("Failure"); }); this.builder .WithFeedDocumentClient(this.CreateMockDocumentClient()) .WithLeaseDocumentClient(leaseClient) .WithObserverFactory(Mock.Of <IChangeFeedObserverFactory>()); await this.builder.BuildAsync(); Exception exception = await Record.ExceptionAsync(() => this.builder.LeaseStoreManager.ReleaseAsync(lease)); Assert.Equal(typeof(LeaseLostException), exception.GetType()); }
public async Task DeleteAsync_ReturnsSuccess_IfDocumentDoesNotExist() { 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.DeleteDocumentAsync(documentUri)) .ThrowsAsync(DocumentExceptionHelpers.CreateNotFoundException()) .Verifiable(); await leaseManager.DeleteAsync(cachedLease); Mock.Get(documentClient).VerifyAll(); }
public static IChangeFeedDocumentClient SetupQueryResponseFailure( this IChangeFeedDocumentClient client, string partitionKeyRangeId, string token, Exception exception = null) { var documentQuery = Mock.Of <IChangeFeedDocumentQuery <Document> >(); Mock.Get(documentQuery) .Setup(q => q.ExecuteNextAsync <Document>(It.IsAny <CancellationToken>())) .ThrowsAsync(exception ?? DocumentExceptionHelpers.CreateNotFoundException()); Mock.Get(client) .Setup(c => c.CreateDocumentChangeFeedQuery( It.IsAny <string>(), It.Is <ChangeFeedOptions>(o => o.PartitionKeyRangeId == partitionKeyRangeId && o.RequestContinuation == token))) .Returns(documentQuery); return(client); }
public async Task UpdateLeaseAsync_ShouldThrowLostLeaseException_WhenReplaceReturnsNotFound() { Exception exception = await TestReplaceException(DocumentExceptionHelpers.CreateNotFoundException()); Assert.IsAssignableFrom <LeaseLostException>(exception); }