/
BulkheadMetricTest.java
259 lines (205 loc) · 11 KB
/
BulkheadMetricTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* Copyright (c) 2018-2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.eclipse.microprofile.fault.tolerance.tck.metrics;
import static java.util.concurrent.TimeUnit.MINUTES;
import static org.eclipse.microprofile.fault.tolerance.tck.metrics.util.MetricComparator.approxMillis;
import static org.eclipse.microprofile.fault.tolerance.tck.metrics.util.MetricDefinition.BulkheadResult.ACCEPTED;
import static org.eclipse.microprofile.fault.tolerance.tck.metrics.util.MetricDefinition.BulkheadResult.REJECTED;
import static org.eclipse.microprofile.fault.tolerance.tck.metrics.util.MetricDefinition.InvocationResult.EXCEPTION_THROWN;
import static org.eclipse.microprofile.fault.tolerance.tck.metrics.util.MetricDefinition.InvocationResult.VALUE_RETURNED;
import static org.eclipse.microprofile.fault.tolerance.tck.util.Exceptions.expectBulkheadException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeoutException;
import org.eclipse.microprofile.fault.tolerance.tck.metrics.util.MetricDefinition.InvocationFallback;
import org.eclipse.microprofile.fault.tolerance.tck.metrics.util.MetricGetter;
import org.eclipse.microprofile.fault.tolerance.tck.util.AsyncCaller;
import org.eclipse.microprofile.fault.tolerance.tck.util.Packages;
import org.eclipse.microprofile.fault.tolerance.tck.util.TCKConfig;
import org.eclipse.microprofile.metrics.Histogram;
import org.eclipse.microprofile.metrics.Snapshot;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import jakarta.inject.Inject;
public class BulkheadMetricTest extends Arquillian {
@Deployment
public static WebArchive deploy() {
WebArchive war = ShrinkWrap.create(WebArchive.class, "ftMetricBulkhead.war")
.addClasses(BulkheadMetricBean.class)
.addPackage(Packages.UTILS)
.addPackage(Packages.METRIC_UTILS)
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
return war;
}
@Inject
private BulkheadMetricBean bulkheadBean;
@Inject
private AsyncCaller async;
private TCKConfig config = TCKConfig.getConfig();
private List<CompletableFuture<Void>> waitingFutures = new ArrayList<>();
/**
* Ensure that any waiting futures get completed at the end of each test
* <p>
* Important in case tests end early due to an exception or failure.
*/
@AfterMethod
public void completeWaitingFutures() {
for (CompletableFuture<Void> future : waitingFutures) {
future.complete(null);
}
waitingFutures.clear();
}
/**
* Use this method to obtain futures for passing to methods on {@link BulkheadMetricBean}
* <p>
* Using this factory method ensures they will be completed at the end of the test if your test fails.
*/
private CompletableFuture<Void> newWaitingFuture() {
CompletableFuture<Void> result = new CompletableFuture<>();
waitingFutures.add(result);
return result;
}
@Test
public void bulkheadMetricTest() throws InterruptedException, ExecutionException, TimeoutException {
MetricGetter m = new MetricGetter(BulkheadMetricBean.class, "waitFor");
m.baselineMetrics();
CompletableFuture<Void> waitingFuture = newWaitingFuture();
Future<?> f1 = async.run(() -> bulkheadBean.waitFor(waitingFuture));
Future<?> f2 = async.run(() -> bulkheadBean.waitFor(waitingFuture));
bulkheadBean.waitForRunningExecutions(2);
assertThat("executions running", m.getBulkheadExecutionsRunning().value(), is(2L));
waitingFuture.complete(null);
f1.get(1, MINUTES);
f2.get(1, MINUTES);
assertThat("executions running", m.getBulkheadExecutionsRunning().value(), is(0L));
assertThat("accepted calls", m.getBulkheadCalls(ACCEPTED).delta(), is(2L));
assertThat("rejected calls", m.getBulkheadCalls(REJECTED).delta(), is(0L));
// Async metrics should not be present
assertThat("bulkhead executions waiting present", m.getBulkheadExecutionsWaiting().gauge().isPresent(),
is(false));
assertThat("bulkhead waiting duration present", m.getBulkheadWaitingDuration().isPresent(), is(false));
// General metrics should be updated
assertThat("successful invocations", m.getInvocations(VALUE_RETURNED, InvocationFallback.NOT_DEFINED).delta(),
is(2L));
assertThat("failed invocations", m.getInvocations(EXCEPTION_THROWN, InvocationFallback.NOT_DEFINED).delta(),
is(0L));
}
@Test
public void bulkheadMetricRejectionTest() throws InterruptedException, ExecutionException, TimeoutException {
MetricGetter m = new MetricGetter(BulkheadMetricBean.class, "waitFor");
m.baselineMetrics();
CompletableFuture<Void> waitingFuture = newWaitingFuture();
Future<?> f1 = async.run(() -> bulkheadBean.waitFor(waitingFuture));
Future<?> f2 = async.run(() -> bulkheadBean.waitFor(waitingFuture));
bulkheadBean.waitForRunningExecutions(2);
Future<?> f3 = async.run(() -> bulkheadBean.waitFor(waitingFuture));
expectBulkheadException(f3);
assertThat("executions running", m.getBulkheadExecutionsRunning().value(), is(2L));
waitingFuture.complete(null);
f1.get(1, MINUTES);
f2.get(1, MINUTES);
assertThat("executions running", m.getBulkheadExecutionsRunning().value(), is(0L));
assertThat("accepted calls", m.getBulkheadCalls(ACCEPTED).delta(), is(2L));
assertThat("rejected calls", m.getBulkheadCalls(REJECTED).delta(), is(1L));
// General metrics should be updated
assertThat("successful invocations", m.getInvocations(VALUE_RETURNED, InvocationFallback.NOT_DEFINED).delta(),
is(2L));
assertThat("failed invocations", m.getInvocations(EXCEPTION_THROWN, InvocationFallback.NOT_DEFINED).delta(),
is(1L));
}
@Test
public void bulkheadMetricHistogramTest() throws InterruptedException, ExecutionException, TimeoutException {
MetricGetter m = new MetricGetter(BulkheadMetricBean.class, "waitForHistogram");
m.baselineMetrics();
CompletableFuture<Void> waitingFuture = newWaitingFuture();
Future<?> f1 = async.run(() -> bulkheadBean.waitForHistogram(waitingFuture));
Future<?> f2 = async.run(() -> bulkheadBean.waitForHistogram(waitingFuture));
bulkheadBean.waitForRunningExecutions(2);
Future<?> f3 = async.run(() -> bulkheadBean.waitForHistogram(waitingFuture));
expectBulkheadException(f3);
Thread.sleep(config.getTimeoutInMillis(1000));
waitingFuture.complete(null);
f1.get(1, MINUTES);
f2.get(1, MINUTES);
Histogram executionTimes = m.getBulkheadRunningDuration().get();
Snapshot snap = executionTimes.getSnapshot();
assertThat("histogram count", executionTimes.getCount(), is(2L)); // Rejected executions not recorded in
// histogram
assertThat("mean", Math.round(snap.getMean()), approxMillis(1000));
// Now let's put some quick results through the bulkhead
bulkheadBean.waitForHistogram(CompletableFuture.completedFuture(null));
bulkheadBean.waitForHistogram(CompletableFuture.completedFuture(null));
// Should have 4 results, ~0ms * 2 and ~1000ms * 2
snap = executionTimes.getSnapshot();
assertThat("histogram count", executionTimes.getCount(), is(4L));
}
@Test
public void bulkheadMetricAsyncTest() throws InterruptedException, ExecutionException, TimeoutException {
MetricGetter m = new MetricGetter(BulkheadMetricBean.class, "waitForAsync");
m.baselineMetrics();
CompletableFuture<Void> waitingFuture = newWaitingFuture();
Future<?> f1 = bulkheadBean.waitForAsync(waitingFuture);
Future<?> f2 = bulkheadBean.waitForAsync(waitingFuture);
bulkheadBean.waitForRunningExecutions(2);
Future<?> f3 = bulkheadBean.waitForAsync(waitingFuture);
Future<?> f4 = bulkheadBean.waitForAsync(waitingFuture);
waitForQueuePopulation(m, 2, config.getTimeoutInMillis(2000));
expectBulkheadException(bulkheadBean.waitForAsync(waitingFuture));
assertThat("executions running", m.getBulkheadExecutionsRunning().value(), is(2L));
assertThat("executions waiting", m.getBulkheadExecutionsWaiting().value(), is(2L));
Thread.sleep(config.getTimeoutInMillis(1000));
waitingFuture.complete(null);
f1.get(1, MINUTES);
f2.get(1, MINUTES);
f3.get(1, MINUTES);
f4.get(1, MINUTES);
assertThat("executions running", m.getBulkheadExecutionsRunning().value(), is(0L));
assertThat("accepted calls", m.getBulkheadCalls(ACCEPTED).delta(), is(4L));
assertThat("rejections", m.getBulkheadCalls(REJECTED).delta(), is(1L));
Histogram queueWaits = m.getBulkheadWaitingDuration().get();
// Expect 2 * wait for 0ms, 2 * wait for durationms
assertThat("waiting duration histogram counts", queueWaits.getCount(), is(4L));
// General metrics should be updated
assertThat("successful invocations", m.getInvocations(VALUE_RETURNED, InvocationFallback.NOT_DEFINED).delta(),
is(4L));
assertThat("failed invocations", m.getInvocations(EXCEPTION_THROWN, InvocationFallback.NOT_DEFINED).delta(),
is(1L));
}
private void waitForQueuePopulation(MetricGetter m,
int expectedQueuePopulation,
long timeoutInMs) throws InterruptedException {
long timeoutTime = System.currentTimeMillis() + timeoutInMs;
while (System.currentTimeMillis() < timeoutTime) {
if (m.getBulkheadExecutionsWaiting().value() == expectedQueuePopulation) {
return;
}
Thread.sleep(100L);
}
}
}