|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | +// |
| 4 | +using Azure; |
| 5 | +using Azure.Core.Testing; |
| 6 | +using Azure.Data.AppConfiguration; |
| 7 | +using Microsoft.Extensions.Configuration; |
| 8 | +using Microsoft.Extensions.Configuration.AzureAppConfiguration; |
| 9 | +using Moq; |
| 10 | +using System; |
| 11 | +using System.Collections.Generic; |
| 12 | +using System.Linq; |
| 13 | +using System.Threading; |
| 14 | +using Xunit; |
| 15 | + |
| 16 | +namespace Tests.AzureAppConfiguration |
| 17 | +{ |
| 18 | + public class LoadBalancingTests |
| 19 | + { |
| 20 | + readonly ConfigurationSetting kv = ConfigurationModelFactory.ConfigurationSetting(key: "TestKey1", label: "label", value: "TestValue1", |
| 21 | + eTag: new ETag("0a76e3d7-7ec1-4e37-883c-9ea6d0d89e63"), |
| 22 | + contentType: "text"); |
| 23 | + |
| 24 | + TimeSpan CacheExpirationTime = TimeSpan.FromSeconds(1); |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public void LoadBalancingTests_UsesAllEndpoints() |
| 28 | + { |
| 29 | + IConfigurationRefresher refresher = null; |
| 30 | + var mockResponse = new MockResponse(200); |
| 31 | + |
| 32 | + var mockClient1 = new Mock<ConfigurationClient>(MockBehavior.Strict); |
| 33 | + mockClient1.Setup(c => c.GetConfigurationSettingsAsync(It.IsAny<SettingSelector>(), It.IsAny<CancellationToken>())) |
| 34 | + .Returns(new MockAsyncPageable(Enumerable.Empty<ConfigurationSetting>().ToList())); |
| 35 | + mockClient1.Setup(c => c.GetConfigurationSettingAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>())) |
| 36 | + .ReturnsAsync(Response.FromValue<ConfigurationSetting>(kv, mockResponse)); |
| 37 | + mockClient1.Setup(c => c.GetConfigurationSettingAsync(It.IsAny<ConfigurationSetting>(), It.IsAny<bool>(), It.IsAny<CancellationToken>())) |
| 38 | + .ReturnsAsync(Response.FromValue(kv, mockResponse)); |
| 39 | + mockClient1.Setup(c => c.Equals(mockClient1)).Returns(true); |
| 40 | + |
| 41 | + var mockClient2 = new Mock<ConfigurationClient>(MockBehavior.Strict); |
| 42 | + mockClient2.Setup(c => c.GetConfigurationSettingsAsync(It.IsAny<SettingSelector>(), It.IsAny<CancellationToken>())) |
| 43 | + .Returns(new MockAsyncPageable(Enumerable.Empty<ConfigurationSetting>().ToList())); |
| 44 | + mockClient2.Setup(c => c.GetConfigurationSettingAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>())) |
| 45 | + .ReturnsAsync(Response.FromValue<ConfigurationSetting>(kv, mockResponse)); |
| 46 | + mockClient2.Setup(c => c.GetConfigurationSettingAsync(It.IsAny<ConfigurationSetting>(), It.IsAny<bool>(), It.IsAny<CancellationToken>())) |
| 47 | + .ReturnsAsync(Response.FromValue(kv, mockResponse)); |
| 48 | + mockClient2.Setup(c => c.Equals(mockClient2)).Returns(true); |
| 49 | + |
| 50 | + ConfigurationClientWrapper cw1 = new ConfigurationClientWrapper(TestHelpers.PrimaryConfigStoreEndpoint, mockClient1.Object); |
| 51 | + ConfigurationClientWrapper cw2 = new ConfigurationClientWrapper(TestHelpers.SecondaryConfigStoreEndpoint, mockClient2.Object); |
| 52 | + |
| 53 | + var clientList = new List<ConfigurationClientWrapper>() { cw1, cw2 }; |
| 54 | + var configClientManager = new ConfigurationClientManager(clientList); |
| 55 | + |
| 56 | + var config = new ConfigurationBuilder() |
| 57 | + .AddAzureAppConfiguration(options => |
| 58 | + { |
| 59 | + options.ClientManager = configClientManager; |
| 60 | + options.ConfigureRefresh(refreshOptions => |
| 61 | + { |
| 62 | + refreshOptions.Register("TestKey1", "label") |
| 63 | + .SetCacheExpiration(CacheExpirationTime); |
| 64 | + }); |
| 65 | + options.ReplicaDiscoveryEnabled = false; |
| 66 | + options.LoadBalancingEnabled = true; |
| 67 | + |
| 68 | + refresher = options.GetRefresher(); |
| 69 | + }).Build(); |
| 70 | + |
| 71 | + // Ensure client 1 was used for startup |
| 72 | + mockClient1.Verify(mc => mc.GetConfigurationSettingsAsync(It.IsAny<SettingSelector>(), It.IsAny<CancellationToken>()), Times.Exactly(1)); |
| 73 | + |
| 74 | + Thread.Sleep(CacheExpirationTime); |
| 75 | + refresher.RefreshAsync().Wait(); |
| 76 | + |
| 77 | + // Ensure client 2 was used for refresh |
| 78 | + mockClient1.Verify(mc => mc.GetConfigurationSettingAsync(It.IsAny<ConfigurationSetting>(), It.IsAny<bool>(), It.IsAny<CancellationToken>()), Times.Exactly(0)); |
| 79 | + |
| 80 | + mockClient2.Verify(mc => mc.GetConfigurationSettingAsync(It.IsAny<ConfigurationSetting>(), It.IsAny<bool>(), It.IsAny<CancellationToken>()), Times.Exactly(1)); |
| 81 | + |
| 82 | + Thread.Sleep(CacheExpirationTime); |
| 83 | + refresher.RefreshAsync().Wait(); |
| 84 | + |
| 85 | + // Ensure client 1 was now used for refresh |
| 86 | + mockClient1.Verify(mc => mc.GetConfigurationSettingAsync(It.IsAny<ConfigurationSetting>(), It.IsAny<bool>(), It.IsAny<CancellationToken>()), Times.Exactly(1)); |
| 87 | + } |
| 88 | + |
| 89 | + [Fact] |
| 90 | + public void LoadBalancingTests_UsesClientAfterBackoffEnds() |
| 91 | + { |
| 92 | + IConfigurationRefresher refresher = null; |
| 93 | + var mockResponse = new MockResponse(200); |
| 94 | + |
| 95 | + var mockClient1 = new Mock<ConfigurationClient>(MockBehavior.Strict); |
| 96 | + mockClient1.Setup(c => c.GetConfigurationSettingsAsync(It.IsAny<SettingSelector>(), It.IsAny<CancellationToken>())) |
| 97 | + .Throws(new RequestFailedException(503, "Request failed.")); |
| 98 | + mockClient1.Setup(c => c.GetConfigurationSettingAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>())) |
| 99 | + .ReturnsAsync(Response.FromValue<ConfigurationSetting>(kv, mockResponse)); |
| 100 | + mockClient1.Setup(c => c.GetConfigurationSettingAsync(It.IsAny<ConfigurationSetting>(), It.IsAny<bool>(), It.IsAny<CancellationToken>())) |
| 101 | + .ReturnsAsync(Response.FromValue(kv, mockResponse)); |
| 102 | + mockClient1.Setup(c => c.Equals(mockClient1)).Returns(true); |
| 103 | + |
| 104 | + var mockClient2 = new Mock<ConfigurationClient>(MockBehavior.Strict); |
| 105 | + mockClient2.Setup(c => c.GetConfigurationSettingsAsync(It.IsAny<SettingSelector>(), It.IsAny<CancellationToken>())) |
| 106 | + .Returns(new MockAsyncPageable(Enumerable.Empty<ConfigurationSetting>().ToList())); |
| 107 | + mockClient2.Setup(c => c.GetConfigurationSettingAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>())) |
| 108 | + .ReturnsAsync(Response.FromValue<ConfigurationSetting>(kv, mockResponse)); |
| 109 | + mockClient2.Setup(c => c.GetConfigurationSettingAsync(It.IsAny<ConfigurationSetting>(), It.IsAny<bool>(), It.IsAny<CancellationToken>())) |
| 110 | + .ReturnsAsync(Response.FromValue(kv, mockResponse)); |
| 111 | + mockClient2.Setup(c => c.Equals(mockClient2)).Returns(true); |
| 112 | + |
| 113 | + ConfigurationClientWrapper cw1 = new ConfigurationClientWrapper(TestHelpers.PrimaryConfigStoreEndpoint, mockClient1.Object); |
| 114 | + ConfigurationClientWrapper cw2 = new ConfigurationClientWrapper(TestHelpers.SecondaryConfigStoreEndpoint, mockClient2.Object); |
| 115 | + |
| 116 | + var clientList = new List<ConfigurationClientWrapper>() { cw1, cw2 }; |
| 117 | + var configClientManager = new ConfigurationClientManager(clientList); |
| 118 | + |
| 119 | + var config = new ConfigurationBuilder() |
| 120 | + .AddAzureAppConfiguration(options => |
| 121 | + { |
| 122 | + options.MinBackoffDuration = TimeSpan.FromSeconds(2); |
| 123 | + options.ClientManager = configClientManager; |
| 124 | + options.ConfigureRefresh(refreshOptions => |
| 125 | + { |
| 126 | + refreshOptions.Register("TestKey1", "label") |
| 127 | + .SetCacheExpiration(CacheExpirationTime); |
| 128 | + }); |
| 129 | + options.ReplicaDiscoveryEnabled = false; |
| 130 | + options.LoadBalancingEnabled = true; |
| 131 | + |
| 132 | + refresher = options.GetRefresher(); |
| 133 | + }).Build(); |
| 134 | + |
| 135 | + // Ensure client 2 was used for startup |
| 136 | + mockClient2.Verify(mc => mc.GetConfigurationSettingsAsync(It.IsAny<SettingSelector>(), It.IsAny<CancellationToken>()), Times.Exactly(1)); |
| 137 | + |
| 138 | + Thread.Sleep(TimeSpan.FromSeconds(2)); |
| 139 | + refresher.RefreshAsync().Wait(); |
| 140 | + |
| 141 | + // Ensure client 1 has recovered and is used for refresh |
| 142 | + mockClient2.Verify(mc => mc.GetConfigurationSettingAsync(It.IsAny<ConfigurationSetting>(), It.IsAny<bool>(), It.IsAny<CancellationToken>()), Times.Exactly(0)); |
| 143 | + |
| 144 | + mockClient1.Verify(mc => mc.GetConfigurationSettingAsync(It.IsAny<ConfigurationSetting>(), It.IsAny<bool>(), It.IsAny<CancellationToken>()), Times.Exactly(1)); |
| 145 | + |
| 146 | + Thread.Sleep(CacheExpirationTime); |
| 147 | + refresher.RefreshAsync().Wait(); |
| 148 | + |
| 149 | + mockClient2.Verify(mc => mc.GetConfigurationSettingAsync(It.IsAny<ConfigurationSetting>(), It.IsAny<bool>(), It.IsAny<CancellationToken>()), Times.Exactly(1)); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments