-
-
Notifications
You must be signed in to change notification settings - Fork 1
Add optional parameters to the ServiceCollection extensions #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| using Amazon.DynamoDBv2; | ||
| using Amazon.Extensions.NETCore.Setup; | ||
| using Amazon.Runtime; | ||
| using DynamoDb.DistributedLock.Extensions; | ||
| using DynamoDb.DistributedLock.Tests.TestKit.Attributes; | ||
| using AwesomeAssertions; | ||
|
|
@@ -120,4 +122,39 @@ public void AddDynamoDbDistributedLock_WithConfiguration_BindsCustomKeyAttribute | |
| options.PartitionKeyAttribute.Should().Be(partitionKey); | ||
| options.SortKeyAttribute.Should().Be(sortKey); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AddDynamoDbDistributedLock_WithActionAndAwsConfig_SetsUpServiceAndOptions() | ||
| { | ||
| // Arrange | ||
| var services = new ServiceCollection(); | ||
| var awsOptions = new AWSOptions | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The unit tests only cover the overload that passes in the object, not the config section, because in the other tests you override the dynamodb service in order to bypass credential resolution. I couldn't figure out a good way to make all that work outside of setting fake aws env vars, and if I did the same thing of registering a substitute, it wouldn't actually test anything. So to increase coverage you could set aws access keys and secret env vars, but that makes me feel gross in tests so I didn't. |
||
| { | ||
| DefaultClientConfig = { ServiceURL = "http://localhost/" }, | ||
| // to allow the service client to be resolved without actual AWS credentials | ||
| Credentials = new AnonymousAWSCredentials(), | ||
| }; | ||
|
|
||
| // Act | ||
| services.AddDynamoDbDistributedLock(options => | ||
| { | ||
| options.TableName = "locks"; | ||
| options.LockTimeoutSeconds = 45; | ||
| }, awsOptions); | ||
|
|
||
| var provider = services.BuildServiceProvider(); | ||
|
|
||
| // Assert | ||
| var lockService = provider.GetService<IDynamoDbDistributedLock>(); | ||
| lockService.Should().NotBeNull(); | ||
|
|
||
| var options = provider.GetRequiredService<IOptions<DynamoDbLockOptions>>().Value; | ||
| options.TableName.Should().Be("locks"); | ||
| options.LockTimeoutSeconds.Should().Be(45); | ||
| options.PartitionKeyAttribute.Should().Be("pk"); | ||
| options.SortKeyAttribute.Should().Be("sk"); | ||
|
|
||
| var dynamoDbClient = provider.GetRequiredService<IAmazonDynamoDB>(); | ||
| dynamoDbClient.Config.ServiceURL.Should().Be("http://localhost/"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,13 +50,11 @@ public async Task ExecuteAsync_WhenOperationSucceedsOnFirstAttempt_ShouldReturnR | |
| var sut = new ExponentialBackoffRetryPolicy(options); | ||
| var operationCalled = 0; | ||
|
|
||
| var result = await sut.ExecuteAsync( | ||
| _ => | ||
| var result = await sut.ExecuteAsync(_ => | ||
| { | ||
| operationCalled++; | ||
| return Task.FromResult(expectedResult); | ||
| }, | ||
| _ => true); | ||
| }, _ => true, TestContext.Current.CancellationToken); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. xunit analyzer was warning about not using a cancellation token here, and recommended this one, so that if you cancel tests that hang they will have a better chance of failing fast and gracefully. This does slightly change the code paths being hit, but seems worth it.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π₯Good catch! |
||
|
|
||
| result.Should().Be(expectedResult); | ||
| operationCalled.Should().Be(1); | ||
|
|
@@ -120,15 +118,13 @@ public async Task ExecuteAsync_WhenOperationSucceedsAfterRetries_ShouldReturnRes | |
| var sut = new ExponentialBackoffRetryPolicy(options); | ||
| var operationCalled = 0; | ||
|
|
||
| var result = await sut.ExecuteAsync( | ||
| _ => | ||
| var result = await sut.ExecuteAsync(_ => | ||
| { | ||
| operationCalled++; | ||
| if (operationCalled < 3) | ||
| throw new InvalidOperationException("Retry me"); | ||
| return Task.FromResult(expectedResult); | ||
| }, | ||
| _ => true); | ||
| }, _ => true, TestContext.Current.CancellationToken); | ||
|
|
||
| result.Should().Be(expectedResult); | ||
| operationCalled.Should().Be(3); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding these optional parameters breaks binary compatibility, but because this has an existing optional param of the same type, I couldn't think of a way to do the trick of manually making another set of methods, because they are ambiguous and one gets hidden.
I think this is probably OK, since in my experience binary breaks are not as big of a deal in modern dotnet + nuget, and if you did hit this by copying some old dll, you would see it immediately in startup.