@@ -2787,20 +2787,20 @@ TEST_P(LeastRequestLoadBalancerTest, SingleHost) {
27872787
27882788 // Host weight is 1.
27892789 {
2790- EXPECT_CALL (random_, random ()).WillOnce (Return (0 )). WillOnce ( Return ( 2 )). WillOnce ( Return ( 3 )) ;
2790+ EXPECT_CALL (random_, random ()).WillOnce (Return (0 ));
27912791 EXPECT_EQ (hostSet ().healthy_hosts_ [0 ], lb_.chooseHost (nullptr ));
27922792 }
27932793
27942794 // Host weight is 100.
27952795 {
2796- EXPECT_CALL (random_, random ()).WillOnce (Return (0 )). WillOnce ( Return ( 2 )). WillOnce ( Return ( 3 )) ;
2796+ EXPECT_CALL (random_, random ()).WillOnce (Return (0 ));
27972797 EXPECT_EQ (hostSet ().healthy_hosts_ [0 ], lb_.chooseHost (nullptr ));
27982798 }
27992799
28002800 HostVector empty;
28012801 {
28022802 hostSet ().runCallbacks (empty, empty);
2803- EXPECT_CALL (random_, random ()).WillOnce (Return (0 )). WillOnce ( Return ( 2 )). WillOnce ( Return ( 3 )) ;
2803+ EXPECT_CALL (random_, random ()).WillOnce (Return (0 ));
28042804 EXPECT_EQ (hostSet ().healthy_hosts_ [0 ], lb_.chooseHost (nullptr ));
28052805 }
28062806
@@ -2823,37 +2823,44 @@ TEST_P(LeastRequestLoadBalancerTest, Normal) {
28232823
28242824 hostSet ().healthy_hosts_ [0 ]->stats ().rq_active_ .set (1 );
28252825 hostSet ().healthy_hosts_ [1 ]->stats ().rq_active_ .set (2 );
2826- EXPECT_CALL (random_, random ()).WillOnce (Return (0 )). WillOnce ( Return ( 2 )). WillOnce ( Return ( 3 )) ;
2826+ EXPECT_CALL (random_, random ()).WillOnce (Return (0 ));
28272827 EXPECT_EQ (hostSet ().healthy_hosts_ [0 ], lb_.chooseHost (nullptr ));
28282828
28292829 hostSet ().healthy_hosts_ [0 ]->stats ().rq_active_ .set (2 );
28302830 hostSet ().healthy_hosts_ [1 ]->stats ().rq_active_ .set (1 );
2831- EXPECT_CALL (random_, random ()).WillOnce (Return (0 )). WillOnce ( Return ( 2 )). WillOnce ( Return ( 3 )) ;
2831+ EXPECT_CALL (random_, random ()).WillOnce (Return (0 ));
28322832 EXPECT_EQ (hostSet ().healthy_hosts_ [1 ], lb_.chooseHost (nullptr ));
28332833}
28342834
28352835TEST_P (LeastRequestLoadBalancerTest, PNC) {
28362836 hostSet ().healthy_hosts_ = {makeTestHost (info_, " tcp://127.0.0.1:80" , simTime ()),
28372837 makeTestHost (info_, " tcp://127.0.0.1:81" , simTime ()),
28382838 makeTestHost (info_, " tcp://127.0.0.1:82" , simTime ()),
2839- makeTestHost (info_, " tcp://127.0.0.1:83" , simTime ())};
2839+ makeTestHost (info_, " tcp://127.0.0.1:83" , simTime ()),
2840+ makeTestHost (info_, " tcp://127.0.0.1:84" , simTime ())};
28402841 hostSet ().hosts_ = hostSet ().healthy_hosts_ ;
28412842 hostSet ().runCallbacks ({}, {}); // Trigger callbacks. The added/removed lists are not relevant.
28422843
28432844 hostSet ().healthy_hosts_ [0 ]->stats ().rq_active_ .set (4 );
28442845 hostSet ().healthy_hosts_ [1 ]->stats ().rq_active_ .set (3 );
28452846 hostSet ().healthy_hosts_ [2 ]->stats ().rq_active_ .set (2 );
28462847 hostSet ().healthy_hosts_ [3 ]->stats ().rq_active_ .set (1 );
2848+ hostSet ().healthy_hosts_ [4 ]->stats ().rq_active_ .set (5 );
28472849
28482850 // Creating various load balancer objects with different choice configs.
28492851 envoy::config::cluster::v3::Cluster::LeastRequestLbConfig lr_lb_config;
28502852 lr_lb_config.mutable_choice_count ()->set_value (2 );
28512853 LeastRequestLoadBalancer lb_2{priority_set_, nullptr , stats_, runtime_,
28522854 random_, common_config_, lr_lb_config, simTime ()};
2853- lr_lb_config.mutable_choice_count ()->set_value (5 );
2854- LeastRequestLoadBalancer lb_5{priority_set_, nullptr , stats_, runtime_,
2855+ lr_lb_config.mutable_choice_count ()->set_value (3 );
2856+ LeastRequestLoadBalancer lb_3{priority_set_, nullptr , stats_, runtime_,
2857+ random_, common_config_, lr_lb_config, simTime ()};
2858+ lr_lb_config.mutable_choice_count ()->set_value (4 );
2859+ LeastRequestLoadBalancer lb_4{priority_set_, nullptr , stats_, runtime_,
2860+ random_, common_config_, lr_lb_config, simTime ()};
2861+ lr_lb_config.mutable_choice_count ()->set_value (6 );
2862+ LeastRequestLoadBalancer lb_6{priority_set_, nullptr , stats_, runtime_,
28552863 random_, common_config_, lr_lb_config, simTime ()};
2856-
28572864 // Verify correct number of choices.
28582865
28592866 // 0 choices configured should default to P2C.
@@ -2864,20 +2871,78 @@ TEST_P(LeastRequestLoadBalancerTest, PNC) {
28642871 EXPECT_CALL (random_, random ()).Times (3 ).WillRepeatedly (Return (0 ));
28652872 EXPECT_EQ (hostSet ().healthy_hosts_ [0 ], lb_2.chooseHost (nullptr ));
28662873
2867- // 5 choices configured results in P5C.
2868- EXPECT_CALL (random_, random ()).Times (6 ).WillRepeatedly (Return (0 ));
2869- EXPECT_EQ (hostSet ().healthy_hosts_ [0 ], lb_5.chooseHost (nullptr ));
2870-
2871- // Verify correct host chosen in P5C scenario.
2874+ // Verify correct host chosen in P3C scenario.
28722875 EXPECT_CALL (random_, random ())
2873- .Times (6 )
2876+ .Times (4 )
28742877 .WillOnce (Return (0 ))
28752878 .WillOnce (Return (3 ))
2879+ .WillOnce (Return (1 ))
2880+ .WillOnce (Return (2 ));
2881+ EXPECT_EQ (hostSet ().healthy_hosts_ [3 ], lb_3.chooseHost (nullptr ));
2882+
2883+ // Verify correct host chosen in P4C scenario.
2884+ EXPECT_CALL (random_, random ())
2885+ .Times (5 )
28762886 .WillOnce (Return (0 ))
28772887 .WillOnce (Return (3 ))
2878- .WillOnce (Return (2 ))
2879- .WillOnce (Return (1 ));
2880- EXPECT_EQ (hostSet ().healthy_hosts_ [3 ], lb_5.chooseHost (nullptr ));
2888+ .WillOnce (Return (1 ))
2889+ .WillOnce (Return (1 ))
2890+ .WillOnce (Return (2 ));
2891+ EXPECT_EQ (hostSet ().healthy_hosts_ [3 ], lb_4.chooseHost (nullptr ));
2892+
2893+ // When the number of hosts is smaller or equal to the number of choices we don't call
2894+ // random() since we do a full table scan.
2895+ EXPECT_CALL (random_, random ()).WillOnce (Return (9999 ));
2896+ EXPECT_EQ (hostSet ().healthy_hosts_ [3 ], lb_6.chooseHost (nullptr ));
2897+ }
2898+
2899+ TEST_P (LeastRequestLoadBalancerTest, FullScan) {
2900+ hostSet ().healthy_hosts_ = {makeTestHost (info_, " tcp://127.0.0.1:80" , simTime ()),
2901+ makeTestHost (info_, " tcp://127.0.0.1:81" , simTime ()),
2902+ makeTestHost (info_, " tcp://127.0.0.1:82" , simTime ()),
2903+ makeTestHost (info_, " tcp://127.0.0.1:83" , simTime ()),
2904+ makeTestHost (info_, " tcp://127.0.0.1:84" , simTime ())};
2905+ hostSet ().hosts_ = hostSet ().healthy_hosts_ ;
2906+ hostSet ().runCallbacks ({}, {}); // Trigger callbacks. The added/removed lists are not relevant.
2907+
2908+ hostSet ().healthy_hosts_ [0 ]->stats ().rq_active_ .set (4 );
2909+ hostSet ().healthy_hosts_ [1 ]->stats ().rq_active_ .set (3 );
2910+ hostSet ().healthy_hosts_ [2 ]->stats ().rq_active_ .set (2 );
2911+ hostSet ().healthy_hosts_ [3 ]->stats ().rq_active_ .set (1 );
2912+ hostSet ().healthy_hosts_ [4 ]->stats ().rq_active_ .set (5 );
2913+
2914+ // Creating various load balancer objects with different choice configs.
2915+ envoy::extensions::load_balancing_policies::least_request::v3::LeastRequest lr_lb_config;
2916+ lr_lb_config.mutable_choice_count ()->set_value (2 );
2917+ // Enable full table scan on hosts
2918+ lr_lb_config.mutable_enable_full_scan ()->set_value (true );
2919+ common_config_.mutable_healthy_panic_threshold ()->set_value (0 );
2920+
2921+ LeastRequestLoadBalancer lb_2{priority_set_, nullptr , stats_, runtime_,
2922+ random_, 1 , lr_lb_config, simTime ()};
2923+ lr_lb_config.mutable_choice_count ()->set_value (3 );
2924+ LeastRequestLoadBalancer lb_3{priority_set_, nullptr , stats_, runtime_,
2925+ random_, 1 , lr_lb_config, simTime ()};
2926+ lr_lb_config.mutable_choice_count ()->set_value (4 );
2927+ LeastRequestLoadBalancer lb_4{priority_set_, nullptr , stats_, runtime_,
2928+ random_, 1 , lr_lb_config, simTime ()};
2929+ lr_lb_config.mutable_choice_count ()->set_value (6 );
2930+ LeastRequestLoadBalancer lb_6{priority_set_, nullptr , stats_, runtime_,
2931+ random_, 1 , lr_lb_config, simTime ()};
2932+
2933+ // random is called only once every time and is not to select the host.
2934+
2935+ EXPECT_CALL (random_, random ()).WillOnce (Return (9999 ));
2936+ EXPECT_EQ (hostSet ().healthy_hosts_ [3 ], lb_2.chooseHost (nullptr ));
2937+
2938+ EXPECT_CALL (random_, random ()).WillOnce (Return (9999 ));
2939+ EXPECT_EQ (hostSet ().healthy_hosts_ [3 ], lb_3.chooseHost (nullptr ));
2940+
2941+ EXPECT_CALL (random_, random ()).WillOnce (Return (9999 ));
2942+ EXPECT_EQ (hostSet ().healthy_hosts_ [3 ], lb_4.chooseHost (nullptr ));
2943+
2944+ EXPECT_CALL (random_, random ()).WillOnce (Return (9999 ));
2945+ EXPECT_EQ (hostSet ().healthy_hosts_ [3 ], lb_6.chooseHost (nullptr ));
28812946}
28822947
28832948TEST_P (LeastRequestLoadBalancerTest, WeightImbalance) {
0 commit comments