|
| 1 | +// ------------------------------------------------------------------------------------------------- |
| 2 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. |
| 4 | +// ------------------------------------------------------------------------------------------------- |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Diagnostics; |
| 8 | +using System.Net; |
| 9 | +using System.Threading; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using Microsoft.Azure.Cosmos; |
| 12 | +using Microsoft.Extensions.Logging.Abstractions; |
| 13 | +using Microsoft.Health.Abstractions.Exceptions; |
| 14 | +using Microsoft.Health.Core.Features.Context; |
| 15 | +using Microsoft.Health.Extensions.DependencyInjection; |
| 16 | +using Microsoft.Health.Fhir.Core.Features.Context; |
| 17 | +using Microsoft.Health.Fhir.CosmosDb.Core.Configs; |
| 18 | +using Microsoft.Health.Fhir.CosmosDb.Core.Features.Storage; |
| 19 | +using Microsoft.Health.Fhir.CosmosDb.Features.Queries; |
| 20 | +using Microsoft.Health.Fhir.CosmosDb.Features.Storage; |
| 21 | +using Microsoft.Health.Fhir.CosmosDb.Features.Storage.Queues; |
| 22 | +using Microsoft.Health.Fhir.Tests.Common; |
| 23 | +using Microsoft.Health.Test.Utilities; |
| 24 | +using NSubstitute; |
| 25 | +using Xunit; |
| 26 | + |
| 27 | +namespace Microsoft.Health.Fhir.CosmosDb.UnitTests.Features.Storage.Queues; |
| 28 | + |
| 29 | +[Trait(Traits.OwningTeam, OwningTeam.Fhir)] |
| 30 | +[Trait(Traits.Category, Categories.DataSourceValidation)] |
| 31 | +public class CosmosQueueClientTests |
| 32 | +{ |
| 33 | + private readonly ICosmosQueryFactory _cosmosQueryFactory; |
| 34 | + private readonly ICosmosDbDistributedLockFactory _distributedLockFactory; |
| 35 | + private readonly CosmosDataStoreConfiguration _cosmosDataStoreConfiguration = new CosmosDataStoreConfiguration(); |
| 36 | + private readonly RequestContextAccessor<IFhirRequestContext> _requestContextAccessor; |
| 37 | + private readonly RetryExceptionPolicyFactory _retryPolicyFactory; |
| 38 | + private readonly CosmosQueueClient _cosmosQueueClient; |
| 39 | + |
| 40 | + public CosmosQueueClientTests() |
| 41 | + { |
| 42 | + _cosmosQueryFactory = Substitute.For<ICosmosQueryFactory>(); |
| 43 | + _distributedLockFactory = Substitute.For<ICosmosDbDistributedLockFactory>(); |
| 44 | + _requestContextAccessor = Substitute.For<RequestContextAccessor<IFhirRequestContext>>(); |
| 45 | + _retryPolicyFactory = new RetryExceptionPolicyFactory(_cosmosDataStoreConfiguration, _requestContextAccessor, NullLogger<RetryExceptionPolicyFactory>.Instance); |
| 46 | + |
| 47 | + _cosmosQueueClient = new CosmosQueueClient( |
| 48 | + Substitute.For<Func<IScoped<Container>>>(), |
| 49 | + _cosmosQueryFactory, |
| 50 | + _distributedLockFactory, |
| 51 | + _retryPolicyFactory); |
| 52 | + } |
| 53 | + |
| 54 | + [Theory] |
| 55 | + [InlineData(HttpStatusCode.ServiceUnavailable)] |
| 56 | + [InlineData(HttpStatusCode.TooManyRequests)] |
| 57 | + [InlineData(HttpStatusCode.Gone)] |
| 58 | + [InlineData((HttpStatusCode)449)] |
| 59 | + [InlineData(HttpStatusCode.RequestTimeout)] |
| 60 | + public async Task GivenADequeueJobOperation_WhenExceptionOccurs_RetryWillHappen(HttpStatusCode statusCode) |
| 61 | + { |
| 62 | + // Arrange |
| 63 | + ICosmosQuery<JobGroupWrapper> cosmosQuery = Substitute.For<ICosmosQuery<JobGroupWrapper>>(); |
| 64 | + _cosmosQueryFactory.Create<JobGroupWrapper>(Arg.Any<Container>(), Arg.Any<CosmosQueryContext>()) |
| 65 | + .ReturnsForAnyArgs(cosmosQuery); |
| 66 | + |
| 67 | + int callCount = 0; |
| 68 | + cosmosQuery.ExecuteNextAsync(Arg.Any<CancellationToken>()).ReturnsForAnyArgs(_ => |
| 69 | + { |
| 70 | + if (callCount++ == 0) |
| 71 | + { |
| 72 | + throw new TestCosmosException(statusCode); |
| 73 | + } |
| 74 | + |
| 75 | + return Task.FromResult(Substitute.For<FeedResponse<JobGroupWrapper>>()); |
| 76 | + }); |
| 77 | + |
| 78 | + // Act |
| 79 | + await _cosmosQueueClient.DequeueAsync(0, "testworker", 10, CancellationToken.None); |
| 80 | + |
| 81 | + // Assert |
| 82 | + Assert.Equal(2, callCount); |
| 83 | + await cosmosQuery.ReceivedWithAnyArgs(2).ExecuteNextAsync(Arg.Any<CancellationToken>()); |
| 84 | + } |
| 85 | + |
| 86 | + [Theory] |
| 87 | + [InlineData(typeof(CosmosException))] |
| 88 | + [InlineData(typeof(RequestRateExceededException))] |
| 89 | + public async Task GivenADequeueJobOperation_WhenExceptionWithRetryAfterIsProvided_PolicyRespectsRetryAfter(Type exceptionType) |
| 90 | + { |
| 91 | + // Arrange |
| 92 | + ICosmosQuery<JobGroupWrapper> cosmosQuery = Substitute.For<ICosmosQuery<JobGroupWrapper>>(); |
| 93 | + _cosmosQueryFactory.Create<JobGroupWrapper>(Arg.Any<Container>(), Arg.Any<CosmosQueryContext>()) |
| 94 | + .ReturnsForAnyArgs(cosmosQuery); |
| 95 | + var retryAfter = TimeSpan.FromSeconds(2); |
| 96 | + int callCount = 0; |
| 97 | + |
| 98 | + cosmosQuery.ExecuteNextAsync(Arg.Any<CancellationToken>()).ReturnsForAnyArgs(_ => |
| 99 | + { |
| 100 | + if (callCount++ == 0) |
| 101 | + { |
| 102 | + throw exceptionType == typeof(CosmosException) |
| 103 | + ? new TestCosmosException(HttpStatusCode.TooManyRequests, retryAfter) |
| 104 | + : new RequestRateExceededException(retryAfter); |
| 105 | + } |
| 106 | + |
| 107 | + return Task.FromResult(Substitute.For<FeedResponse<JobGroupWrapper>>()); |
| 108 | + }); |
| 109 | + |
| 110 | + var stopwatch = Stopwatch.StartNew(); |
| 111 | + |
| 112 | + // Act |
| 113 | + await _cosmosQueueClient.DequeueAsync(0, "testworker", 10, CancellationToken.None); |
| 114 | + |
| 115 | + stopwatch.Stop(); |
| 116 | + |
| 117 | + // Assert |
| 118 | + Assert.Equal(2, callCount); |
| 119 | + await cosmosQuery.ReceivedWithAnyArgs(2).ExecuteNextAsync(Arg.Any<CancellationToken>()); |
| 120 | + |
| 121 | + // Allowing small imprecision due to timer resolution |
| 122 | + var actualElapsedSeconds = stopwatch.Elapsed.TotalSeconds; |
| 123 | + Assert.True( |
| 124 | + Math.Abs(actualElapsedSeconds - retryAfter.TotalSeconds) <= 0.5, |
| 125 | + $"Expected retry after {retryAfter.TotalSeconds} seconds, but actual elapsed time was {actualElapsedSeconds} seconds."); |
| 126 | + } |
| 127 | + |
| 128 | + public class TestCosmosException : CosmosException |
| 129 | + { |
| 130 | + private readonly TimeSpan? _retryAfter; |
| 131 | + |
| 132 | + public TestCosmosException(HttpStatusCode statusCode, TimeSpan? retryAfter = null) |
| 133 | + : base("Test exception message", statusCode, 0, "test-activity-id", 0.0) |
| 134 | + { |
| 135 | + _retryAfter = retryAfter; |
| 136 | + } |
| 137 | + |
| 138 | + public override TimeSpan? RetryAfter => _retryAfter; |
| 139 | + } |
| 140 | +} |
0 commit comments