コード例 #1
0
        public void Random_All_Down()
        {
            var instances = new List <IServiceInstance>
            {
                new ServiceInstance("test", 80)
                {
                    Weight = 1, ServiceState = ServiceState.Down
                },
                new ServiceInstance("test", 81)
                {
                    Weight = 2, ServiceState = ServiceState.Down
                },
                new ServiceInstance("test", 82)
                {
                    Weight = 3, ServiceState = ServiceState.Down
                },
            };
            var record = new InstanceCacheRecord();

            foreach (var ins in instances)
            {
                record.InstanceMap.TryAdd(Guid.NewGuid(), ins);
            }

            var balancer = new RandomBalancer(null, record);

            Assert.Null(balancer.Pick());
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: itadapter/TestRocket
        static void Main(string[] args)
        {
            /*
             * A Singleton represents a single instance in our application.
             */

            hr();
            con("Prepare for Launch!");
            hr();

            hr();
            con("Gathering 30 randomly distributed resources...");
            hr();

            var rb = RandomBalancer.GetBalancer();

            for (int i = 0; i < 30; i++)
            {
                var r = rb.NextResource;
                con(string.Format("Connecting to resource {0} at {1}", r.Name, r.Target));
            }

            con("Press any key to abort mission!");
            Console.ReadKey(true);
        }
コード例 #3
0
        public void Random()
        {
            var instances = new List <IServiceInstance>
            {
                new ServiceInstance("test", 80)
                {
                    Weight = 1
                },
                new ServiceInstance("test", 81)
                {
                    Weight = 2
                },
                new ServiceInstance("test", 82)
                {
                    Weight = 3
                },
            };
            var record = new InstanceCacheRecord();

            foreach (var ins in instances)
            {
                record.InstanceMap.TryAdd(Guid.NewGuid(), ins);
            }

            var ports = default(IEnumerable <int>);

            var balancer = new RandomBalancer(null, record);

            {
                ports = Enumerable.Range(0, 600).Select(_ => balancer.Pick().Instance.Port).ToList();
                var x80 = ports.Count(p => p == 80);
                var x81 = ports.Count(p => p == 81);
                var x82 = ports.Count(p => p == 82);
                Assert.True(100 * 0.8 < x80 && x80 < 100 * 1.2);
                Assert.True(200 * 0.8 < x81 && x81 < 200 * 1.2);
                Assert.True(300 * 0.8 < x82 && x82 < 300 * 1.2);
            }

            (instances[0] as ServiceInstance).ServiceState = ServiceState.Down;
            {
                ports = Enumerable.Range(0, 600).Select(_ => balancer.Pick().Instance.Port).ToList();
                var x80 = ports.Count(p => p == 80);
                var x81 = ports.Count(p => p == 81);
                var x82 = ports.Count(p => p == 82);
                Assert.Equal(0, x80);
                Assert.True(240 * 0.8 < x81 && x81 < 240 * 1.2);
                Assert.True(360 * 0.8 < x82 && x82 < 360 * 1.2);
            }

            (instances[0] as ServiceInstance).ServiceState = ServiceState.Up;
            {
                ports = Enumerable.Range(0, 600).Select(_ => balancer.Pick().Instance.Port).ToList();
                var x80 = ports.Count(p => p == 80);
                var x81 = ports.Count(p => p == 81);
                var x82 = ports.Count(p => p == 82);
                Assert.True(100 * 0.8 < x80 && x80 < 100 * 1.2);
                Assert.True(200 * 0.8 < x81 && x81 < 200 * 1.2);
                Assert.True(300 * 0.8 < x82 && x82 < 300 * 1.2);
            }

            (instances[0] as ServiceInstance).ServiceState = ServiceState.Down;
            (instances[1] as ServiceInstance).ServiceState = ServiceState.Down;
            {
                ports = Enumerable.Range(0, 600).Select(_ => balancer.Pick().Instance.Port).ToList();
                var x80 = ports.Count(p => p == 80);
                var x81 = ports.Count(p => p == 81);
                var x82 = ports.Count(p => p == 82);
                Assert.Equal(0, x80);
                Assert.Equal(0, x81);
                Assert.Equal(600, x82);
            }
        }