-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathTestNetty4Transport.java
136 lines (114 loc) · 4.98 KB
/
TestNetty4Transport.java
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.sdk;
import java.io.IOException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.opensearch.common.lifecycle.Lifecycle;
import org.opensearch.common.settings.Settings;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.transport.TransportSettings;
import org.opensearch.transport.netty4.Netty4Transport;
public class TestNetty4Transport extends OpenSearchTestCase {
private ThreadPool threadPool;
private ExtensionsRunner extensionsRunner;
private NettyTransport nettyTransport;
@BeforeEach
public void setUp() throws IOException {
this.threadPool = new TestThreadPool("test");
this.extensionsRunner = new ExtensionsRunnerForTest();
this.nettyTransport = new NettyTransport(extensionsRunner);
}
// test Netty can bind to multiple ports, default and additional client
@Test
public void testNettyCanBindToMultiplePorts() throws IOException {
Settings settings = Settings.builder()
.put("node.name", "netty_test")
.put(TransportSettings.BIND_HOST.getKey(), "127.0.0.1")
.put("transport.profiles.default.port", 0)
.put("transport.profiles.client1.port", 0)
.build();
Netty4Transport transport = nettyTransport.getNetty4Transport(settings, threadPool);
try {
startNetty4Transport(transport);
assertEquals(1, transport.profileBoundAddresses().size());
assertEquals(1, transport.boundAddress().boundAddresses().length);
} finally {
stopNetty4Transport(transport);
terminate(threadPool);
}
}
// test that default profile inherits from standard settings
@Test
public void testDefaultProfileInheritsFomStandardSettings() throws IOException {
// omit transport.profiles.default.port setting to determine if default port is automatically set
Settings settings = Settings.builder()
.put("node.name", "netty_test")
.put(TransportSettings.BIND_HOST.getKey(), "127.0.0.1")
.put("transport.profiles.client1.port", 0)
.build();
Netty4Transport transport = nettyTransport.getNetty4Transport(settings, threadPool);
try {
startNetty4Transport(transport);
assertEquals(1, transport.profileBoundAddresses().size());
assertEquals(1, transport.boundAddress().boundAddresses().length);
} finally {
stopNetty4Transport(transport);
terminate(threadPool);
}
}
// test profile without port settings fails
@Test
public void testThatProfileWithoutPortFails() throws IOException {
// settings without port for profile no_port
Settings settings = Settings.builder()
.put("node.name", "netty_test")
.put(TransportSettings.BIND_HOST.getKey(), "127.0.0.1")
.put("transport.profiles.no_port.foo", "bar")
.build();
try {
// attempt creating netty object with invalid settings
IllegalStateException ex = expectThrows(
IllegalStateException.class,
() -> nettyTransport.getNetty4Transport(settings, threadPool)
);
assertEquals("profile [no_port] has no port configured", ex.getMessage());
} finally {
terminate(threadPool);
}
}
// test default profile port overrides general config
@Test
public void testDefaultProfilePortOverridesGeneralConfiguration() throws IOException {
Settings settings = Settings.builder()
.put("node.name", "netty_test")
.put(TransportSettings.BIND_HOST.getKey(), "127.0.0.1")
.put(TransportSettings.PORT.getKey(), "22") // attempt to bind SSH port will throw exception
.put("transport.profiles.default.port", 0) // default port configuration will overwrite attempt
.build();
Netty4Transport transport = nettyTransport.getNetty4Transport(settings, threadPool);
try {
startNetty4Transport(transport);
assertEquals(0, transport.profileBoundAddresses().size());
assertEquals(1, transport.boundAddress().boundAddresses().length);
} finally {
terminate(threadPool);
}
}
// helper method to ensure netty transport was started
private void startNetty4Transport(Netty4Transport transport) {
transport.start();
assertEquals(Lifecycle.State.STARTED, transport.lifecycleState());
}
private void stopNetty4Transport(Netty4Transport transport) {
transport.close();
assertEquals(Lifecycle.State.CLOSED, transport.lifecycleState());
}
}