1
- // Copyright 2018, Oracle Corporation and/or its affiliates. All rights reserved.
1
+ // Copyright 2018, 2019 Oracle Corporation and/or its affiliates. All rights reserved.
2
2
// Licensed under the Universal Permissive License v 1.0 as shown at
3
3
// http://oss.oracle.com/licenses/upl.
4
4
8
8
import static java .net .HttpURLConnection .HTTP_UNAVAILABLE ;
9
9
import static oracle .kubernetes .operator .LabelConstants .CREATEDBYOPERATOR_LABEL ;
10
10
import static oracle .kubernetes .operator .LabelConstants .DOMAINUID_LABEL ;
11
- import static oracle .kubernetes .operator .builders .EventMatcher .*;
11
+ import static oracle .kubernetes .operator .builders .EventMatcher .addEvent ;
12
+ import static oracle .kubernetes .operator .builders .EventMatcher .deleteEvent ;
13
+ import static oracle .kubernetes .operator .builders .EventMatcher .errorEvent ;
14
+ import static oracle .kubernetes .operator .builders .EventMatcher .modifyEvent ;
12
15
import static oracle .kubernetes .operator .builders .WatchBuilderTest .JsonServletAction .withResponses ;
13
16
import static oracle .kubernetes .operator .builders .WatchBuilderTest .ParameterValidation .parameter ;
14
17
import static org .hamcrest .MatcherAssert .assertThat ;
15
- import static org .hamcrest .Matchers .*;
18
+ import static org .hamcrest .Matchers .contains ;
19
+ import static org .hamcrest .Matchers .empty ;
20
+ import static org .hamcrest .Matchers .equalTo ;
21
+ import static org .hamcrest .Matchers .is ;
22
+ import static org .hamcrest .Matchers .not ;
23
+ import static org .junit .Assert .fail ;
16
24
17
25
import com .meterware .pseudoserver .HttpUserAgentTest ;
18
26
import com .meterware .pseudoserver .PseudoServlet ;
19
27
import com .meterware .pseudoserver .WebResource ;
20
28
import com .meterware .simplestub .Memento ;
21
29
import com .meterware .simplestub .StaticStubSupport ;
22
- import com .squareup .okhttp .Call ;
23
30
import io .kubernetes .client .ApiClient ;
24
- import io .kubernetes .client .ApiException ;
25
31
import io .kubernetes .client .models .V1ObjectMeta ;
26
32
import io .kubernetes .client .models .V1Pod ;
27
33
import io .kubernetes .client .models .V1Service ;
28
- import java .io . IOException ;
34
+ import java .util . ArrayDeque ;
29
35
import java .util .ArrayList ;
30
36
import java .util .Arrays ;
37
+ import java .util .Collection ;
38
+ import java .util .Collections ;
31
39
import java .util .List ;
32
- import java .util .function . BiFunction ;
40
+ import java .util .Queue ;
33
41
import oracle .kubernetes .TestUtils ;
34
- import oracle .kubernetes .operator .helpers .Pool ;
42
+ import oracle .kubernetes .operator .helpers .ClientPool ;
35
43
import oracle .kubernetes .weblogic .domain .v2 .Domain ;
36
44
import org .junit .After ;
37
45
import org .junit .Before ;
@@ -61,12 +69,12 @@ public class WatchBuilderTest extends HttpUserAgentTest {
61
69
@ Before
62
70
public void setUp () throws Exception {
63
71
mementos .add (TestUtils .silenceOperatorLogger ());
64
- mementos .add (TestServerWatchFactory .install (getHostPath ()));
72
+ mementos .add (ClientPoolStub .install (getHostPath ()));
65
73
validationErrors = new ArrayList <>();
66
74
}
67
75
68
76
@ After
69
- public void tearDown () throws Exception {
77
+ public void tearDown () {
70
78
for (Memento memento : mementos ) memento .revert ();
71
79
if (!validationErrors .isEmpty ()) throw validationErrors .get (0 );
72
80
}
@@ -85,7 +93,35 @@ public void whenDomainWatchReceivesAddResponse_returnItFromIterator() throws Exc
85
93
assertThat (domainWatch , contains (addEvent (domain )));
86
94
}
87
95
88
- @ SuppressWarnings ("unchecked" )
96
+ @ Test
97
+ public void afterWatchClosed_returnClientToPool () throws Exception {
98
+ Domain domain =
99
+ new Domain ()
100
+ .withApiVersion (API_VERSION )
101
+ .withKind ("Domain" )
102
+ .withMetadata (createMetaData ("domain1" , NAMESPACE ));
103
+ defineHttpResponse (DOMAIN_RESOURCE , withResponses (createAddedResponse (domain )));
104
+
105
+ try (WatchI <Domain > domainWatch = new WatchBuilder ().createDomainWatch (NAMESPACE )) {
106
+ domainWatch .next ();
107
+ }
108
+
109
+ assertThat (ClientPoolStub .getPooledClients (), not (empty ()));
110
+ }
111
+
112
+ @ Test
113
+ public void afterWatchError_closeDoesNotReturnClientToPool () throws Exception {
114
+ defineHttpResponse (DOMAIN_RESOURCE , withResponses ());
115
+
116
+ try (WatchI <Domain > domainWatch = new WatchBuilder ().createDomainWatch (NAMESPACE )) {
117
+ domainWatch .next ();
118
+ fail ("Should have thrown an exception" );
119
+ } catch (Throwable ignore ) {
120
+ }
121
+
122
+ assertThat (ClientPoolStub .getPooledClients (), is (empty ()));
123
+ }
124
+
89
125
@ Test
90
126
public void whenDomainWatchReceivesModifyAndDeleteResponses_returnBothFromIterator ()
91
127
throws Exception {
@@ -239,7 +275,7 @@ private JsonServlet(JsonServletAction... actions) {
239
275
}
240
276
241
277
@ Override
242
- public WebResource getGetResponse () throws IOException {
278
+ public WebResource getGetResponse () {
243
279
if (requestNum >= actions .size ())
244
280
return new WebResource ("Unexpected Request #" + requestNum , HTTP_UNAVAILABLE );
245
281
@@ -251,6 +287,7 @@ public WebResource getGetResponse() throws IOException {
251
287
}
252
288
}
253
289
290
+ @ SuppressWarnings ("SameParameterValue" )
254
291
private V1ObjectMeta createMetaData (String name , String namespace ) {
255
292
return new V1ObjectMeta ()
256
293
.name (name )
@@ -274,45 +311,38 @@ private <T> String createDeletedResponse(T object) {
274
311
return WatchEvent .createDeleteEvent (object ).toJson ();
275
312
}
276
313
314
+ @ SuppressWarnings ("SameParameterValue" )
277
315
private String createErrorResponse (int statusCode ) {
278
316
return WatchEvent .createErrorEvent (statusCode ).toJson ();
279
317
}
280
318
281
- static class TestServerWatchFactory extends WatchBuilder .WatchFactoryImpl {
319
+ static class ClientPoolStub extends ClientPool {
320
+ private String basePath ;
321
+ private static Queue <ApiClient > queue ;
322
+
282
323
static Memento install (String basePath ) throws NoSuchFieldException {
283
- return StaticStubSupport . install (
284
- WatchBuilder . class , "FACTORY " , new TestServerWatchFactory (basePath ));
324
+ queue = new ArrayDeque <>();
325
+ return StaticStubSupport . install ( ClientPool . class , "SINGLETON " , new ClientPoolStub (basePath ));
285
326
}
286
327
287
- private String basePath ;
328
+ static Collection <ApiClient > getPooledClients () {
329
+ return Collections .unmodifiableCollection (queue );
330
+ }
288
331
289
- private TestServerWatchFactory (String basePath ) {
332
+ ClientPoolStub (String basePath ) {
290
333
this .basePath = basePath ;
291
334
}
292
335
293
336
@ Override
294
- public <T > WatchI <T > createWatch (
295
- Pool <ApiClient > pool ,
296
- CallParams callParams ,
297
- Class <?> responseBodyType ,
298
- BiFunction <ApiClient , CallParams , Call > function )
299
- throws ApiException {
300
- Pool <ApiClient > testPool =
301
- new Pool <ApiClient >() {
302
-
303
- @ Override
304
- protected ApiClient create () {
305
- Memento memento = TestUtils .silenceOperatorLogger ();
306
- try {
307
- ApiClient client = pool .take ();
308
- client .setBasePath (basePath );
309
- return client ;
310
- } finally {
311
- memento .revert ();
312
- }
313
- }
314
- };
315
- return super .createWatch (testPool , callParams , responseBodyType , function );
337
+ protected ApiClient create () {
338
+ ApiClient apiClient = super .create ();
339
+ apiClient .setBasePath (basePath );
340
+ return apiClient ;
341
+ }
342
+
343
+ @ Override
344
+ protected Queue <ApiClient > getQueue () {
345
+ return queue ;
316
346
}
317
347
}
318
348
}
0 commit comments