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.core5.testing.nio;
29  
30  import java.net.InetSocketAddress;
31  import java.util.Set;
32  import java.util.concurrent.ExecutionException;
33  import java.util.concurrent.Future;
34  
35  import org.apache.hc.core5.io.CloseMode;
36  import org.apache.hc.core5.reactor.DefaultListeningIOReactor;
37  import org.apache.hc.core5.reactor.IOReactorConfig;
38  import org.apache.hc.core5.reactor.IOReactorStatus;
39  import org.apache.hc.core5.reactor.ListenerEndpoint;
40  import org.apache.hc.core5.util.TimeValue;
41  import org.junit.jupiter.api.AfterEach;
42  import org.junit.jupiter.api.Assertions;
43  import org.junit.jupiter.api.BeforeEach;
44  import org.junit.jupiter.api.Test;
45  
46  /**
47   * Basic tests for {@link DefaultListeningIOReactor}.
48   */
49  public class TestDefaultListeningIOReactor {
50  
51      private DefaultListeningIOReactor ioReactor;
52  
53      @BeforeEach
54      public void setup() throws Exception {
55          final IOReactorConfig reactorConfig = IOReactorConfig.custom()
56                  .setIoThreadCount(1)
57                  .build();
58          this.ioReactor = new DefaultListeningIOReactor(new NoopIOEventHandlerFactory(), reactorConfig, null);
59      }
60  
61      @AfterEach
62      public void cleanup() throws Exception {
63          if (this.ioReactor != null) {
64              this.ioReactor.close(CloseMode.IMMEDIATE);
65          }
66      }
67  
68      @Test
69      public void testEndpointUpAndDown() throws Exception {
70          ioReactor.start();
71  
72          Set<ListenerEndpoint> endpoints = ioReactor.getEndpoints();
73          Assertions.assertNotNull(endpoints);
74          Assertions.assertEquals(0, endpoints.size());
75  
76          final Future<ListenerEndpoint> future1 = ioReactor.listen(new InetSocketAddress(0));
77          final ListenerEndpoint endpoint1 = future1.get();
78  
79          final Future<ListenerEndpoint> future2 = ioReactor.listen(new InetSocketAddress(0));
80          final ListenerEndpoint endpoint2 = future2.get();
81          final int port = ((InetSocketAddress) endpoint2.getAddress()).getPort();
82  
83          endpoints = ioReactor.getEndpoints();
84          Assertions.assertNotNull(endpoints);
85          Assertions.assertEquals(2, endpoints.size());
86  
87          endpoint1.close();
88  
89          endpoints = ioReactor.getEndpoints();
90          Assertions.assertNotNull(endpoints);
91          Assertions.assertEquals(1, endpoints.size());
92  
93          final ListenerEndpoint endpoint = endpoints.iterator().next();
94  
95          Assertions.assertEquals(port, ((InetSocketAddress) endpoint.getAddress()).getPort());
96  
97          ioReactor.close(CloseMode.GRACEFUL);
98          ioReactor.awaitShutdown(TimeValue.ofSeconds(5));
99          Assertions.assertEquals(IOReactorStatus.SHUT_DOWN, ioReactor.getStatus());
100     }
101 
102     @Test
103     public void testEndpointAlreadyBound() throws Exception {
104         ioReactor.start();
105 
106         final Future<ListenerEndpoint> future1 = ioReactor.listen(new InetSocketAddress(0));
107         final ListenerEndpoint endpoint1 = future1.get();
108         final int port = ((InetSocketAddress) endpoint1.getAddress()).getPort();
109 
110         final Future<ListenerEndpoint> future2 = ioReactor.listen(new InetSocketAddress(port));
111         Assertions.assertThrows(ExecutionException.class, () -> future2.get());
112         ioReactor.close(CloseMode.GRACEFUL);
113         ioReactor.awaitShutdown(TimeValue.ofSeconds(5));
114 
115         Assertions.assertEquals(IOReactorStatus.SHUT_DOWN, ioReactor.getStatus());
116     }
117 
118 }