View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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) throws Exception {
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 }