/
TimeoutTest.java
399 lines (374 loc) · 15.6 KB
/
TimeoutTest.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
*******************************************************************************
* Copyright (c) 2016-2017 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;
import javax.inject.Inject;
import org.eclipse.microprofile.fault.tolerance.tck.config.ConfigAnnotationAsset;
import org.eclipse.microprofile.fault.tolerance.tck.timeout.clientserver.DefaultTimeoutClient;
import org.eclipse.microprofile.fault.tolerance.tck.timeout.clientserver.ShorterTimeoutClient;
import org.eclipse.microprofile.fault.tolerance.tck.timeout.clientserver.TimeoutClient;
import org.eclipse.microprofile.faulttolerance.Timeout;
import org.eclipse.microprofile.faulttolerance.exceptions.TimeoutException;
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.Asset;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.testng.Assert;
import org.testng.annotations.Test;
import static org.eclipse.microprofile.fault.tolerance.tck.util.TCKConfig.getConfig;
/**
* Tests to exercise Fault Tolerance Timeouts.
*
* @author <a href="mailto:neil_young@uk.ibm.com">Neil Young</a>
*
*/
public class TimeoutTest extends Arquillian {
private @Inject TimeoutClient clientForTimeout;
private @Inject DefaultTimeoutClient clientForDefaultTimeout;
private @Inject ShorterTimeoutClient clientForShorterTimeout;
@Deployment
public static WebArchive deploy() {
final Asset config = new ConfigAnnotationAsset()
.setValue(TimeoutClient.class,"serviceA", Timeout.class, getConfig().getTimeoutInStr(1000))
.setValue(TimeoutClient.class,"serviceB", Timeout.class, getConfig().getTimeoutInStr(2000))
.setValue(TimeoutClient.class,"serviceC", Timeout.class, getConfig().getTimeoutInStr(500));
// serviceD uses SECONDS time unit and will not be configured
JavaArchive testJar = ShrinkWrap
.create(JavaArchive.class, "ftTimeout.jar")
.addClasses(TimeoutClient.class, DefaultTimeoutClient.class, ShorterTimeoutClient.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml").as(JavaArchive.class);
WebArchive war = ShrinkWrap.create(WebArchive.class, "ftTimeout.war").addAsLibrary(testJar);
return war;
}
/**
* A test to exercise the default timeout. The default Fault Tolerance
* timeout is 1 second but serviceA will attempt to sleep for 20 seconds, so
* should throw a TimeoutException.
*
*/
@Test
public void testTimeout() {
try {
clientForTimeout.serviceA(20000);
Assert.fail("serviceA should throw a TimeoutException in testTimeout");
}
catch (TimeoutException ex) {
// Expected
}
catch (RuntimeException ex) {
// Not Expected
Assert.fail("serviceA should throw a TimeoutException in testTimeout not a RuntimeException");
}
}
/**
* A test that should not time out. The default Fault Tolerance timeout is 1
* second but serviceA will attempt to sleep for only 10 milliseconds before
* throwing a RuntimeException. There should be no Timeout.
*
*/
@Test
public void testNoTimeout() {
try {
clientForTimeout.serviceA(10);
Assert.fail("serviceB should throw a RuntimeException in testNoTimeout");
}
catch (TimeoutException ex) {
// Not Expected
Assert.fail("serviceB should throw a RuntimeException in testNoTimeout not a TimeoutException");
}
catch (RuntimeException ex) {
// Expected
}
}
/**
* A test that should time out. The Fault Tolerance timeout is set to a (non-default) 2 seconds but serviceB
* will attempt to sleep for 2.5 seconds - so longer than a default timeout.
*/
@Test
public void testGTDefaultTimeout() {
try {
clientForTimeout.serviceB(2500);
Assert.fail("serviceB should throw a TimeoutException in testGTDefaultTimeout");
}
catch (TimeoutException ex) {
// Expected
}
catch (RuntimeException ex) {
// Not Expected
Assert.fail("serviceB should throw a TimeoutException in testGTDefaultTimeout not a RuntimeException");
}
}
/**
* A test that should not time out. The Fault Tolerance timeout is set to 2
* seconds but serviceB will attempt to sleep for 1.5 seconds - so longer than a default
* timeout but shorter than the timeout that has been configured, before throwing a
* RuntimeException. There should be no Timeout.
*
*/
@Test
public void testGTDefaultNoTimeout() {
try {
clientForTimeout.serviceB(1500);
Assert.fail("serviceB should throw a RuntimeException in testGTDefaultNoTimeout");
}
catch (TimeoutException ex) {
// Not Expected
Assert.fail("serviceB should throw a RuntimeException in testGTDefaultNoTimeout not a TimeoutException");
}
catch (RuntimeException ex) {
// Expected
}
}
/**
* A test that should time out. The Fault Tolerance timeout is set to a (non-default) 0.5 seconds but serviceC
* will attempt to sleep for 1 second - so longer than a default timeout.
*/
@Test
public void testLTDefaultTimeout() {
try {
clientForTimeout.serviceC(1000);
Assert.fail("serviceC should throw a TimeoutException in testLTDefaultTimeout");
}
catch (TimeoutException ex) {
// Expected
}
catch (RuntimeException ex) {
// Not Expected
Assert.fail("serviceC should throw a TimeoutException in testLTDefaultTimeout not a RuntimeException");
}
}
/**
* A test that should not time out. The Fault Tolerance timeout is set to a (non-default) 0.5
* seconds but serviceC will attempt to sleep for only 10 milliseconds before
* throwing a RuntimeException. There should be no Timeout.
*
*/
@Test
public void testLTDefaultNoTimeout() {
try {
clientForTimeout.serviceC(10);
Assert.fail("serviceC should throw a RuntimeException in testLTDefaultNoTimeout");
}
catch (TimeoutException ex) {
// Not Expected
Assert.fail("serviceC should throw a RuntimeException in testLTDefaultNoTimeout not a TimeoutException");
}
catch (RuntimeException ex) {
// Expected
}
}
/**
* A test that should time out. The Fault Tolerance timeout is set to a (non-default) 2 seconds but serviceD
* will attempt to sleep for 2.5 seconds - so longer than a default timeout. serviceD specifies its timeout
* in Seconds rather than milliseconds.
*/
@Test
public void testSecondsTimeout() {
try {
clientForTimeout.serviceD(2500);// timeout will not be dynamically configured
Assert.fail("serviceD should throw a TimeoutException in testSecondsTimeout");
}
catch (TimeoutException ex) {
// Expected
}
catch (RuntimeException ex) {
// Not Expected
Assert.fail("serviceD should throw a TimeoutException in testSecondsTimeout not a RuntimeException");
}
}
/**
* A test that should not time out. The Fault Tolerance timeout is set to 2
* seconds but serviceD will attempt to sleep for 1.5 seconds - so longer than a default
* timeout but shorter than the timeout that has been configured, before throwing a
* RuntimeException. There should be no Timeout.
*
*/
@Test
public void testSecondsNoTimeout() {
try {
clientForTimeout.serviceD(1500);// timeout will not be dynamically configured
Assert.fail("serviceD should throw a RuntimeException in testSecondsNoTimeout");
}
catch (TimeoutException ex) {
// Not Expected
Assert.fail("serviceD should throw a RuntimeException in testSecondsNoTimeout not a TimeoutException");
}
catch (RuntimeException ex) {
// Expected
}
}
/**
* A parallel test to testTimeout with class level annotation. The default Fault Tolerance
* timeout is 1 second but serviceA will attempt to sleep for 20 seconds, so
* should throw a TimeoutException.
*
*/
@Test
public void testTimeoutClassLevel() {
try {
clientForDefaultTimeout.serviceA(20000);
Assert.fail("serviceA should throw a TimeoutException in testTimeout");
}
catch (TimeoutException ex) {
// Expected
}
catch (RuntimeException ex) {
// Not Expected
Assert.fail("serviceA should throw a TimeoutException in testTimeout not a RuntimeException");
}
}
/**
* A parallel test to testNoTimeout with class level annotation. The default Fault
* Tolerance timeout is 1 second but serviceA will attempt to sleep for only 10 milliseconds
* before throwing a RuntimeException. There should be no Timeout.
*
*/
@Test
public void testNoTimeoutClassLevel() {
try {
clientForDefaultTimeout.serviceA(10);
Assert.fail("serviceB should throw a RuntimeException in testNoTimeoutClassLevel");
}
catch (TimeoutException ex) {
// Not Expected
Assert.fail("serviceB should throw a RuntimeException in testNoTimeoutClassLevel not a TimeoutException");
}
catch (RuntimeException ex) {
// Expected
}
}
/**
* A parallel test to testGTDefaultTimeout where the method level Timeout annotation overrides
* the class level annotation. The Fault Tolerance timeout is set to a (non-default) 2 seconds but
* serviceB will attempt to sleep for 2.5 seconds - so longer than a default timeout.
*/
@Test
public void testGTDefaultTimeoutOverride() {
try {
clientForDefaultTimeout.serviceB(2500);
Assert.fail("serviceB should throw a TimeoutException in testGTDefaultTimeout");
}
catch (TimeoutException ex) {
// Expected
}
catch (RuntimeException ex) {
// Not Expected
Assert.fail("serviceB should throw a TimeoutException in testGTDefaultTimeout not a RuntimeException");
}
}
/**
* A parallel test to testGTDefaultNoTimeout where the method level Timeout annotation overrides
* the class level annotation. The Fault Tolerance timeout is set to 2 seconds but serviceB will attempt
* to sleep for 1.5 seconds - so longer than a default timeout but shorter than the timeout that has been
* configured, before throwing a RuntimeException. There should be no Timeout.
*/
@Test
public void testGTDefaultNoTimeoutOverride() {
try {
clientForDefaultTimeout.serviceB(1500);
Assert.fail("serviceB should throw a RuntimeException in testGTDefaultNoTimeout");
}
catch (TimeoutException ex) {
// Not Expected
Assert.fail("serviceB should throw a RuntimeException in testGTDefaultNoTimeout not a TimeoutException");
}
catch (RuntimeException ex) {
// Expected
}
}
/**
* A parallel test to testLTDefaultTimeout with class level annotation. The Fault Tolerance timeout is set
* to a (non-default) 0.5 seconds but serviceA will attempt to sleep for 1 second - so longer than a default timeout.
*/
@Test
public void testLTDefaultTimeoutClassLevel() {
try {
clientForShorterTimeout.serviceA(1000);
Assert.fail("serviceA should throw a TimeoutException in testLTDefaultTimeoutClassLevel");
}
catch (TimeoutException ex) {
// Expected
}
catch (RuntimeException ex) {
// Not Expected
Assert.fail("serviceA should throw a TimeoutException in testLTDefaultTimeoutClassLevel not a RuntimeException");
}
}
/**
* A parallel test to testLTDefaultNoTimeout with class level annotation. The Fault Tolerance timeout is set
* to a (non-default) 0.5 seconds but serviceC will attempt to sleep for only 10 milliseconds before throwing a
* RuntimeException. There should be no Timeout.
*/
@Test
public void testLTDefaultNoTimeoutClassLevel() {
try {
clientForShorterTimeout.serviceA(10);
Assert.fail("serviceA should throw a RuntimeException in testLTDefaultNoTimeoutClassLevel");
}
catch (TimeoutException ex) {
// Not Expected
Assert.fail("serviceA should throw a RuntimeException in testLTDefaultNoTimeoutClassLevel not a TimeoutException");
}
catch (RuntimeException ex) {
// Expected
}
}
/**
* A parallel test to testGTDefaultTimeout where the method level Timeout annotation overrides
* the class level annotation. The Fault Tolerance timeout is set to a (non-default) 2 seconds but
* serviceB will attempt to sleep for 2.5 seconds - so longer than a default timeout.
*/
@Test
public void testGTShorterTimeoutOverride() {
try {
clientForShorterTimeout.serviceB(2500);
Assert.fail("serviceB should throw a TimeoutException in testGTShorterTimeoutOverride");
}
catch (TimeoutException ex) {
// Expected
}
catch (RuntimeException ex) {
// Not Expected
Assert.fail("serviceB should throw a TimeoutException in testGTShorterTimeoutOverride not a RuntimeException");
}
}
/**
* A parallel test to testGTDefaultNoTimeout where the method level Timeout annotation overrides
* the class level annotation. The Fault Tolerance timeout is set to 2 seconds but serviceB will attempt
* to sleep for 1.5 seconds - so longer than a default timeout but shorter than the timeout that has been
* configured, before throwing a RuntimeException. There should be no Timeout.
*/
@Test
public void testGTShorterNoTimeoutOverride() {
try {
clientForShorterTimeout.serviceB(1500);
Assert.fail("serviceB should throw a RuntimeException in testGTShorterNoTimeoutOverride");
}
catch (TimeoutException ex) {
// Not Expected
Assert.fail("serviceB should throw a RuntimeException in testGTShorterNoTimeoutOverride not a TimeoutException");
}
catch (RuntimeException ex) {
// Expected
}
}
}