Skip to content

Commit

Permalink
Merge pull request #168 from Kentico/feature/KCL-1222_Support_rate_li…
Browse files Browse the repository at this point in the history
…mitation

Support rate limitation
  • Loading branch information
Enngage authored Oct 7, 2019
2 parents 32b8270 + 0031116 commit f4bcf86
Show file tree
Hide file tree
Showing 32 changed files with 965 additions and 520 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
using System.Linq;
using System.Net.Http;
using System.Reactive.Linq;
using System.Threading.Tasks;
using FakeItEasy;
using Kentico.Kontent.Delivery.InlineContentItems;
using Kentico.Kontent.Delivery.ResiliencePolicy;
using Kentico.Kontent.Delivery.RetryPolicy;
using Kentico.Kontent.Delivery.StrongTyping;
using Microsoft.Extensions.Options;
using Polly;
using RichardSzalay.MockHttp;
using Xunit;

Expand Down Expand Up @@ -256,16 +256,18 @@ private IDeliveryClient GetDeliveryClient(Action mockAction)
var contentPropertyMapper = new PropertyMapper();
var contentTypeProvider = new CustomTypeProvider();
var modelProvider = new ModelProvider(contentLinkUrlResolver, contentItemsProcessor, contentTypeProvider, contentPropertyMapper);
var resiliencePolicyProvider = A.Fake<IResiliencePolicyProvider>();
A.CallTo(() => resiliencePolicyProvider.Policy)
.Returns(Policy.HandleResult<HttpResponseMessage>(result => true).RetryAsync(deliveryOptions.Value.MaxRetryAttempts));
var retryPolicy = A.Fake<IRetryPolicy>();
var retryPolicyProvider = A.Fake<IRetryPolicyProvider>();
A.CallTo(() => retryPolicyProvider.GetRetryPolicy()).Returns(retryPolicy);
A.CallTo(() => retryPolicy.ExecuteAsync(A<Func<Task<HttpResponseMessage>>>._))
.ReturnsLazily(call => call.GetArgument<Func<Task<HttpResponseMessage>>>(0)());
var client = new DeliveryClient(
deliveryOptions,
httpClient,
contentLinkUrlResolver,
null,
modelProvider,
resiliencePolicyProvider,
retryPolicyProvider,
contentTypeProvider
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using FakeItEasy;
using Kentico.Kontent.Delivery.InlineContentItems;
using Kentico.Kontent.Delivery.ResiliencePolicy;
using Kentico.Kontent.Delivery.RetryPolicy;
using Kentico.Kontent.Delivery.Tests.DependencyInjectionFrameworks.Helpers;
using RichardSzalay.MockHttp;
using Xunit;
Expand Down Expand Up @@ -49,7 +49,7 @@ public void BuildWithDeliveryOptions_ReturnsDeliveryClientWithDeliveryOptions()
public void BuildWithOptionalSteps_ReturnsDeliveryClientWithSetInstances()
{
var mockModelProvider = A.Fake<IModelProvider>();
var mockResiliencePolicyProvider = A.Fake<IResiliencePolicyProvider>();
var mockRetryPolicyProvider = A.Fake<IRetryPolicyProvider>();
var mockPropertyMapper = A.Fake<IPropertyMapper>();
var mockContentLinkUrlResolver = A.Fake<IContentLinkUrlResolver>();
var mockInlineContentItemsProcessor = A.Fake<IInlineContentItemsProcessor>();
Expand All @@ -69,7 +69,7 @@ public void BuildWithOptionalSteps_ReturnsDeliveryClientWithSetInstances()
.WithInlineContentItemsResolver(mockAnContentItemsResolver)
.WithModelProvider(mockModelProvider)
.WithPropertyMapper(mockPropertyMapper)
.WithResiliencePolicyProvider(mockResiliencePolicyProvider)
.WithRetryPolicyProvider(mockRetryPolicyProvider)
.WithTypeProvider(mockTypeProvider)
.Build();

Expand All @@ -78,7 +78,7 @@ public void BuildWithOptionalSteps_ReturnsDeliveryClientWithSetInstances()
Assert.Equal(mockInlineContentItemsProcessor, deliveryClient.InlineContentItemsProcessor);
Assert.Equal(mockModelProvider, deliveryClient.ModelProvider);
Assert.Equal(mockPropertyMapper, deliveryClient.PropertyMapper);
Assert.Equal(mockResiliencePolicyProvider, deliveryClient.ResiliencePolicyProvider);
Assert.Equal(mockRetryPolicyProvider, deliveryClient.RetryPolicyProvider);
Assert.Equal(mockTypeProvider, deliveryClient.TypeProvider);
Assert.Equal(mockHttp, deliveryClient.HttpClient);
}
Expand Down Expand Up @@ -123,7 +123,7 @@ public void BuildWithOptionalStepsAndCustomProvider_ReturnsDeliveryClientWithSet
}

