Пример #1
0
        public async Task ReleaseInitializationLockAsync_PassesOverETag()
        {
            var etag = "etag";

            var client = Mock.Of <IChangeFeedDocumentClient>();

            Mock.Get(client)
            .Setup(c => c.CreateDocumentAsync(leaseCollectionLink, It.IsAny <object>(), null, false, default(CancellationToken)))
            .ReturnsAsync(new ResourceResponse <Document>(JsonConvert.DeserializeObject <Document>($"{{\"_etag\":\"{etag}\"}}")));
            Mock.Get(client)
            .Setup(c => c.DeleteDocumentAsync(It.IsAny <Uri>(), It.IsAny <RequestOptions>(), default(CancellationToken)))
            .ReturnsAsync(new ResourceResponse <Document>(new Document()));

            var leaseStore = new DocumentServiceLeaseStore(client, collectionInfo, containerNamePrefix, leaseCollectionLink, Mock.Of <IRequestOptionsFactory>());

            bool isLockAcquired = await leaseStore.AcquireInitializationLockAsync(lockTime);

            Assert.True(isLockAcquired);

            bool isLockFoundAndReleased = await leaseStore.ReleaseInitializationLockAsync();

            Assert.True(isLockFoundAndReleased);

            Mock.Get(client)
            .Verify(c => c.DeleteDocumentAsync(
                        It.IsAny <Uri>(),
                        It.Is <RequestOptions>(options => options.AccessCondition.Type == AccessConditionType.IfMatch && options.AccessCondition.Condition == etag),
                        default(CancellationToken)),
                    Times.Once);
        }
Пример #2
0
        public async Task AcquireInitializationLockAsync_ShouldThrow_IfLockThrows()
        {
            var client = Mock.Of <IChangeFeedDocumentClient>();

            Mock.Get(client)
            .Setup(c => c.CreateDocumentAsync(It.IsAny <string>(), It.IsAny <object>(), null, false, default(CancellationToken)))
            .ThrowsAsync(DocumentExceptionHelpers.CreateRequestRateTooLargeException());

            var       leaseStore = new DocumentServiceLeaseStore(client, collectionInfo, containerNamePrefix, leaseCollectionLink, Mock.Of <IRequestOptionsFactory>());
            Exception exception  = await Record.ExceptionAsync(() => leaseStore.AcquireInitializationLockAsync(lockTime));

            Assert.IsAssignableFrom <DocumentClientException>(exception);
        }
Пример #3
0
        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);
        }