28
28
import java .util .Set ;
29
29
import java .util .concurrent .Callable ;
30
30
import java .util .concurrent .CompletableFuture ;
31
+ import java .util .concurrent .TimeUnit ;
32
+ import java .util .concurrent .TimeoutException ;
31
33
import java .util .stream .Collectors ;
32
34
33
35
import org .junit .Before ;
34
36
import org .junit .Test ;
35
37
36
- import static io .github .resilience4j .micrometer .tagged .MetricsTestHelper .findCounterByNamesTag ;
38
+ import static io .github .resilience4j .micrometer .tagged .MetricsTestHelper .*;
39
+ import static io .github .resilience4j .micrometer .tagged .TaggedCircuitBreakerMetrics .MetricNames .DEFAULT_CIRCUIT_BREAKER_BUFFERED_CALLS ;
40
+ import static io .github .resilience4j .micrometer .tagged .TaggedTimeLimiterMetrics .MetricNames .DEFAULT_TIME_LIMITER_CALLS ;
37
41
import static org .assertj .core .api .Assertions .assertThat ;
38
42
39
43
public class TaggedTimeLimiterMetricsTest {
40
44
41
- private static final String DEFAULT_PREFIX = "resilience4j.timelimiter" ;
42
- private static final String SUCCESSFUL = DEFAULT_PREFIX + ".successful" ;
43
- private static final String FAILED = DEFAULT_PREFIX + ".failed" ;
44
- private static final String TIMEOUT = DEFAULT_PREFIX + ".timeout" ;
45
-
46
45
private MeterRegistry meterRegistry ;
47
46
private TimeLimiter timeLimiter ;
48
47
private TimeLimiterRegistry timeLimiterRegistry ;
@@ -61,17 +60,18 @@ public void setUp() {
61
60
@ Test
62
61
public void shouldAddMetricsForANewlyCreatedTimeLimiter () {
63
62
TimeLimiter newTimeLimiter = timeLimiterRegistry .timeLimiter ("backendB" );
63
+ newTimeLimiter .onSuccess ();
64
64
65
65
assertThat (taggedTimeLimiterMetrics .meterIdMap ).containsKeys ("backendA" , "backendB" );
66
66
assertThat (taggedTimeLimiterMetrics .meterIdMap .get ("backendA" )).hasSize (3 );
67
67
assertThat (taggedTimeLimiterMetrics .meterIdMap .get ("backendB" )).hasSize (3 );
68
68
69
69
assertThat (meterRegistry .getMeters ()).hasSize (6 );
70
70
71
- Collection <Counter > counters = meterRegistry .get (SUCCESSFUL ).counters ();
71
+ Collection <Counter > counters = meterRegistry .get (DEFAULT_TIME_LIMITER_CALLS ).counters ();
72
72
73
- Optional <Counter > successful = findCounterByNamesTag (counters , newTimeLimiter .getName ());
74
- assertThat (successful ).map (Counter ::count ).contains (0d );
73
+ Optional <Counter > successful = findCounterByKindAndNameTags (counters , "successful" , newTimeLimiter .getName ());
74
+ assertThat (successful ).map (Counter ::count ).contains (1d );
75
75
}
76
76
77
77
@ Test
@@ -88,69 +88,53 @@ public void shouldRemovedMetricsForRemovedRetry() {
88
88
89
89
@ Test
90
90
public void shouldReplaceMetrics () {
91
- Counter before = meterRegistry .get (SUCCESSFUL ).counter ();
91
+ Counter before = meterRegistry .get (DEFAULT_TIME_LIMITER_CALLS ).counter ();
92
92
assertThat (before ).isNotNull ();
93
93
assertThat (before .count ()).isEqualTo (0 );
94
94
assertThat (before .getId ().getTag (TagNames .NAME )).isEqualTo (timeLimiter .getName ());
95
95
96
96
timeLimiterRegistry .replace (timeLimiter .getName (), TimeLimiter .ofDefaults ());
97
97
98
- Counter after = meterRegistry .get (SUCCESSFUL ).counter ();
98
+ Counter after = meterRegistry .get (DEFAULT_TIME_LIMITER_CALLS ).counter ();
99
99
assertThat (after ).isNotNull ();
100
100
assertThat (after .count ()).isEqualTo (0 );
101
101
assertThat (after .getId ().getTag (TagNames .NAME )).isEqualTo (TimeLimiter .ofDefaults ().getName ());
102
102
}
103
103
104
104
@ Test
105
105
public void successfulCounterIsRegistered () {
106
- Counter successful = meterRegistry .get (SUCCESSFUL ).counter ();
106
+ Collection <Counter > counters = meterRegistry .get (DEFAULT_TIME_LIMITER_CALLS ).counters ();
107
+ timeLimiter .onSuccess ();
107
108
108
- assertThat (successful ).isNotNull ();
109
- assertThat (successful .count ()).isEqualTo (0 );
110
- assertThat (successful .getId ().getTag (TagNames .NAME )).isEqualTo (timeLimiter .getName ());
109
+ Optional <Counter > successful = findCounterByKindAndNameTags (counters , "successful" , timeLimiter .getName ());
110
+ assertThat (successful ).map (Counter ::count ).contains (1d );
111
111
}
112
112
113
113
@ Test
114
114
public void failedCounterIsRegistered () {
115
- Counter failed = meterRegistry .get (FAILED ).counter ();
115
+ Collection <Counter > counters = meterRegistry .get (DEFAULT_TIME_LIMITER_CALLS ).counters ();
116
+ timeLimiter .onError (new RuntimeException ());
116
117
117
- assertThat (failed ).isNotNull ();
118
- assertThat (failed .count ()).isEqualTo (0 );
119
- assertThat (failed .getId ().getTag (TagNames .NAME )).isEqualTo (timeLimiter .getName ());
118
+ Optional <Counter > failed = findCounterByKindAndNameTags (counters , "failed" , timeLimiter .getName ());
119
+ assertThat (failed ).map (Counter ::count ).contains (1d );
120
120
}
121
121
122
122
@ Test
123
123
public void timoutCounterIsRegistered () {
124
- Counter timout = meterRegistry .get (TIMEOUT ).counter ();
124
+ Collection <Counter > counters = meterRegistry .get (DEFAULT_TIME_LIMITER_CALLS ).counters ();
125
+ timeLimiter .onError (new TimeoutException ());
125
126
126
- assertThat (timout ).isNotNull ();
127
- assertThat (timout .count ()).isEqualTo (0 );
128
- assertThat (timout .getId ().getTag (TagNames .NAME )).isEqualTo (timeLimiter .getName ());
127
+ Optional <Counter > timeout = findCounterByKindAndNameTags (counters , "timeout" , timeLimiter .getName ());
128
+ assertThat (timeout ).map (Counter ::count ).contains (1d );
129
129
}
130
-
131
- @ Test
132
- public void successfulCounterIsIncremented () throws Exception {
133
- Callable <String > decoratedSupplier = timeLimiter .decorateFutureSupplier (() ->
134
- CompletableFuture .completedFuture ("Hello world" ));
135
-
136
- decoratedSupplier .call ();
137
- decoratedSupplier .call ();
138
-
139
- assertThat (meterRegistry .get (SUCCESSFUL ).counter ().count ()).isEqualTo (2 );
140
- assertThat (meterRegistry .get (FAILED ).counter ().count ()).isEqualTo (0 );
141
- assertThat (meterRegistry .get (TIMEOUT ).counter ().count ()).isEqualTo (0 );
142
- }
143
-
144
130
@ Test
145
131
public void customMetricNamesGetApplied () {
146
132
MeterRegistry meterRegistry = new SimpleMeterRegistry ();
147
133
TimeLimiterRegistry timeLimiterRegistry = TimeLimiterRegistry .ofDefaults ();
148
134
timeLimiterRegistry .timeLimiter ("backendA" );
149
135
TaggedTimeLimiterMetrics .ofTimeLimiterRegistry (
150
136
TaggedTimeLimiterMetrics .MetricNames .custom ()
151
- .successfulMetricName ("custom_successful" )
152
- .failedMetricName ("custom_failed" )
153
- .timeoutMetricName ("custom_timeout" )
137
+ .callsMetricName ("custom_calls" )
154
138
.build (),
155
139
timeLimiterRegistry
156
140
).bindTo (meterRegistry );
@@ -162,9 +146,7 @@ public void customMetricNamesGetApplied() {
162
146
.collect (Collectors .toSet ());
163
147
164
148
assertThat (metricNames ).hasSameElementsAs (Arrays .asList (
165
- "custom_successful" ,
166
- "custom_failed" ,
167
- "custom_timeout"
149
+ "custom_calls"
168
150
));
169
151
}
170
152
}
0 commit comments