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.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
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 }