16
16
package com .alibaba .csp .sentinel .slots .statistic .metric ;
17
17
18
18
import java .util .ArrayList ;
19
+ import java .util .Arrays ;
20
+ import java .util .List ;
19
21
22
+ import com .alibaba .csp .sentinel .node .metric .MetricNode ;
23
+ import com .alibaba .csp .sentinel .slots .statistic .MetricEvent ;
20
24
import com .alibaba .csp .sentinel .slots .statistic .base .WindowWrap ;
21
25
import com .alibaba .csp .sentinel .slots .statistic .data .MetricBucket ;
26
+ import com .alibaba .csp .sentinel .util .function .Predicate ;
22
27
23
28
import org .junit .Test ;
24
29
@@ -38,7 +43,8 @@ public class ArrayMetricTest {
38
43
@ Test
39
44
public void testOperateArrayMetric () {
40
45
BucketLeapArray leapArray = mock (BucketLeapArray .class );
41
- final WindowWrap <MetricBucket > windowWrap = new WindowWrap <MetricBucket >(windowLengthInMs , 0 , new MetricBucket ());
46
+ final WindowWrap <MetricBucket > windowWrap = new WindowWrap <MetricBucket >(windowLengthInMs , 0 ,
47
+ new MetricBucket ());
42
48
when (leapArray .currentWindow ()).thenReturn (windowWrap );
43
49
when (leapArray .values ()).thenReturn (new ArrayList <MetricBucket >() {{ add (windowWrap .value ()); }});
44
50
@@ -70,4 +76,45 @@ public void testOperateArrayMetric() {
70
76
assertEquals (expectedException , metric .exception ());
71
77
assertEquals (expectedRt , metric .rt ());
72
78
}
73
- }
79
+
80
+ @ Test
81
+ public void testGetMetricDetailsOnCondition () {
82
+ BucketLeapArray leapArray = mock (BucketLeapArray .class );
83
+ // Mock interval=2s, sampleCount=2
84
+ final WindowWrap <MetricBucket > w1 = new WindowWrap <>(windowLengthInMs , 500 ,
85
+ new MetricBucket ().add (MetricEvent .PASS , 1 ));
86
+ final WindowWrap <MetricBucket > w2 = new WindowWrap <>(windowLengthInMs , 1000 ,
87
+ new MetricBucket ().add (MetricEvent .PASS , 2 ));
88
+ final WindowWrap <MetricBucket > w3 = new WindowWrap <>(windowLengthInMs , 1500 ,
89
+ new MetricBucket ().add (MetricEvent .PASS , 3 ));
90
+ final WindowWrap <MetricBucket > w4 = new WindowWrap <>(windowLengthInMs , 2000 ,
91
+ new MetricBucket ().add (MetricEvent .PASS , 4 ));
92
+ List <WindowWrap <MetricBucket >> buckets = Arrays .asList (w1 , w2 , w3 , w4 );
93
+ when (leapArray .currentWindow ()).thenReturn (w4 );
94
+ when (leapArray .list ()).thenReturn (buckets );
95
+
96
+ ArrayMetric metric = new ArrayMetric (leapArray );
97
+
98
+ // Empty condition -> retrieve all
99
+ assertEquals (4 , metric .detailsOnCondition (null ).size ());
100
+ // Normal condition
101
+ List <MetricNode > metricNodes = metric .detailsOnCondition (new Predicate <Long >() {
102
+ @ Override
103
+ public boolean test (Long t ) {
104
+ return t >= 1500 ;
105
+ }
106
+ });
107
+ assertEquals (2 , metricNodes .size ());
108
+ assertEquals (3 , metricNodes .get (0 ).getPassQps ());
109
+ assertEquals (4 , metricNodes .get (1 ).getPassQps ());
110
+
111
+ // Future condition
112
+ metricNodes = metric .detailsOnCondition (new Predicate <Long >() {
113
+ @ Override
114
+ public boolean test (Long t ) {
115
+ return t >= 2500 ;
116
+ }
117
+ });
118
+ assertEquals (0 , metricNodes .size ());
119
+ }
120
+ }
0 commit comments