public void UpdateServerList_FirstTimeNotPing_Test() { int serverCount = 10; IConfiguration configuration = ObjectFactory.CreateAppSettingConfiguration(); var configurationSource = ObjectFactory.CreateDefaultConfigurationSource(0, "App.config", configuration); var configurationManager = ObjectFactory.CreateDefaultConfigurationManager(configurationSource); List <Server> servers = new List <Server>(); for (int i = 0; i < serverCount; i++) { var server = new Server("Server_" + i, null); servers.Add(server); } LoadBalancerRouteConfig routeConfig = new LoadBalancerRouteConfig("default", 1, false); LoadBalancerRequestConfig requestConfig = new LoadBalancerRequestConfig(new List <LoadBalancerRouteConfig>() { routeConfig }); int pingCount = 0; var ping = new PredicatePing(server => { ++pingCount; return(true); }); var serverSource = new DefaultDynamicServerSource(servers); var factory = LoadBalancerManager.GetManager("soa", new LoadBalancerManagerConfig(configurationManager)); var loadBalancerConfig = new LoadBalancerConfig(ping, serverSource); var loadBalancer = factory.GetLoadBalancer(MethodInfo.GetCurrentMethod().Name, loadBalancerConfig); var defaultLoadBalancer = ((DefaultLoadBalancer)loadBalancer).LoadBalancerContext; Assert.AreEqual(0, pingCount); }
public void RoundRobinRuleWithUnavailableServerTest() { int serverCount = 10; int repeatTimes = 5; int threadCount = 1; int availableCount = 0; string loadBalancerId = MethodInfo.GetCurrentMethod().Name; IConfiguration configuration = ObjectFactory.CreateAppSettingConfiguration(); var configurationSource = ObjectFactory.CreateDefaultConfigurationSource(0, "App.config", configuration); var configurationManager = ObjectFactory.CreateDefaultConfigurationManager(configurationSource); var urlCountMap = new ConcurrentDictionary <Server, AtomicInteger>(); List <Server> servers = new List <Server>(); for (int i = 0; i < serverCount; i++) { var metadata = new Dictionary <string, string>(); metadata["Index"] = i.ToString(); var server = new Server("Server_" + i, i % 2 == 0, metadata); servers.Add(server); urlCountMap[server] = 0; if (server.IsAlive) { availableCount++; } } var routeConfig = new LoadBalancerRouteConfig("default", 1, false); var routeConfigs = new LoadBalancerRequestConfig(new List <LoadBalancerRouteConfig>() { routeConfig }); var ping = new PredicatePing(server => int.Parse(server.Metadata["Index"]) % 2 == 0); var serverSource = new DefaultDynamicServerSource(servers); var factory = LoadBalancerManager.GetManager("soa", new LoadBalancerManagerConfig(configurationManager)); var loadBalancerConfig = new LoadBalancerConfig(ping, serverSource); var loadBalancer = factory.GetLoadBalancer(loadBalancerId, loadBalancerConfig); Action action = () => { for (int i = 0; i < availableCount * repeatTimes; i++) { urlCountMap[loadBalancer.GetRequestContext(routeConfigs).Server].IncrementAndGet(); } }; List <Task> tasks = new List <Task>(); for (int i = 0; i < threadCount; i++) { tasks.Add(Task.Factory.StartNew(action)); } Task.WaitAll(tasks.ToArray()); foreach (var item in urlCountMap) { int index = int.Parse(item.Key.Metadata["Index"]); if (ping.IsAlive(item.Key)) { Assert.AreEqual(repeatTimes * threadCount, (int)item.Value); } else { Assert.AreEqual(0, (int)item.Value); } } }
public void UpdateServerList_RetainStatus_Test() { IConfiguration configuration = ObjectFactory.CreateAppSettingConfiguration(); var configurationSource = ObjectFactory.CreateDefaultConfigurationSource(0, "App.config", configuration); var configurationManager = ObjectFactory.CreateDefaultConfigurationManager(configurationSource); int serverCount = 10; string managerId = "soa"; string loadBalancerId = MethodInfo.GetCurrentMethod().Name; List <Server> servers = new List <Server>(); for (int i = 0; i < serverCount; i++) { servers.Add(new Server("Server_" + i, i % 2 == 0, new Dictionary <string, string>() { { "Index", i.ToString() } })); } LoadBalancerRouteConfig routeConfig = new LoadBalancerRouteConfig("default", 1, false); LoadBalancerRequestConfig requestConfig = new LoadBalancerRequestConfig(new List <LoadBalancerRouteConfig>() { routeConfig }); var ping = new PredicatePing(server => { int index = int.Parse(server.Metadata["Index"]); return(index >= serverCount || index % 2 == 0); }); var serverSource = new DefaultDynamicServerSource(servers); var factory = LoadBalancerManager.GetManager(managerId, new LoadBalancerManagerConfig(configurationManager)); var loadBalancerConfig = new LoadBalancerConfig(ping, serverSource); var loadBalancer = factory.GetLoadBalancer(loadBalancerId, loadBalancerConfig); var loadBalancerContext = ((DefaultLoadBalancer)loadBalancer).LoadBalancerContext; List <Server> newServers = new List <Server>(); for (int i = 0; i < serverCount * 2; i++) { var metadata = new Dictionary <string, string>() { { "Index", i.ToString() }, { "Hello", "World" } }; newServers.Add(new Server("Server_" + i, metadata)); } serverSource.LoadBalancerRoutes = new DefaultDynamicServerSource(newServers).LoadBalancerRoutes; Assert.AreEqual(serverCount * 2, loadBalancerContext.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).Servers.Length); foreach (var server in loadBalancerContext.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).Servers) { int index = int.Parse(server.Metadata["Index"]); Assert.AreEqual(2, server.Metadata.Count); if (index < serverCount) { Assert.AreEqual(index % 2 == 0, server.IsAlive, index.ToString()); } else { Assert.IsTrue(server.IsAlive); } } }