26
26
import org .apache .commons .pool2 .impl .GenericObjectPool ;
27
27
import org .apache .commons .pool2 .impl .GenericObjectPoolConfig ;
28
28
import org .junit .jupiter .api .AfterEach ;
29
+ import org .junit .jupiter .api .BeforeEach ;
29
30
import org .junit .jupiter .api .Test ;
30
31
31
32
import java .util .Arrays ;
@@ -41,50 +42,55 @@ class CommonsObjectPool2MetricsTest {
41
42
42
43
private int genericObjectPoolCount = 0 ;
43
44
44
- private Tags tags = Tags .of ("app" , "myapp" , "version" , "1" );
45
+ private final Tags tags = Tags .of ("app" , "myapp" , "version" , "1" );
45
46
46
- private final MeterRegistry registry = new SimpleMeterRegistry () ;
47
+ private MeterRegistry registry ;
47
48
48
49
// tag::setup[]
49
50
private final CommonsObjectPool2Metrics commonsObjectPool2Metrics = new CommonsObjectPool2Metrics (tags );
50
51
51
52
// end::setup[]
53
+ @ BeforeEach
54
+ void setUp () {
55
+ registry = new SimpleMeterRegistry ();
56
+ }
52
57
53
58
@ AfterEach
54
- void afterEach () {
59
+ void tearDown () {
55
60
commonsObjectPool2Metrics .close ();
56
61
}
57
62
58
63
@ Test
59
- void verifyMetricsWithExpectedTags () {
64
+ void verifyGenericObjectPoolMetricsWithExpectedTags () {
60
65
// tag::generic_pool[]
61
- try (GenericObjectPool <Object > p = createGenericObjectPool ()) {
62
- MeterRegistry registry = new SimpleMeterRegistry ();
66
+ try (GenericObjectPool <Object > ignored = createGenericObjectPool ()) {
63
67
commonsObjectPool2Metrics .bindTo (registry );
68
+ Tags tagsToMatch = tags .and ("name" , "pool" , "type" , "GenericObjectPool" , "factoryType" ,
69
+ "io.micrometer.core.instrument.binder.commonspool2.CommonsObjectPool2MetricsTest$1<java.lang.Object>" );
64
70
65
- registry .get ("commons.pool2.num.idle" ).tags (tags ).gauge ();
66
- registry .get ("commons.pool2.num.waiters" ).tags (tags ).gauge ();
71
+ registry .get ("commons.pool2.num.idle" ).tags (tagsToMatch ).gauge ();
72
+ registry .get ("commons.pool2.num.waiters" ).tags (tagsToMatch ).gauge ();
67
73
68
74
Arrays
69
75
.asList ("commons.pool2.created" , "commons.pool2.borrowed" , "commons.pool2.returned" ,
70
76
"commons.pool2.destroyed" , "commons.pool2.destroyed.by.evictor" ,
71
77
"commons.pool2.destroyed.by.borrow.validation" )
72
- .forEach (name -> registry .get (name ).tags (tags ).functionCounter ());
78
+ .forEach (name -> registry .get (name ).tags (tagsToMatch ).functionCounter ());
73
79
74
80
Arrays
75
81
.asList ("commons.pool2.max.borrow.wait" , "commons.pool2.mean.active" , "commons.pool2.mean.idle" ,
76
82
"commons.pool2.mean.borrow.wait" )
77
- .forEach (name -> registry .get (name ).tags (tags ).timeGauge ());
83
+ .forEach (name -> registry .get (name ).tags (tagsToMatch ).timeGauge ());
78
84
}
79
85
// end::generic_pool[]
80
86
}
81
87
82
88
@ Test
83
89
void verifyGenericKeyedObjectPoolMetricsWithExpectedTags () {
84
90
// tag::generic_keyed_pool[]
85
- try (GenericKeyedObjectPool <Object , Object > p = createGenericKeyedObjectPool ()) {
86
- Tags tagsToMatch = tags .and ("type" , "GenericKeyedObjectPool" );
91
+ try (GenericKeyedObjectPool <Object , Object > ignored = createGenericKeyedObjectPool ()) {
87
92
commonsObjectPool2Metrics .bindTo (registry );
93
+ Tags tagsToMatch = tags .and ("name" , "pool" , "type" , "GenericKeyedObjectPool" , "factoryType" , "none" );
88
94
89
95
Arrays .asList ("commons.pool2.num.idle" , "commons.pool2.num.waiters" )
90
96
.forEach (name -> registry .get (name ).tags (tagsToMatch ).gauge ());
@@ -113,13 +119,13 @@ void verifyIdleAndActiveInstancesAreReported() throws Exception {
113
119
});
114
120
115
121
try (GenericObjectPool <Object > genericObjectPool = createGenericObjectPool ()) {
116
- latch .await (10 , TimeUnit .SECONDS );
117
- final Object o = genericObjectPool .borrowObject (10_000L );
122
+ assertThat ( latch .await (10 , TimeUnit .SECONDS )). isTrue ( );
123
+ Object object = genericObjectPool .borrowObject (10_000L );
118
124
119
125
assertThat (registry .get ("commons.pool2.num.active" ).gauge ().value ()).isEqualTo (1.0 );
120
126
assertThat (registry .get ("commons.pool2.num.idle" ).gauge ().value ()).isEqualTo (0.0 );
121
127
122
- genericObjectPool .returnObject (o );
128
+ genericObjectPool .returnObject (object );
123
129
124
130
assertThat (registry .get ("commons.pool2.num.active" ).gauge ().value ()).isEqualTo (0.0 );
125
131
assertThat (registry .get ("commons.pool2.num.idle" ).gauge ().value ()).isEqualTo (1.0 );
@@ -128,11 +134,9 @@ void verifyIdleAndActiveInstancesAreReported() throws Exception {
128
134
129
135
@ Test
130
136
void metricsReportedPerMultiplePools () {
131
- try (final GenericObjectPool <Object > p1 = createGenericObjectPool ();
132
- final GenericObjectPool <Object > p2 = createGenericObjectPool ();
133
- final GenericObjectPool <Object > p3 = createGenericObjectPool ()) {
134
-
135
- MeterRegistry registry = new SimpleMeterRegistry ();
137
+ try (GenericObjectPool <Object > ignored1 = createGenericObjectPool ();
138
+ GenericObjectPool <Object > ignored2 = createGenericObjectPool ();
139
+ GenericObjectPool <Object > ignored3 = createGenericObjectPool ()) {
136
140
commonsObjectPool2Metrics .bindTo (registry );
137
141
138
142
registry .get ("commons.pool2.num.waiters" ).tag ("name" , "pool" + genericObjectPoolCount ).gauge ();
@@ -142,7 +146,6 @@ void metricsReportedPerMultiplePools() {
142
146
143
147
@ Test
144
148
void newPoolsAreDiscoveredByListener () throws InterruptedException {
145
- MeterRegistry registry = new SimpleMeterRegistry ();
146
149
commonsObjectPool2Metrics .bindTo (registry );
147
150
148
151
CountDownLatch latch = new CountDownLatch (1 );
@@ -151,8 +154,8 @@ void newPoolsAreDiscoveredByListener() throws InterruptedException {
151
154
latch .countDown ();
152
155
});
153
156
154
- try (GenericObjectPool <Object > p = createGenericObjectPool ()) {
155
- latch .await (10 , TimeUnit .SECONDS );
157
+ try (GenericObjectPool <Object > ignored = createGenericObjectPool ()) {
158
+ assertThat ( latch .await (10 , TimeUnit .SECONDS )). isTrue ( );
156
159
}
157
160
}
158
161
@@ -161,7 +164,7 @@ private GenericObjectPool<Object> createGenericObjectPool() {
161
164
GenericObjectPoolConfig <Object > config = new GenericObjectPoolConfig <>();
162
165
config .setMaxTotal (10 );
163
166
164
- return new GenericObjectPool <>(new BasePooledObjectFactory <Object >() {
167
+ return new GenericObjectPool <>(new BasePooledObjectFactory <>() {
165
168
@ Override
166
169
public Object create () {
167
170
return new Object ();
@@ -175,7 +178,7 @@ public PooledObject<Object> wrap(Object testObject) {
175
178
}
176
179
177
180
private GenericKeyedObjectPool <Object , Object > createGenericKeyedObjectPool () {
178
- return new GenericKeyedObjectPool <>(new BaseKeyedPooledObjectFactory <Object , Object >() {
181
+ return new GenericKeyedObjectPool <>(new BaseKeyedPooledObjectFactory <>() {
179
182
@ Override
180
183
public Object create (Object key ) {
181
184
return key ;
0 commit comments