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.nio.ByteBuffer;
32  import java.util.Set;
33  import java.util.concurrent.ExecutionException;
34  import java.util.concurrent.Future;
35  
36  import org.apache.hc.core5.io.CloseMode;
37  import org.apache.hc.core5.reactor.DefaultListeningIOReactor;
38  import org.apache.hc.core5.reactor.IOEventHandler;
39  import org.apache.hc.core5.reactor.IOEventHandlerFactory;
40  import org.apache.hc.core5.reactor.IOReactorConfig;
41  import org.apache.hc.core5.reactor.IOReactorStatus;
42  import org.apache.hc.core5.reactor.IOSession;
43  import org.apache.hc.core5.reactor.ListenerEndpoint;
44  import org.apache.hc.core5.reactor.ProtocolIOSession;
45  import org.apache.hc.core5.util.TimeValue;
46  import org.apache.hc.core5.util.Timeout;
47  import org.junit.After;
48  import org.junit.Assert;
49  import org.junit.Before;
50  import org.junit.Test;
51  
52  /**
53   * Basic tests for {@link DefaultListeningIOReactor}.
54   */
55  public class TestDefaultListeningIOReactor {
56  
57      private DefaultListeningIOReactor ioReactor;
58  
59      private static class NoopIOEventHandlerFactory implements IOEventHandlerFactory {
60  
61          @Override
62          public IOEventHandler createHandler(final ProtocolIOSession ioSession, final Object attachment) {
63              return new IOEventHandler() {
64  
65                  @Override
66                  public void connected(final IOSession session) {
67                  }
68  
69                  @Override
70                  public void inputReady(final IOSession session, final ByteBuffer src) {
71                  }
72  
73                  @Override
74                  public void outputReady(final IOSession session) {
75                  }
76  
77                  @Override
78                  public void timeout(final IOSession session, final Timeout timeout) {
79                  }
80  
81                  @Override
82                  public void exception(final IOSession session, final Exception cause) {
83                  }
84  
85                  @Override
86                  public void disconnected(final IOSession session) {
87                  }
88              };
89          }
90      }
91  
92      @Before
93      public void setup() throws Exception {
94          final IOReactorConfig reactorConfig = IOReactorConfig.custom()
95                  .setIoThreadCount(1)
96                  .build();
97          this.ioReactor = new DefaultListeningIOReactor(new NoopIOEventHandlerFactory(), reactorConfig, null);
98      }
99  
100     @After
101     public void cleanup() throws Exception {
102         if (this.ioReactor != null) {
103             this.ioReactor.close(CloseMode.IMMEDIATE);
104         }
105     }
106 
107     @Test
108     public void testEndpointUpAndDown() throws Exception {
109         ioReactor.start();
110 
111         Set<ListenerEndpoint> endpoints = ioReactor.getEndpoints();
112         Assert.assertNotNull(endpoints);
113         Assert.assertEquals(0, endpoints.size());
114 
115         final Future<ListenerEndpoint> future1 = ioReactor.listen(new InetSocketAddress(0));
116         final ListenerEndpoint endpoint1 = future1.get();
117 
118         final Future<ListenerEndpoint> future2 = ioReactor.listen(new InetSocketAddress(0));
119         final ListenerEndpoint endpoint2 = future2.get();
120         final int port = ((InetSocketAddress) endpoint2.getAddress()).getPort();
121 
122         endpoints = ioReactor.getEndpoints();
123         Assert.assertNotNull(endpoints);
124         Assert.assertEquals(2, endpoints.size());
125 
126         endpoint1.close();
127 
128         endpoints = ioReactor.getEndpoints();
129         Assert.assertNotNull(endpoints);
130         Assert.assertEquals(1, endpoints.size());
131 
132         final ListenerEndpoint endpoint = endpoints.iterator().next();
133 
134         Assert.assertEquals(port, ((InetSocketAddress) endpoint.getAddress()).getPort());
135 
136         ioReactor.close(CloseMode.GRACEFUL);
137         ioReactor.awaitShutdown(TimeValue.ofSeconds(5));
138         Assert.assertEquals(IOReactorStatus.SHUT_DOWN, ioReactor.getStatus());
139     }
140 
141     @Test
142     public void testEndpointAlreadyBound() throws Exception {
143         ioReactor.start();
144 
145         final Future<ListenerEndpoint> future1 = ioReactor.listen(new InetSocketAddress(0));
146         final ListenerEndpoint endpoint1 = future1.get();
147         final int port = ((InetSocketAddress) endpoint1.getAddress()).getPort();
148 
149         final Future<ListenerEndpoint> future2 = ioReactor.listen(new InetSocketAddress(port));
150         try {
151             future2.get();
152             Assert.fail("ExecutionException expected");
153         } catch (final ExecutionException expected) {
154         }
155         ioReactor.close(CloseMode.GRACEFUL);
156         ioReactor.awaitShutdown(TimeValue.ofSeconds(5));
157 
158         Assert.assertEquals(IOReactorStatus.SHUT_DOWN, ioReactor.getStatus());
159     }
160 
161 }