-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatabaseTest.java
More file actions
229 lines (210 loc) · 9.12 KB
/
DatabaseTest.java
File metadata and controls
229 lines (210 loc) · 9.12 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package com.ladybugdb;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.Test;
public class DatabaseTest extends TestBase {
@Test
void DBCreationAndDestroyWithArgs() {
String dbPath = "";
try {
dbPath = tempDir.resolve("db1.lbdb").toString();
} catch (Exception e) {
fail("Cannot get database path: " + e.getMessage());
}
try (Database database = new Database(
dbPath,
1 << 28 /* 256 MB */,
true /* compression */,
false /* readOnly */,
1 << 30 /* 1 GB */,
false /* autoCheckpoint */,
1234 /* checkpointThreshold */,
true /* throwOnWalReplayFailure */,
true /* enableChecksums */)) {
Connection conn = new Connection(database);
{
QueryResult result = conn.query("CALL current_setting('auto_checkpoint') RETURN *");
assertEquals(result.getNext().getValue(0).toString(), "False");
}
{
QueryResult result = conn.query("CALL current_setting('checkpoint_threshold') RETURN *");
assertEquals(result.getNext().getValue(0).toString(), "1234");
}
conn.close();
// Database will be automatically destroyed after this block
} catch (Exception e) {
fail("DBCreationAndDestroyWithArgs failed: " + e.getMessage());
}
}
@Test
void DBCreationWithInvalidMaxDBSize() {
String dbPath = "";
try {
dbPath = tempDir.resolve("db2.lbdb").toString();
} catch (Exception e) {
fail("Cannot get database path: " + e.getMessage());
}
try {
Database database = new Database(
dbPath,
1 << 28 /* 256 MB */,
true /* compression */,
false /* readOnly */,
(1 << 30) - 1 /* 1 GB - 1 Byte (Odd Number)*/,
false /* autoCheckpoint */,
1234 /* checkpointThreshold */,
true /* throwOnWalReplayFailure */,
true /* enableChecksums */);
fail("DBCreationWithInvalidMaxDBSize failed:");
}
catch (Exception e) {
assertEquals("Buffer manager exception: The given max db size should be a power of 2.", e.getMessage());
return;
}
fail("DBCreationWithInvalidMaxDBSize failed:");
}
@Test
void DBCreationAndDestroyWithPathOnly() {
String dbPath = "";
try {
dbPath = tempDir.resolve("db3.lbdb").toString();
} catch (Exception e) {
fail("Cannot get database path: " + e.getMessage());
}
try (Database database = new Database(dbPath)) {
// check default config
QueryResult result = conn.query("CALL current_setting('checkpoint_threshold') RETURN *");
String checkpointThresholdStr = result.getNext().getValue(0).toString();
assertTrue(Long.parseLong(checkpointThresholdStr) > 0);
} catch (Exception e) {
fail("DBCreationAndDestroyWithPathOnly failed: " + e.getMessage());
}
}
@Test
void DBCreationAndDestroyWithNoParam() {
try (Database database = new Database()) {
} catch (Exception e) {
fail("DBCreationAndDestroyWithNoParam failed: " + e.getMessage());
}
}
@Test
void DBDestroyBeforeConnectionAndQueryResult(){
Database database = new Database(":memory:", 1 << 28, true, false, 1 << 30, false, 0, true, true);
Connection conn = new Connection(database);
QueryResult result = conn.query("RETURN 1");
assertTrue(result.hasNext());
assertEquals(result.getNext().getValue(0).toString(), "1");
database.close();
try {
result.getNext();
fail("DBDestroyBeforeConnectionAndQueryResult failed: QueryResult should not be usable after database is closed.");
} catch (Exception e) {
assertEquals("Runtime exception: The current operation is not allowed because the parent database is closed.", e.getMessage());
}
try {
conn.query("RETURN 1");
fail("DBDestroyBeforeConnectionAndQueryResult failed: Connection should not be usable after database is closed.");
} catch (Exception e) {
assertEquals("Runtime exception: The current operation is not allowed because the parent database is closed.", e.getMessage());
}
result.close();
conn.close();
}
@Test
void DBCreationTestThrowOnWALReplayFailure() {
String dbPath = "";
try {
dbPath = tempDir.resolve("db4.lbdb").toString();
} catch (Exception e) {
fail("Cannot get database path: " + e.getMessage());
}
// Create a DB, don't checkpoint and leave a WAL
try (Database database = new Database(
dbPath)) {
Connection conn = new Connection(database);
{
conn.query("call force_checkpoint_on_close=false");
conn.query("call auto_checkpoint=false");
QueryResult result = conn.query("create node table testtest1(id int64 primary key)");
assertTrue(result.isSuccess());
result = conn.query("call show_tables() where prefix(name, 'testtest') return name");
assertEquals(result.getNext().getValue(0).toString(), "testtest1");
// Trigger an exception, this will also trigger during replay
result = conn.query("create node table testtest1(id int64 primary key)");
assertFalse(result.isSuccess());
}
conn.close();
// Database will be automatically destroyed after this block
} catch (Exception e) {
fail("DBCreationTestThrowOnWALReplayFailure failed: " + e.getMessage());
}
try (Database database = new Database(
dbPath,
1 << 28 /* 256 MB */,
true /* compression */,
false /* readOnly */,
1 << 30 /* 1 GB */,
false /* autoCheckpoint */,
1234 /* checkpointThreshold */,
false /* throwOnWalReplayFailure */,
true /* enableChecksums */)) {
// Database will be automatically destroyed after this block
Connection conn = new Connection(database);
QueryResult result = conn.query("call show_tables() where prefix(name, 'testtest') return name");
assertTrue(result.isSuccess());
assertTrue(result.hasNext());
assertEquals(result.getNext().getValue(0).toString(), "testtest1");
} catch (Exception e) {
fail("DBCreationTestThrowOnWALReplayFailure failed: " + e.getMessage());
}
}
@Test
void DBCreationTestEnableChecksums() {
String dbPath = "";
try {
dbPath = tempDir.resolve("db5.lbdb").toString();
} catch (Exception e) {
fail("Cannot get database path: " + e.getMessage());
}
// Create a DB, don't checkpoint and leave a WAL
try (Database database = new Database(
dbPath,
1 << 28 /* 256 MB */,
true /* compression */,
false /* readOnly */,
1 << 30 /* 1 GB */,
false /* autoCheckpoint */,
1234 /* checkpointThreshold */,
true /* throwOnWalReplayFailure */,
true /* enableChecksums */)) {
Connection conn = new Connection(database);
{
conn.query("call force_checkpoint_on_close=false");
conn.query("call auto_checkpoint=false");
QueryResult result = conn.query("create node table testtest1(id int64 primary key)");
assertTrue(result.isSuccess());
}
conn.close();
// Database will be automatically destroyed after this block
} catch (Exception e) {
fail("DBCreationTestEnableChecksums failed: " + e.getMessage());
}
try (Database database = new Database(
dbPath,
1 << 28 /* 256 MB */,
true /* compression */,
false /* readOnly */,
1 << 30 /* 1 GB */,
false /* autoCheckpoint */,
1234 /* checkpointThreshold */,
true /* throwOnWalReplayFailure */,
false /* enableChecksums */)) {
// The database creation should fail
assertTrue(false);
} catch (Exception e) {
assertTrue(e.getMessage().contains("Please open your database using the correct enableChecksums config."));
}
}
}