-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
TestingServer.java
199 lines (180 loc) · 5.22 KB
/
TestingServer.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.curator.test;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
/**
* manages an internally running ZooKeeper server. FOR TESTING PURPOSES ONLY
*/
public class TestingServer implements Closeable
{
private final TestingZooKeeperServer testingZooKeeperServer;
private final InstanceSpec spec;
TestingZooKeeperServer getTestingZooKeeperServer() {
return testingZooKeeperServer;
}
/**
* Create the server using a random port
*
* @throws Exception errors
*/
public TestingServer() throws Exception
{
this(-1, null, true);
}
/**
* Create the server using a random port
*
* @param start True if the server should be started, false otherwise
* @throws Exception errors
*/
public TestingServer(boolean start) throws Exception
{
this(-1, null, start);
}
/**
* Create and start the server using the given port
*
* @param port the port
* @throws Exception errors
*/
public TestingServer(int port) throws Exception
{
this(port, null, true);
}
/**
* Create the server using the given port
*
* @param port the port
* @param start True if the server should be started, false otherwise
* @throws Exception errors
*/
public TestingServer(int port, boolean start) throws Exception
{
this(port, null, start);
}
/**
* Create and start the server using the given port
*
* @param port the port
* @param tempDirectory directory to use
* @throws Exception errors
*/
public TestingServer(int port, File tempDirectory) throws Exception
{
this(port, tempDirectory, true);
}
/**
* Create the server using the given port
*
* @param port the port
* @param tempDirectory directory to use
* @param start True if the server should be started, false otherwise
* @throws Exception errors
*/
public TestingServer(int port, File tempDirectory, boolean start) throws Exception
{
this(new InstanceSpec(tempDirectory, Math.max(0, port), -1, -1, true, -1), start);
}
/**
* Create the server using the given port
*
* @param spec instance details
* @param start True if the server should be started, false otherwise
* @throws Exception errors
*/
public TestingServer(InstanceSpec spec, boolean start) throws Exception
{
this.spec = spec;
testingZooKeeperServer = new TestingZooKeeperServer(new QuorumConfigBuilder(spec));
if ( start )
{
testingZooKeeperServer.start();
}
}
/**
* Return the port being used or will be used.
*
* @return port
* @throws IllegalStateException if server is configured to bind to port 0 but not started
*/
public int getPort()
{
int port = spec.getPort();
if (port > 0) {
return port;
}
return testingZooKeeperServer.getLocalPort();
}
/**
* Returns the temp directory being used
*
* @return directory
*/
public File getTempDirectory()
{
return spec.getDataDirectory();
}
/**
* Start the server
*
* @throws Exception
*/
public void start() throws Exception
{
testingZooKeeperServer.start();
}
/**
* Stop the server without deleting the temp directory
*/
public void stop() throws IOException
{
testingZooKeeperServer.stop();
}
/**
* Restart the server. If the server is currently running it will be stopped
* and restarted. If it's not currently running then it will be started. If
* it has been closed (had close() called on it) then an exception will be
* thrown.
*
* @throws Exception
*/
public void restart() throws Exception
{
testingZooKeeperServer.restart();
}
/**
* Close the server and any open clients and delete the temp directory
*/
@Override
public void close() throws IOException
{
testingZooKeeperServer.close();
}
/**
* Returns the connection string to use
*
* @return connection string
* @throws IllegalStateException if server is configured to bind to port 0 but not started
*/
public String getConnectString() {
return spec.getHostname() + ":" + getPort();
}
}