@@ -39,63 +39,94 @@ func Test(t *testing.T) {
3939// on a weighted SubConn, and expects certain metrics for each of these
4040// scenarios.
4141func (s ) TestWRR_Metrics_SubConnWeight (t * testing.T ) {
42- tmr := stats .NewTestMetricsRecorder (t , []string {"grpc.lb.wrr.rr_fallback" , "grpc.lb.wrr.endpoint_weight_not_yet_usable" , "grpc.lb.wrr.endpoint_weight_stale" , "grpc.lb.wrr.endpoint_weights" })
43-
44- wsc := & weightedSubConn {
45- metricsRecorder : tmr ,
46- weightVal : 3 ,
42+ tests := []struct {
43+ name string
44+ weightExpirationPeriod time.Duration
45+ blackoutPeriod time.Duration
46+ lastUpdatedSet bool
47+ nonEmptySet bool
48+ nowTime time.Time
49+ endpointWeightStaleWant float64
50+ endpointWeightNotYetUsableWant float64
51+ endpointWeightWant float64
52+ }{
53+ // The weighted SubConn's lastUpdated field hasn't been set, so this
54+ // SubConn's weight is not yet usable. Thus, should emit that endpoint
55+ // weight is not yet usable, and 0 for weight.
56+ {
57+ name : "no weight set" ,
58+ weightExpirationPeriod : time .Second ,
59+ blackoutPeriod : time .Second ,
60+ nowTime : time .Now (),
61+ endpointWeightStaleWant : 0 ,
62+ endpointWeightNotYetUsableWant : 1 ,
63+ endpointWeightWant : 0 ,
64+ },
65+ {
66+ name : "weight expiration" ,
67+ lastUpdatedSet : true ,
68+ weightExpirationPeriod : 2 * time .Second ,
69+ blackoutPeriod : time .Second ,
70+ nowTime : time .Now ().Add (100 * time .Second ),
71+ endpointWeightStaleWant : 1 ,
72+ endpointWeightNotYetUsableWant : 0 ,
73+ endpointWeightWant : 0 ,
74+ },
75+ {
76+ name : "in blackout period" ,
77+ lastUpdatedSet : true ,
78+ weightExpirationPeriod : time .Minute ,
79+ blackoutPeriod : 10 * time .Second ,
80+ nowTime : time .Now (),
81+ endpointWeightStaleWant : 0 ,
82+ endpointWeightNotYetUsableWant : 1 ,
83+ endpointWeightWant : 0 ,
84+ },
85+ {
86+ name : "normal weight" ,
87+ lastUpdatedSet : true ,
88+ nonEmptySet : true ,
89+ weightExpirationPeriod : time .Minute ,
90+ blackoutPeriod : time .Second ,
91+ nowTime : time .Now ().Add (10 * time .Second ),
92+ endpointWeightStaleWant : 0 ,
93+ endpointWeightNotYetUsableWant : 0 ,
94+ endpointWeightWant : 3 ,
95+ },
96+ {
97+ name : "weight expiration takes precdedence over blackout" ,
98+ lastUpdatedSet : true ,
99+ nonEmptySet : true ,
100+ weightExpirationPeriod : time .Second ,
101+ blackoutPeriod : time .Minute ,
102+ nowTime : time .Now ().Add (10 * time .Second ),
103+ endpointWeightStaleWant : 1 ,
104+ endpointWeightNotYetUsableWant : 0 ,
105+ endpointWeightWant : 0 ,
106+ },
47107 }
48108
49- // The weighted SubConn's lastUpdated field hasn't been set, so this
50- // SubConn's weight is not yet usable. Thus, should emit that endpoint
51- // weight is not yet usable, and 0 for weight.
52- wsc .weight (time .Now (), time .Second , time .Second , true )
53- tmr .AssertDataForMetric ("grpc.lb.wrr.endpoint_weight_stale" , 0 ) // The endpoint weight has not expired so this is 0.
54- tmr .AssertDataForMetric ("grpc.lb.wrr.endpoint_weight_not_yet_usable" , 1 )
55- // Unusable, so no endpoint weight (i.e. 0).
56- tmr .AssertDataForMetric ("grpc.lb.wrr.endpoint_weights" , 0 )
57- tmr .ClearMetrics ()
58-
59- // Setup a scenario where the SubConn's weight expires. Thus, should emit
60- // that endpoint weight is stale, and 0 for weight.
61- wsc .lastUpdated = time .Now ()
62- wsc .weight (time .Now ().Add (100 * time .Second ), 2 * time .Second , time .Second , true )
63- tmr .AssertDataForMetric ("grpc.lb.wrr.endpoint_weight_stale" , 1 )
64- tmr .AssertDataForMetric ("grpc.lb.wrr.endpoint_weight_not_yet_usable" , 0 )
65- // Unusable, so no endpoint weight (i.e. 0).
66- tmr .AssertDataForMetric ("grpc.lb.wrr.endpoint_weights" , 0 )
67- tmr .ClearMetrics ()
68-
69- // Setup a scenario where the SubConn's weight is in the blackout period.
70- // Thus, should emit that endpoint weight is not yet usable, and 0 for
71- // weight.
72- wsc .weight (time .Now (), time .Minute , 10 * time .Second , true )
73- tmr .AssertDataForMetric ("grpc.lb.wrr.endpoint_weight_stale" , 0 )
74- tmr .AssertDataForMetric ("grpc.lb.wrr.endpoint_weight_not_yet_usable" , 1 )
75- // Unusable, so no endpoint weight (i.e. 0).
76- tmr .AssertDataForMetric ("grpc.lb.wrr.endpoint_weights" , 0 )
77- tmr .ClearMetrics ()
109+ for _ , test := range tests {
110+ t .Run (test .name , func (t * testing.T ) {
111+ tmr := stats .NewTestMetricsRecorder (t , []string {"grpc.lb.wrr.rr_fallback" , "grpc.lb.wrr.endpoint_weight_not_yet_usable" , "grpc.lb.wrr.endpoint_weight_stale" , "grpc.lb.wrr.endpoint_weights" })
112+ wsc := & weightedSubConn {
113+ metricsRecorder : tmr ,
114+ weightVal : 3 ,
115+ }
116+ if test .lastUpdatedSet {
117+ wsc .lastUpdated = time .Now ()
118+ }
119+ if test .nonEmptySet {
120+ wsc .nonEmptySince = time .Now ()
121+ }
122+ wsc .weight (test .nowTime , test .weightExpirationPeriod , test .blackoutPeriod , true )
78123
79- // Setup a scenario where SubConn's weight is what is persists in weight
80- // field. This is triggered by last update being past blackout period and
81- // before weight update period. Should thus emit that endpoint weight is 3,
82- // and no other metrics.
83- wsc .nonEmptySince = time .Now ()
84- wsc .weight (time .Now ().Add (10 * time .Second ), time .Minute , time .Second , true )
85- tmr .AssertDataForMetric ("grpc.lb.wrr.endpoint_weight_stale" , 0 )
86- tmr .AssertDataForMetric ("grpc.lb.wrr.endpoint_weight_not_yet_usable" , 0 )
87- tmr .AssertDataForMetric ("grpc.lb.wrr.endpoint_weights" , 3 )
88- tmr .ClearMetrics ()
124+ tmr .AssertDataForMetric ("grpc.lb.wrr.endpoint_weight_stale" , test .endpointWeightStaleWant )
125+ tmr .AssertDataForMetric ("grpc.lb.wrr.endpoint_weight_not_yet_usable" , test .endpointWeightNotYetUsableWant )
126+ tmr .AssertDataForMetric ("grpc.lb.wrr.endpoint_weights" , test .endpointWeightWant )
127+ })
128+ }
89129
90- // Setup a scenario where a SubConn's weight both expires and is within the
91- // blackout period. In this case, weight expiry should take precedence with
92- // respect to metrics emitted. Thus, should emit that endpoint weight is not
93- // yet usable, and 0 for weight.
94- wsc .weight (time .Now ().Add (10 * time .Second ), time .Second , time .Minute , true )
95- tmr .AssertDataForMetric ("grpc.lb.wrr.endpoint_weight_stale" , 1 )
96- tmr .AssertDataForMetric ("grpc.lb.wrr.endpoint_weight_not_yet_usable" , 0 )
97- tmr .AssertDataForMetric ("grpc.lb.wrr.endpoint_weights" , 0 )
98- tmr .ClearMetrics ()
99130}
100131
101132// TestWRR_Metrics_Scheduler_RR_Fallback tests the round robin fallback metric
0 commit comments