[Fact]
public void BuildWithoutOptionalStepts_ReturnsDeliveryClientWithDefaultImplementations()
public void BuildWithoutOptionalSteps_ReturnsDeliveryClientWithDefaultImplementations()
{
var expectedResolvableInlineContentItemsTypes = new[]
{
Expand All @@ -143,7 +143,7 @@ public void BuildWithoutOptionalStepts_ReturnsDeliveryClientWithDefaultImplement
Assert.NotNull(deliveryClient.ContentLinkUrlResolver);
Assert.NotNull(deliveryClient.HttpClient);
Assert.NotNull(deliveryClient.InlineContentItemsProcessor);
Assert.NotNull(deliveryClient.ResiliencePolicyProvider);
Assert.NotNull(deliveryClient.RetryPolicyProvider);
Assert.Equal(expectedResolvableInlineContentItemsTypes, actualResolvableInlineContentItemTypes);
}

Expand Down Expand Up @@ -200,7 +200,7 @@ public void BuildWithOptionsAndNullResiliencePolicyProvider_ThrowsArgumentNullEx
{
var builderStep = DeliveryClientBuilder.WithProjectId(_guid);

Assert.Throws<ArgumentNullException>(() => builderStep.WithResiliencePolicyProvider(null));
Assert.Throws<ArgumentNullException>(() => builderStep.WithRetryPolicyProvider(null));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public void BuildWithProjectIdAndUseProductionApi()
var deliveryOptions = DeliveryOptionsBuilder
.CreateInstance()
.WithProjectId(ProjectId)
.UseProductionApi
.UseProductionApi()
.Build();

Assert.Equal(deliveryOptions.ProjectId, ProjectId);
Assert.False(deliveryOptions.UsePreviewApi);
Assert.False(deliveryOptions.UseSecuredProductionApi);
Assert.False(deliveryOptions.UseSecureAccess);
}

[Fact]
Expand All @@ -49,68 +49,64 @@ public void BuildWithProjectIdAndSecuredProductionApi()
var deliveryOptions = DeliveryOptionsBuilder
.CreateInstance()
.WithProjectId(ProjectId)
.UseSecuredProductionApi(SecuredApiKey)
.UseProductionApi(SecuredApiKey)
.Build();

Assert.Equal(ProjectId, deliveryOptions.ProjectId);
Assert.True(deliveryOptions.UseSecuredProductionApi);
Assert.Equal(SecuredApiKey, deliveryOptions.SecuredProductionApiKey);
Assert.True(deliveryOptions.UseSecureAccess);
Assert.Equal(SecuredApiKey, deliveryOptions.SecureAccessApiKey);
}

[Fact]
public void BuildWithMaxRetryAttempts()
public void BuildWithRetryPolicyOptions()
{
const int maxRetryAttempts = 10;
var retryOptions = new DefaultRetryPolicyOptions();

var deliveryOptions = DeliveryOptionsBuilder
.CreateInstance()
.WithProjectId(ProjectId)
.UseProductionApi
.WithMaxRetryAttempts(maxRetryAttempts)
.UseProductionApi()
.WithDefaultRetryPolicyOptions(retryOptions)
.Build();

Assert.Equal(deliveryOptions.MaxRetryAttempts, maxRetryAttempts);
Assert.Equal(deliveryOptions.DefaultRetryPolicyOptions, retryOptions);
}

[Fact]
public void BuildWithZeroMaxRetryAttemps_ResilienceLogicIsDisabled()
public void BuildWithNullRetryPolicyOptions_ThrowsException()
{
const int maxRetryAttempts = 0;

var deliveryOptions = DeliveryOptionsBuilder
Assert.Throws<ArgumentNullException>(() => DeliveryOptionsBuilder
.CreateInstance()
.WithProjectId(ProjectId)
.UseProductionApi
.WithMaxRetryAttempts(maxRetryAttempts)
.Build();

Assert.False(deliveryOptions.EnableResilienceLogic);
.UseProductionApi()
.WithDefaultRetryPolicyOptions(null)
.Build());
}

