|
3 | 3 |
|
4 | 4 | using System;
|
5 | 5 | using System.Collections.Generic;
|
| 6 | +using System.Linq; |
6 | 7 | using System.Net;
|
7 | 8 | using System.Net.Http;
|
8 | 9 | using System.Threading;
|
@@ -522,6 +523,63 @@ public void AddPolicyHandlerFromRegistry_WithConfigureDelegate_AddsPolicyRegistr
|
522 | 523 | Assert.Same(registry, services.GetService<IReadOnlyPolicyRegistry<string>>());
|
523 | 524 | }
|
524 | 525 |
|
| 526 | + [Fact] |
| 527 | + public void AddPolicyRegistry_DoesNotOverrideOrAddExtraRegistrations() |
| 528 | + { |
| 529 | + // Arrange |
| 530 | + var serviceCollection = new ServiceCollection(); |
| 531 | + |
| 532 | + // Act 1 |
| 533 | + var existingRegistry = serviceCollection.AddPolicyRegistry(); |
| 534 | + |
| 535 | + // Act 2 |
| 536 | + var registry = serviceCollection.AddPolicyRegistry(); |
| 537 | + var services = serviceCollection.BuildServiceProvider(); |
| 538 | + |
| 539 | + // Assert |
| 540 | + Assert.NotNull(existingRegistry); |
| 541 | + Assert.Same(existingRegistry, registry); |
| 542 | + |
| 543 | + Assert.Same(existingRegistry, services.GetService<IPolicyRegistry<string>>()); |
| 544 | + Assert.Same(existingRegistry, services.GetService<IConcurrentPolicyRegistry<string>>()); |
| 545 | + Assert.Same(existingRegistry, services.GetService<IReadOnlyPolicyRegistry<string>>()); |
| 546 | + |
| 547 | + Assert.Single(serviceCollection, sd => sd.ServiceType == typeof(IPolicyRegistry<string>)); |
| 548 | + Assert.Single(serviceCollection, sd => sd.ServiceType == typeof(IReadOnlyPolicyRegistry<string>)); |
| 549 | + Assert.Single(serviceCollection, sd => sd.ServiceType == typeof(IConcurrentPolicyRegistry<string>)); |
| 550 | + } |
| 551 | + |
| 552 | + [Theory] |
| 553 | + [InlineData(typeof(IPolicyRegistry<string>))] |
| 554 | + [InlineData(typeof(IReadOnlyPolicyRegistry<string>))] |
| 555 | + [InlineData(typeof(IConcurrentPolicyRegistry<string>))] |
| 556 | + public void AddPolicyRegistry_AddsOnlyMissingRegistrations(Type missingType) |
| 557 | + { |
| 558 | + // Arrange |
| 559 | + var serviceCollection = new ServiceCollection(); |
| 560 | + var registry = new PolicyRegistry(); |
| 561 | + var policyTypes = new List<Type> |
| 562 | + { |
| 563 | + typeof(IPolicyRegistry<string>), |
| 564 | + typeof(IReadOnlyPolicyRegistry<string>), |
| 565 | + typeof(IConcurrentPolicyRegistry<string>) |
| 566 | + }; |
| 567 | + |
| 568 | + // Act 1 |
| 569 | + foreach (var policyType in policyTypes.Where(x => x != missingType)) |
| 570 | + { |
| 571 | + serviceCollection.AddSingleton(policyType, registry); |
| 572 | + } |
| 573 | + |
| 574 | + // Act 2 |
| 575 | + serviceCollection.AddPolicyRegistry(); |
| 576 | + |
| 577 | + // Assert |
| 578 | + Assert.Single(serviceCollection, sd => sd.ServiceType == typeof(IPolicyRegistry<string>)); |
| 579 | + Assert.Single(serviceCollection, sd => sd.ServiceType == typeof(IReadOnlyPolicyRegistry<string>)); |
| 580 | + Assert.Single(serviceCollection, sd => sd.ServiceType == typeof(IConcurrentPolicyRegistry<string>)); |
| 581 | + } |
| 582 | + |
525 | 583 | // Throws an exception or fails on even numbered requests, otherwise succeeds.
|
526 | 584 | private class FaultyMessageHandler : DelegatingHandler
|
527 | 585 | {
|
|
0 commit comments