Skip to content

Commit 495e458

Browse files
ztarvosTotktonada
authored andcommitted
jdbc: fix NPE in update
Updated constants to be in sync with tarantool IPROTO. Added integration tests for the working JDBC functionality. Fixed travis script to use tarantool 2.0 which is required for SQL. Fixed failing user permission calls in tarantool initialization script. Closes #39 ---- Rebase fixes (Alexander Turenko): * Perform cleanup of the test directory once before all tests (move it from the TarantoolControl constructor). * Start tarantool from the Java code in JDBC tests (use TarantoolControl). * Wait for the instance to be started / stopped in setupEnv() / cleanupEnv(); add waiting into startTarantool() / stopTarantool() also should fix races in testLongParallelCloseReconnects when an instance tried to be started against an instance that is already run.
1 parent 867d6f4 commit 495e458

11 files changed

+741
-34
lines changed

src/main/java/org/tarantool/Key.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ public enum Key implements Callable<Integer> {
1818
UPSERT_OPS(0x28),
1919
DATA(0x30), ERROR(0x31),
2020

21-
SQL_FIELD_NAME(0x29),
21+
SQL_FIELD_NAME(0),
2222
SQL_METADATA(0x32),
2323
SQL_TEXT(0x40),
2424
SQL_BIND(0x41),
2525
SQL_OPTIONS(0x42),
26-
SQL_INFO(0x43),
27-
SQL_ROW_COUNT(0x44);
26+
SQL_INFO(0x42),
27+
SQL_ROW_COUNT(0);
2828

2929
int id;
3030

src/test/java/org/tarantool/AbstractTarantoolConnectorIT.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public abstract class AbstractTarantoolConnectorIT {
8282
@BeforeAll
8383
public static void setupEnv() {
8484
control = new TarantoolControl();
85-
control.start("jdk-testing");
85+
startTarantool("jdk-testing");
8686

8787
console = openConsole();
8888

@@ -104,7 +104,7 @@ public static void cleanupEnv() {
104104

105105
console.close();
106106
} finally {
107-
control.stop("jdk-testing");
107+
stopTarantool("jdk-testing");
108108
}
109109
}
110110

@@ -189,12 +189,14 @@ protected List<?> consoleSelect(String spaceName, Object key) {
189189
return console.eval(sb.toString());
190190
}
191191

192-
protected void stopTarantool(String instance) {
192+
protected static void stopTarantool(String instance) {
193193
control.stop(instance);
194+
control.waitStopped("jdk-testing");
194195
}
195196

196-
protected void startTarantool(String instance) {
197+
protected static void startTarantool(String instance) {
197198
control.start(instance);
199+
control.waitStarted("jdk-testing");
198200
}
199201

200202
/**

src/test/java/org/tarantool/TarantoolControl.java

+111-23
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,66 @@
1616
* Wrapper around tarantoolctl utility.
1717
*/
1818
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+
1933
protected static final String tntCtlWorkDir = System.getProperty("tntCtlWorkDir",
2034
new File("testroot").getAbsolutePath());
2135
protected static final String instanceDir = new File("src/test").getAbsolutePath();
2236
protected static final String tarantoolCtlConfig = new File("src/test/.tarantoolctl").getAbsolutePath();
2337
protected static final int RESTART_TIMEOUT = 2000;
2438

39+
static {
40+
try {
41+
setupWorkDirectory();
42+
} catch (IOException e) {
43+
throw new RuntimeException("Can't setup test root directory!", e);
44+
}
45+
}
46+
47+
protected static void setupWorkDirectory() throws IOException {
48+
try {
49+
rmdir(tntCtlWorkDir);
50+
} catch (IOException ignored) {
51+
/* No-op. */
52+
}
53+
54+
mkdir(tntCtlWorkDir);
55+
for (File c : new File(instanceDir).listFiles())
56+
if (c.getName().endsWith(".lua"))
57+
copyFile(c, tntCtlWorkDir);
58+
copyFile(tarantoolCtlConfig, tntCtlWorkDir);
59+
}
60+
2561
// Based on https://stackoverflow.com/a/779529
26-
private void rmdir(File f) throws IOException {
62+
private static void rmdir(File f) throws IOException {
2763
if (f.isDirectory()) {
2864
for (File c : f.listFiles())
2965
rmdir(c);
3066
}
3167
f.delete();
3268
}
3369

34-
private void rmdir(String f) throws IOException {
70+
private static void rmdir(String f) throws IOException {
3571
rmdir(new File(f));
3672
}
3773

38-
private void mkdir(File f) throws IOException {
74+
private static void mkdir(File f) throws IOException {
3975
f.mkdirs();
4076
}
4177

42-
private void mkdir(String f) throws IOException {
78+
private static void mkdir(String f) throws IOException {
4379
mkdir(new File(f));
4480
}
4581

@@ -79,23 +115,6 @@ private static String loadStream(InputStream s) throws IOException {
79115
return sb.toString();
80116
}
81117

82-
protected void setupWorkDirectory() throws IOException {
83-
rmdir(tntCtlWorkDir);
84-
mkdir(tntCtlWorkDir);
85-
for (File c : new File(instanceDir).listFiles())
86-
if (c.getName().endsWith(".lua"))
87-
copyFile(c, tntCtlWorkDir);
88-
copyFile(tarantoolCtlConfig, tntCtlWorkDir);
89-
}
90-
91-
TarantoolControl() {
92-
try {
93-
setupWorkDirectory();
94-
} catch (IOException e) {
95-
throw new RuntimeException(e);
96-
}
97-
}
98-
99118
/**
100119
* Control the given tarantool instance via tarantoolctl utility.
101120
*
@@ -158,16 +177,85 @@ public void run() {
158177
} catch (IOException e) {
159178
/* No-op. */
160179
}
161-
throw new RuntimeException("returned exitcode " + code + "\n" +
162-
"[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 connection using text console (TarantoolTcpConsole) to ensure
190+
* the instance was entirely started or fallback to binary port
191+
* (TarantoolLocalConsole) when the test console was not opened.
192+
*/
193+
public void waitStarted(String instanceName) {
194+
while (status(instanceName) != 0)
195+
sleep();
196+
197+
while (true) {
198+
try {
199+
openConsole(instanceName).close();
200+
break;
201+
} catch (Exception ignored) {
202+
/* No-op. */
203+
}
204+
sleep();
163205
}
164206
}
165207

208+
/**
209+
* Wait until the instance will be stopped.
210+
*
211+
* Use tarantoolctl status instanceName.
212+
*/
213+
public void waitStopped(String instanceName) {
214+
while (status(instanceName) != 1)
215+
sleep();
216+
}
217+
166218
public void start(String instanceName) {
167219
executeCommand("start", instanceName);
168220
}
169221

170222
public void stop(String instanceName) {
171223
executeCommand("stop", instanceName);
172224
}
225+
226+
/**
227+
* Wrapper for `tarantoolctl status instanceName`.
228+
*
229+
* Return exit code of the command:
230+
*
231+
* * 0 -- started;
232+
* * 1 -- stopped;
233+
* * 2 -- pid file exists, control socket inaccessible.
234+
*/
235+
public int status(String instanceName) {
236+
try {
237+
executeCommand("status", instanceName);
238+
} catch (TarantoolControlException e) {
239+
return e.code;
240+
}
241+
242+
return 0;
243+
}
244+
245+
/*
246+
* XXX: This function is planned to use text console (from ADMIN
247+
* environment variable) when it is availiable for the instance and
248+
* fallback to TarantoolLocalConsole.
249+
*/
250+
public TarantoolConsole openConsole(String instanceName) {
251+
return TarantoolConsole.open(tntCtlWorkDir, instanceName);
252+
}
253+
254+
public static void sleep() {
255+
try {
256+
Thread.sleep(100);
257+
} catch (InterruptedException e) {
258+
throw new RuntimeException(e);
259+
}
260+
}
173261
}

0 commit comments

Comments
 (0)