[Fact]
public void BuildWithWaitForLoadingNewContent()
public void BuildWithDisabledRetryLogic()
{
var deliveryOptions = DeliveryOptionsBuilder
.CreateInstance()
.WithProjectId(Guid.NewGuid())
.UseProductionApi
.WaitForLoadingNewContent
.UseProductionApi()
.DisableRetryPolicy()
.Build();

Assert.True(deliveryOptions.WaitForLoadingNewContent);
Assert.False(deliveryOptions.EnableRetryPolicy);
}

[Fact]
public void BuildWithDisabledResilienceLogic()
public void BuildWithWaitForLoadingNewContent()
{
var deliveryOptions = DeliveryOptionsBuilder
.CreateInstance()
.WithProjectId(Guid.NewGuid())
.UseProductionApi
.DisableResilienceLogic
.UseProductionApi()
.WaitForLoadingNewContent()
.Build();

Assert.False(deliveryOptions.EnableResilienceLogic);
Assert.True(deliveryOptions.WaitForLoadingNewContent);
}

[Fact]
Expand All @@ -136,7 +132,7 @@ public void BuildWithCustomEndpointForProductionApi()
var deliveryOptions = DeliveryOptionsBuilder
.CreateInstance()
.WithProjectId(ProjectId)
.UseProductionApi
.UseProductionApi()
.WithCustomEndpoint(customEndpoint)
.Build();

Expand Down Expand Up @@ -168,7 +164,7 @@ public void BuildWithCustomEndpointAsUriForProductionApi()
var deliveryOptions = DeliveryOptionsBuilder
.CreateInstance()
.WithProjectId(ProjectId)
.UseProductionApi
.UseProductionApi()
.WithCustomEndpoint(uri)
.Build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,77 @@ public class DeliveryOptionsValidatorTests
private readonly Guid _guid = Guid.NewGuid();

[Fact]
public void ValidateOptionsWithNegativeMaxRetryAttempts()
public void ValidateRetryOptions_NegativeDeltaBackoff_Throws()
{
var deliveryOptions = new Delivery.DeliveryOptions
{
ProjectId = _guid.ToString(),
MaxRetryAttempts = -10
DefaultRetryPolicyOptions = new DefaultRetryPolicyOptions
{
DeltaBackoff = TimeSpan.FromSeconds(-1)
}
};

Assert.Throws<ArgumentException>(() => deliveryOptions.Validate());
}

[Fact]
public void ValidateRetryOptions_ZeroDeltaBackoff_Throws()
{
var deliveryOptions = new Delivery.DeliveryOptions
{
ProjectId = _guid.ToString(),
DefaultRetryPolicyOptions = new DefaultRetryPolicyOptions
{
DeltaBackoff = TimeSpan.Zero
}
};

Assert.Throws<ArgumentException>(() => deliveryOptions.Validate());
}

[Fact]
public void ValidateRetryOptions_NegativeMaxCumulativeWaitTime_Throws()
{
var deliveryOptions = new Delivery.DeliveryOptions
{
ProjectId = _guid.ToString(),
DefaultRetryPolicyOptions = new DefaultRetryPolicyOptions
{
MaxCumulativeWaitTime = TimeSpan.FromSeconds(-1)
}
};

Assert.Throws<ArgumentException>(() => deliveryOptions.Validate());
}

[Fact]
public void ValidateRetryOptions_ZeroMaxCumulativeWaitTime_Throws()
{
var deliveryOptions = new Delivery.DeliveryOptions
{
ProjectId = _guid.ToString(),
DefaultRetryPolicyOptions = new DefaultRetryPolicyOptions
{
MaxCumulativeWaitTime = TimeSpan.Zero
}
};

Assert.Throws<ArgumentException>(() => deliveryOptions.Validate());
}

[Fact]
public void ValidateNullRetryOptions_Throws()
{
var deliveryOptions = new Delivery.DeliveryOptions
{
ProjectId = _guid.ToString(),
DefaultRetryPolicyOptions = null
};

Assert.Throws<ArgumentNullException>(() => deliveryOptions.Validate());
}

