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 package org.apache.hc.client5.testing.extension.async;
29
30 import java.util.function.Consumer;
31
32 import org.apache.hc.client5.testing.extension.sync.TestClientResources;
33 import org.apache.hc.core5.http.URIScheme;
34 import org.apache.hc.core5.io.CloseMode;
35 import org.apache.hc.core5.util.Asserts;
36 import org.apache.hc.core5.util.TimeValue;
37 import org.apache.hc.core5.util.Timeout;
38 import org.junit.jupiter.api.extension.AfterEachCallback;
39 import org.junit.jupiter.api.extension.ExtensionContext;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public class TestAsyncResources implements AfterEachCallback {
44
45 private static final Logger LOG = LoggerFactory.getLogger(TestClientResources.class);
46
47 private final URIScheme scheme;
48 private final ClientProtocolLevel clientProtocolLevel;
49 private final ServerProtocolLevel serverProtocolLevel;
50 private final TestAsyncServerBootstrap serverBootstrap;
51 private final TestAsyncClientBuilder clientBuilder;
52
53 private TestAsyncServer server;
54 private TestAsyncClient client;
55
56 public TestAsyncResources(final URIScheme scheme, final ClientProtocolLevel clientProtocolLevel, final ServerProtocolLevel serverProtocolLevel, final Timeout timeout) {
57 this.scheme = scheme != null ? scheme : URIScheme.HTTP;
58 this.clientProtocolLevel = clientProtocolLevel != null ? clientProtocolLevel : ClientProtocolLevel.STANDARD;
59 this.serverProtocolLevel = serverProtocolLevel != null ? serverProtocolLevel : ServerProtocolLevel.STANDARD;
60 this.serverBootstrap = new TestAsyncServerBootstrap(this.scheme, this.serverProtocolLevel)
61 .setTimeout(timeout);
62 switch (this.clientProtocolLevel) {
63 case STANDARD:
64 this.clientBuilder = new StandardTestClientBuilder();
65 break;
66 case H2_ONLY:
67 this.clientBuilder = new H2OnlyTestClientBuilder();
68 break;
69 case MINIMAL_H2_ONLY:
70 this.clientBuilder = new H2OnlyMinimalTestClientBuilder();
71 break;
72 case MINIMAL:
73 this.clientBuilder = new MinimalTestClientBuilder();
74 break;
75 default:
76 this.clientBuilder = new StandardTestClientBuilder();
77 }
78 this.clientBuilder.setTimeout(timeout);
79 }
80
81 @Override
82 public void afterEach(final ExtensionContext extensionContext) {
83 LOG.debug("Shutting down test server");
84 if (client != null) {
85 client.close(CloseMode.GRACEFUL);
86 }
87 if (server != null) {
88 server.shutdown(TimeValue.ofSeconds(5));
89 }
90 }
91
92 public URIScheme scheme() {
93 return this.scheme;
94 }
95
96 public ServerProtocolLevel getServerProtocolLevel() {
97 return serverProtocolLevel;
98 }
99
100 public ClientProtocolLevel getClientProtocolLevel() {
101 return clientProtocolLevel;
102 }
103
104 public void configureServer(final Consumer<TestAsyncServerBootstrap> serverCustomizer) {
105 Asserts.check(server == null, "Server is already running and cannot be changed");
106 serverCustomizer.accept(serverBootstrap);
107 }
108
109 public TestAsyncServer server() throws Exception {
110 if (server == null) {
111 server = serverBootstrap.build();
112 }
113 return server;
114 }
115
116 public void configureClient(final Consumer<TestAsyncClientBuilder> clientCustomizer) {
117 Asserts.check(client == null, "Client is already running and cannot be changed");
118 clientCustomizer.accept(clientBuilder);
119 }
120
121 public TestAsyncClient client() throws Exception {
122 if (client == null) {
123 client = clientBuilder.build();
124 }
125 return client;
126 }
127
128 }