Skip to content

Commit d220ca2

Browse files
ztarvosTotktonada
authored andcommitted
add tests for basic connector functionality
Added tests for connector API. Closes #51
1 parent a96c8d9 commit d220ca2

18 files changed

+1434
-13
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
out
44
target
55
build
6+
testroot

.travis.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ before_script:
1212
- src/test/travis.pre.sh
1313

1414
script:
15-
- mvn test
16-
- sudo cat /var/log/tarantool/jdk-testing.log
15+
- mvn verify
16+
- cat testroot/jdk-testing.log

README.md

+31
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,37 @@ Remember that `TarantoolClient` adopts a
5858
[fail-fast](https://en.wikipedia.org/wiki/Fail-fast) policy
5959
when a client is not connected.
6060

61+
The `TarantoolClient` will stop functioning if your implementation of a socket
62+
channel provider raises an exception or returns a null. You will need a new
63+
instance of client to recover. Hence, you should only throw in case you have
64+
met unrecoverable error.
65+
66+
Below is an example of `SocketChannelProvider` implementation that handles short
67+
tarantool restarts.
68+
69+
```java
70+
SocketChannelProvider socketChannelProvider = new SocketChannelProvider() {
71+
@Override
72+
public SocketChannel get(int retryNumber, Throwable lastError) {
73+
long deadline = System.currentTimeMillis() + RESTART_TIMEOUT;
74+
while (!Thread.currentThread().isInterrupted()) {
75+
try {
76+
return SocketChannel.open(new InetSocketAddress("localhost", 3301));
77+
} catch (IOException e) {
78+
if (deadline < System.currentTimeMillis())
79+
throw new RuntimeException(e);
80+
try {
81+
Thread.sleep(100);
82+
} catch (InterruptedException ignored) {
83+
Thread.currentThread().interrupt();
84+
}
85+
}
86+
}
87+
throw new RuntimeException(new TimeoutException("Connect timed out."));
88+
}
89+
};
90+
```
91+
6192
4. Create a client.
6293

6394
```java

pom.xml

+19
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@
5050
<artifactId>maven-surefire-plugin</artifactId>
5151
<version>2.22.0</version>
5252
</plugin>
53+
<plugin>
54+
<groupId>org.apache.maven.plugins</groupId>
55+
<artifactId>maven-failsafe-plugin</artifactId>
56+
<version>2.22.0</version>
57+
<executions>
58+
<execution>
59+
<goals>
60+
<goal>integration-test</goal>
61+
<goal>verify</goal>
62+
</goals>
63+
</execution>
64+
</executions>
65+
</plugin>
5366
</plugins>
5467
</build>
5568

@@ -66,6 +79,12 @@
6679
<version>1.9.5</version>
6780
<scope>test</scope>
6881
</dependency>
82+
<dependency>
83+
<groupId>org.yaml</groupId>
84+
<artifactId>snakeyaml</artifactId>
85+
<version>1.23</version>
86+
<scope>test</scope>
87+
</dependency>
6988
</dependencies>
7089

7190
<parent>

src/main/java/org/tarantool/TarantoolClientImpl.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ public List exec(Code code, Object... args) {
503503

504504
@Override
505505
public void close() {
506-
throw new IllegalStateException("You should close TarantoolClient to make this");
506+
throw new IllegalStateException("You should close TarantoolClient instead.");
507507
}
508508
}
509509

@@ -525,7 +525,7 @@ public Long exec(Code code, Object... args) {
525525

526526
@Override
527527
public void close() {
528-
throw new IllegalStateException("You should close TarantoolClient to make this");
528+
throw new IllegalStateException("You should close TarantoolClient instead.");
529529
}
530530
}
531531

src/test/.tarantoolctl

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- Options for tarantoolctl.
2+
3+
local workdir = os.getenv('TEST_WORKDIR')
4+
default_cfg = {
5+
pid_file = workdir,
6+
wal_dir = workdir,
7+
memtx_dir = workdir,
8+
vinyl_dir = workdir,
9+
log = workdir,
10+
background = true,
11+
}
12+
13+
instance_dir = workdir
14+
15+
-- vim: set ft=lua :
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
package org.tarantool;
2+
3+
import org.junit.jupiter.api.AfterAll;
4+
import org.junit.jupiter.api.BeforeAll;
5+
6+
import java.io.IOException;
7+
import java.net.InetSocketAddress;
8+
import java.net.Socket;
9+
import java.util.List;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
import static org.junit.jupiter.api.Assertions.assertNotNull;
13+
import static org.junit.jupiter.api.Assertions.assertTrue;
14+
15+
/**
16+
* Abstract test. Provides environment control and frequently used functions.
17+
*/
18+
public abstract class AbstractTarantoolConnectorIT {
19+
protected static final String host = System.getProperty("tntHost", "localhost");
20+
protected static final int port = Integer.parseInt(System.getProperty("tntPort", "3301"));
21+
protected static final int consolePort = Integer.parseInt(System.getProperty("tntConsolePort", "3313"));
22+
protected static final String username = System.getProperty("tntUser", "test_admin");
23+
protected static final String password = System.getProperty("tntPass", "4pWBZmLEgkmKK5WP");
24+
25+
protected static final int TIMEOUT = 500;
26+
protected static final int RESTART_TIMEOUT = 2000;
27+
28+
protected static final SocketChannelProvider socketChannelProvider = new TestSocketChannelProvider(host, port,
29+
RESTART_TIMEOUT);
30+
31+
protected static TarantoolControl control;
32+
protected static TarantoolConsole console;
33+
34+
protected static final String SPACE_NAME = "basic_test";
35+
protected static final String MULTIPART_SPACE_NAME = "multipart_test";
36+
37+
protected static int SPACE_ID;
38+
protected static int MULTI_PART_SPACE_ID;
39+
40+
protected static int PK_INDEX_ID;
41+
protected static int MPK_INDEX_ID;
42+
protected static int VIDX_INDEX_ID;
43+
44+
private static final String[] setupScript = new String[] {
45+
"box.schema.space.create('basic_test', { format = " +
46+
"{{name = 'id', type = 'integer'}," +
47+
" {name = 'val', type = 'string'} } })",
48+
49+
"box.space.basic_test:create_index('pk', { type = 'TREE', parts = {'id'} } )",
50+
"box.space.basic_test:create_index('vidx', { type = 'TREE', unique = false, parts = {'val'} } )",
51+
52+
"box.space.basic_test:replace{1, 'one'}",
53+
"box.space.basic_test:replace{2, 'two'}",
54+
"box.space.basic_test:replace{3, 'three'}",
55+
56+
"box.schema.space.create('multipart_test', { format = " +
57+
"{{name = 'id1', type = 'integer'}," +
58+
" {name = 'id2', type = 'string'}," +
59+
" {name = 'val1', type = 'string'} } })",
60+
61+
"box.space.multipart_test:create_index('pk', { type = 'TREE', parts = {'id1', 'id2'} })",
62+
"box.space.multipart_test:create_index('vidx', { type = 'TREE', unique = false, parts = {'val1'} })",
63+
64+
"box.space.multipart_test:replace{1, 'one', 'o n e'}",
65+
"box.space.multipart_test:replace{2, 'two', 't w o'}",
66+
"box.space.multipart_test:replace{3, 'three', 't h r e e'}",
67+
68+
"function echo(...) return ... end"
69+
};
70+
71+
private static final String[] cleanScript = new String[] {
72+
"box.space.basic_test and box.space.basic_test:drop()",
73+
"box.space.multipart_test and box.space.multipart_test:drop()"
74+
};
75+
76+
@BeforeAll
77+
public static void setupEnv() {
78+
control = new TarantoolControl();
79+
control.start("jdk-testing");
80+
81+
console = openConsole();
82+
83+
executeLua(cleanScript);
84+
executeLua(setupScript);
85+
86+
SPACE_ID = console.eval("box.space.basic_test.id");
87+
PK_INDEX_ID = console.eval("box.space.basic_test.index.pk.id");
88+
VIDX_INDEX_ID = console.eval("box.space.basic_test.index.vidx.id");
89+
90+
MULTI_PART_SPACE_ID = console.eval("box.space.multipart_test.id");
91+
MPK_INDEX_ID = console.eval("box.space.multipart_test.index.pk.id");
92+
}
93+
94+
@AfterAll
95+
public static void cleanupEnv() {
96+
executeLua(cleanScript);
97+
98+
console.close();
99+
control.stop("jdk-testing");
100+
}
101+
102+
private static void executeLua(String[] exprs) {
103+
for (String expr : exprs) {
104+
console.exec(expr);
105+
}
106+
}
107+
108+
protected void checkTupleResult(Object res, List tuple) {
109+
assertNotNull(res);
110+
assertTrue(List.class.isAssignableFrom(res.getClass()));
111+
List list = (List)res;
112+
assertEquals(1, list.size());
113+
assertNotNull(list.get(0));
114+
assertTrue(List.class.isAssignableFrom(list.get(0).getClass()));
115+
assertEquals(tuple, list.get(0));
116+
}
117+
118+
protected TarantoolClient makeClient() {
119+
TarantoolClientConfig config = new TarantoolClientConfig();
120+
config.username = username;
121+
config.password = password;
122+
config.initTimeoutMillis = 1000;
123+
config.sharedBufferSize = 128;
124+
125+
return new TarantoolClientImpl(socketChannelProvider, config);
126+
}
127+
128+
protected static TarantoolConsole openConsole() {
129+
return TarantoolConsole.open(host, consolePort);
130+
}
131+
132+
protected static TarantoolConsole openConsole(String instance) {
133+
return TarantoolConsole.open(control.tntCtlWorkDir, instance);
134+
}
135+
136+
protected TarantoolConnection openConnection() {
137+
Socket socket = new Socket();
138+
try {
139+
socket.connect(new InetSocketAddress(host, port));
140+
} catch (IOException e) {
141+
throw new RuntimeException("Test failed due to invalid environment.", e);
142+
}
143+
try {
144+
return new TarantoolConnection(username, password, socket);
145+
} catch (Exception e) {
146+
try {
147+
socket.close();
148+
} catch (IOException ignored) {
149+
// No-op.
150+
}
151+
throw new RuntimeException(e);
152+
}
153+
}
154+
155+
protected List<?> consoleSelect(String spaceName, Object key) {
156+
StringBuilder sb = new StringBuilder("box.space.");
157+
sb.append(spaceName);
158+
sb.append(":select{");
159+
if (List.class.isAssignableFrom(key.getClass())) {
160+
List parts = (List)key;
161+
for (int i = 0; i < parts.size(); i++) {
162+
if (i != 0)
163+
sb.append(", ");
164+
Object k = parts.get(i);
165+
if (k.getClass().isAssignableFrom(String.class)) {
166+
sb.append('\'');
167+
sb.append(k);
168+
sb.append('\'');
169+
} else {
170+
sb.append(k);
171+
}
172+
}
173+
} else {
174+
sb.append(key);
175+
}
176+
sb.append("}");
177+
return console.eval(sb.toString());
178+
}
179+
180+
protected void stopTarantool(String instance) {
181+
control.stop(instance);
182+
}
183+
184+
protected void startTarantool(String instance) {
185+
control.start(instance);
186+
}
187+
}

0 commit comments

Comments
 (0)