[Fact]
public void ValidateOptionsWithEmptyProjectId()
{
Expand Down Expand Up @@ -62,7 +122,7 @@ public void ValidateOptionsWithNullSecuredApiKey()
.CreateInstance()
.WithProjectId(_guid);

Assert.Throws<ArgumentNullException>(() => deliveryOptionsStep.UseSecuredProductionApi(null));
Assert.Throws<ArgumentNullException>(() => deliveryOptionsStep.UseProductionApi(null));
}

[Fact]
Expand All @@ -86,8 +146,8 @@ public void ValidateOptionsUseOfPreviewAndProductionApiSimultaneously()
ProjectId = _guid.ToString(),
UsePreviewApi = true,
PreviewApiKey = previewApiKey,
UseSecuredProductionApi = true,
SecuredProductionApiKey = productionApiKey
UseSecureAccess = true,
SecureAccessApiKey = productionApiKey
};

Assert.Throws<InvalidOperationException>(() => deliveryOptions.Validate());
Expand All @@ -111,7 +171,7 @@ public void ValidateOptionsWithEnabledSecuredApiWithSetKey()
var deliveryOptions = new Delivery.DeliveryOptions
{
ProjectId = _guid.ToString(),
UseSecuredProductionApi = true
UseSecureAccess = true
};

Assert.Throws<InvalidOperationException>(() => deliveryOptions.Validate());
Expand All @@ -126,7 +186,7 @@ public void ValidateOptionsWithInvalidEndpointFormat(string endpoint)
var deliveryOptionsSteps = DeliveryOptionsBuilder
.CreateInstance()
.WithProjectId(_guid)
.UseProductionApi;
.UseProductionApi();

Assert.Throws<ArgumentException>(() => deliveryOptionsSteps.WithCustomEndpoint(endpoint));
}
Expand All @@ -137,7 +197,7 @@ public void ValidateOptionsWithNullUriEndpoint()
var deliveryOptionsSteps = DeliveryOptionsBuilder
.CreateInstance()
.WithProjectId(_guid)
.UseProductionApi;
.UseProductionApi();

Assert.Throws<ArgumentNullException>(() => deliveryOptionsSteps.WithCustomEndpoint((Uri)null));
}
Expand All @@ -149,7 +209,7 @@ public void ValidateOptionsWithUriEndpointWrongScheme()
var deliveryOptionsSteps = DeliveryOptionsBuilder
.CreateInstance()
.WithProjectId(_guid)
.UseProductionApi;
.UseProductionApi();

Assert.Throws<ArgumentException>(() => deliveryOptionsSteps.WithCustomEndpoint(incorrectSchemeUri));
}
Expand All @@ -161,7 +221,7 @@ public void ValidateOptionsWithRelativeUriEndpoint()
var deliveryOptionsSteps = DeliveryOptionsBuilder
.CreateInstance()
.WithProjectId(_guid)
.UseProductionApi;
.UseProductionApi();

Assert.Throws<ArgumentException>(() => deliveryOptionsSteps.WithCustomEndpoint(relativeUri));
}
Expand Down
4 changes: 2 additions & 2 deletions Kentico.Kontent.Delivery.Tests/ContentLinkResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System;
using System.IO;
using Kentico.Kontent.Delivery.ContentLinks;
using Kentico.Kontent.Delivery.ResiliencePolicy;
using Kentico.Kontent.Delivery.RetryPolicy;
using Kentico.Kontent.Delivery.StrongTyping;
using Kentico.Kontent.Delivery.Tests.Factories;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -124,7 +124,7 @@ public async void ResolveLinksInStronglyTypedModel()

var deliveryOptions = Options.Create(new DeliveryOptions { ProjectId = guid });
var httpClient = mockHttp.ToHttpClient();
var resiliencePolicyProvider = new DefaultResiliencePolicyProvider(deliveryOptions);
var resiliencePolicyProvider = new DefaultRetryPolicyProvider(deliveryOptions);
var contentLinkUrlResolver = new CustomContentLinkUrlResolver();
var contentItemsProcessor = InlineContentItemsProcessorFactory.Create();
var modelProvider= new ModelProvider(contentLinkUrlResolver, contentItemsProcessor, new CustomTypeProvider(), new PropertyMapper());
Expand Down
Loading

0 comments on commit f4bcf86

Please sign in to comment.