|
16 | 16 | * Wrapper around tarantoolctl utility.
|
17 | 17 | */
|
18 | 18 | public class TarantoolControl {
|
| 19 | + public class TarantoolControlException extends RuntimeException { |
| 20 | + int code; |
| 21 | + String stdout; |
| 22 | + String stderr; |
| 23 | + |
| 24 | + TarantoolControlException(int code, String stdout, String stderr) { |
| 25 | + super("returned exitcode " + code + "\n" + |
| 26 | + "[stdout]\n" + stdout + "\n[stderr]\n" + stderr); |
| 27 | + this.code = code; |
| 28 | + this.stdout = stdout; |
| 29 | + this.stderr = stderr; |
| 30 | + } |
| 31 | + } |
| 32 | + |
19 | 33 | protected static final String tntCtlWorkDir = System.getProperty("tntCtlWorkDir",
|
20 | 34 | new File("testroot").getAbsolutePath());
|
21 | 35 | protected static final String instanceDir = new File("src/test").getAbsolutePath();
|
@@ -163,16 +177,87 @@ public void run() {
|
163 | 177 | } catch (IOException e) {
|
164 | 178 | /* No-op. */
|
165 | 179 | }
|
166 |
| - throw new RuntimeException("returned exitcode " + code + "\n" + |
167 |
| - "[stdout]\n" + stdout + "\n[stderr]\n" + stderr); |
| 180 | + throw new TarantoolControlException(code, stdout, stderr); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Wait until the instance will be started. |
| 186 | + * |
| 187 | + * Use tarantoolctl status instanceName. |
| 188 | + * |
| 189 | + * Then test the instance with TarantoolTcpConsole (ADMIN environment |
| 190 | + * variable is set) or TarantoolLocalConsole. |
| 191 | + * |
| 192 | + * XXX: Now TarantoolLocalConsole is used unconditionally, see |
| 193 | + * openConsole(). |
| 194 | + */ |
| 195 | + public void waitStarted(String instanceName) { |
| 196 | + while (status(instanceName) != 0) |
| 197 | + sleep(); |
| 198 | + |
| 199 | + while (true) { |
| 200 | + try { |
| 201 | + openConsole(instanceName).close(); |
| 202 | + break; |
| 203 | + } catch (Exception ignored) { |
| 204 | + /* No-op. */ |
| 205 | + } |
| 206 | + sleep(); |
168 | 207 | }
|
169 | 208 | }
|
170 | 209 |
|
| 210 | + /** |
| 211 | + * Wait until the instance will be stopped. |
| 212 | + * |
| 213 | + * Use tarantoolctl status instanceName. |
| 214 | + */ |
| 215 | + public void waitStopped(String instanceName) { |
| 216 | + while (status(instanceName) != 1) |
| 217 | + sleep(); |
| 218 | + } |
| 219 | + |
171 | 220 | public void start(String instanceName) {
|
172 | 221 | executeCommand("start", instanceName);
|
173 | 222 | }
|
174 | 223 |
|
175 | 224 | public void stop(String instanceName) {
|
176 | 225 | executeCommand("stop", instanceName);
|
177 | 226 | }
|
| 227 | + |
| 228 | + /** |
| 229 | + * Wrapper for `tarantoolctl status instanceName`. |
| 230 | + * |
| 231 | + * Return exit code of the command: |
| 232 | + * |
| 233 | + * * 0 -- started; |
| 234 | + * * 1 -- stopped; |
| 235 | + * * 2 -- pid file exists, control socket inaccessible. |
| 236 | + */ |
| 237 | + public int status(String instanceName) { |
| 238 | + try { |
| 239 | + executeCommand("status", instanceName); |
| 240 | + } catch (TarantoolControlException e) { |
| 241 | + return e.code; |
| 242 | + } |
| 243 | + |
| 244 | + return 0; |
| 245 | + } |
| 246 | + |
| 247 | + /* |
| 248 | + * XXX: This function is planned to use text console (from ADMIN |
| 249 | + * environment variable) when it is available for the instance and |
| 250 | + * fallback to TarantoolLocalConsole. |
| 251 | + */ |
| 252 | + public TarantoolConsole openConsole(String instanceName) { |
| 253 | + return TarantoolConsole.open(tntCtlWorkDir, instanceName); |
| 254 | + } |
| 255 | + |
| 256 | + public static void sleep() { |
| 257 | + try { |
| 258 | + Thread.sleep(100); |
| 259 | + } catch (InterruptedException e) { |
| 260 | + throw new RuntimeException(e); |
| 261 | + } |
| 262 | + } |
178 | 263 | }
|
0 commit comments