Skip to content

Commit 35f2ff6

Browse files
authored
Clarify the exception message when using a closed session (#15811)
1 parent b3435b7 commit 35f2ff6

7 files changed

Lines changed: 270 additions & 155 deletions

File tree

integration-test/src/test/java/org/apache/iotdb/session/it/SessionIT.java

Lines changed: 110 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@
2121
import org.apache.iotdb.isession.ISession;
2222
import org.apache.iotdb.isession.SessionDataSet;
2323
import org.apache.iotdb.it.env.EnvFactory;
24+
import org.apache.iotdb.it.env.cluster.node.DataNodeWrapper;
2425
import org.apache.iotdb.it.framework.IoTDBTestRunner;
2526
import org.apache.iotdb.itbase.category.ClusterIT;
2627
import org.apache.iotdb.itbase.category.LocalStandaloneIT;
28+
import org.apache.iotdb.rpc.IoTDBConnectionException;
2729
import org.apache.iotdb.rpc.StatementExecutionException;
30+
import org.apache.iotdb.session.Session;
31+
import org.apache.iotdb.session.Session.Builder;
2832

2933
import org.apache.tsfile.enums.TSDataType;
3034
import org.apache.tsfile.file.metadata.enums.CompressionType;
@@ -45,9 +49,11 @@
4549
import java.time.LocalDate;
4650
import java.util.ArrayList;
4751
import java.util.Arrays;
52+
import java.util.Collections;
4853
import java.util.HashSet;
4954
import java.util.List;
5055

56+
import static org.junit.Assert.assertEquals;
5157
import static org.junit.Assert.fail;
5258

5359
@RunWith(IoTDBTestRunner.class)
@@ -107,10 +113,10 @@ public void testInsertByStrAndSelectFailedData() {
107113
int i = 0;
108114
while (dataSet.hasNext()) {
109115
RowRecord record = dataSet.next();
110-
Assert.assertEquals(i, record.getFields().get(0).getLongV());
116+
assertEquals(i, record.getFields().get(0).getLongV());
111117
Assert.assertNull(record.getFields().get(1).getDataType());
112118
Assert.assertNull(record.getFields().get(2).getDataType());
113-
Assert.assertEquals(i, record.getFields().get(3).getDoubleV(), 0.00001);
119+
assertEquals(i, record.getFields().get(3).getDoubleV(), 0.00001);
114120
i++;
115121
}
116122

@@ -147,30 +153,30 @@ public void testInsertRecord() {
147153
}
148154
try (SessionDataSet dataSet = session.executeQueryStatement("select * from root.db.d1")) {
149155
HashSet<String> columnNames = new HashSet<>(dataSet.getColumnNames());
150-
Assert.assertEquals(5, columnNames.size());
156+
assertEquals(5, columnNames.size());
151157
for (int i = 0; i < 4; i++) {
152158
Assert.assertTrue(columnNames.contains(deviceId + "." + measurements.get(i)));
153159
}
154160
dataSet.setFetchSize(1024); // default is 10000
155161
int row = 10;
156162
while (dataSet.hasNext()) {
157163
RowRecord record = dataSet.next();
158-
Assert.assertEquals(row, record.getTimestamp());
164+
assertEquals(row, record.getTimestamp());
159165
List<Field> fields = record.getFields();
160-
Assert.assertEquals(4, fields.size());
166+
assertEquals(4, fields.size());
161167
for (int i = 0; i < 4; i++) {
162168
switch (fields.get(i).getDataType()) {
163169
case DATE:
164-
Assert.assertEquals(LocalDate.of(2024, 1, row), fields.get(i).getDateV());
170+
assertEquals(LocalDate.of(2024, 1, row), fields.get(i).getDateV());
165171
break;
166172
case TIMESTAMP:
167-
Assert.assertEquals(row, fields.get(i).getLongV());
173+
assertEquals(row, fields.get(i).getLongV());
168174
break;
169175
case BLOB:
170176
Assert.assertArrayEquals(bytes, fields.get(i).getBinaryV().getValues());
171177
break;
172178
case STRING:
173-
Assert.assertEquals("" + row, fields.get(i).getBinaryV().toString());
179+
assertEquals("" + row, fields.get(i).getBinaryV().toString());
174180
break;
175181
default:
176182
fail("Unsupported data type");
@@ -179,7 +185,7 @@ public void testInsertRecord() {
179185
}
180186
row++;
181187
}
182-
Assert.assertEquals(20, row);
188+
assertEquals(20, row);
183189
}
184190

185191
} catch (Exception e) {
@@ -215,7 +221,7 @@ public void testInsertStrRecord() {
215221
}
216222
try (SessionDataSet dataSet = session.executeQueryStatement("select * from root.db.d1")) {
217223
HashSet<String> columnNames = new HashSet<>(dataSet.getColumnNames());
218-
Assert.assertEquals(5, columnNames.size());
224+
assertEquals(5, columnNames.size());
219225
for (int i = 0; i < 4; i++) {
220226
Assert.assertTrue(columnNames.contains(deviceId + "." + measurements.get(i)));
221227
}
@@ -224,22 +230,22 @@ public void testInsertStrRecord() {
224230
while (dataSet.hasNext()) {
225231
RowRecord record = dataSet.next();
226232
System.out.println(record);
227-
Assert.assertEquals(row, record.getTimestamp());
233+
assertEquals(row, record.getTimestamp());
228234
List<Field> fields = record.getFields();
229-
Assert.assertEquals(4, fields.size());
235+
assertEquals(4, fields.size());
230236
for (int i = 0; i < 4; i++) {
231237
switch (fields.get(i).getDataType()) {
232238
case DATE:
233-
Assert.assertEquals(LocalDate.of(2024, 1, row), fields.get(i).getDateV());
239+
assertEquals(LocalDate.of(2024, 1, row), fields.get(i).getDateV());
234240
break;
235241
case TIMESTAMP:
236-
Assert.assertEquals(row, fields.get(i).getLongV());
242+
assertEquals(row, fields.get(i).getLongV());
237243
break;
238244
case BLOB:
239245
Assert.assertArrayEquals(bytes, fields.get(i).getBinaryV().getValues());
240246
break;
241247
case STRING:
242-
Assert.assertEquals("" + row, fields.get(i).getBinaryV().toString());
248+
assertEquals("" + row, fields.get(i).getBinaryV().toString());
243249
break;
244250
default:
245251
fail("Unsupported data type");
@@ -248,7 +254,7 @@ public void testInsertStrRecord() {
248254
}
249255
row++;
250256
}
251-
Assert.assertEquals(20, row);
257+
assertEquals(20, row);
252258
}
253259

254260
} catch (Exception e) {
@@ -295,7 +301,7 @@ public void testInsertTablet() {
295301
tablet.reset();
296302
try (SessionDataSet dataSet = session.executeQueryStatement("select * from root.db.d1")) {
297303
HashSet<String> columnNames = new HashSet<>(dataSet.getColumnNames());
298-
Assert.assertEquals(5, columnNames.size());
304+
assertEquals(5, columnNames.size());
299305
for (int i = 0; i < 4; i++) {
300306
Assert.assertTrue(
301307
columnNames.contains(deviceId + "." + schemaList.get(i).getMeasurementName()));
@@ -304,22 +310,22 @@ public void testInsertTablet() {
304310
int row = 10;
305311
while (dataSet.hasNext()) {
306312
RowRecord record = dataSet.next();
307-
Assert.assertEquals(row, record.getTimestamp());
313+
assertEquals(row, record.getTimestamp());
308314
List<Field> fields = record.getFields();
309-
Assert.assertEquals(4, fields.size());
315+
assertEquals(4, fields.size());
310316
for (int i = 0; i < 4; i++) {
311317
switch (fields.get(i).getDataType()) {
312318
case DATE:
313-
Assert.assertEquals(LocalDate.of(2024, 1, row), fields.get(i).getDateV());
319+
assertEquals(LocalDate.of(2024, 1, row), fields.get(i).getDateV());
314320
break;
315321
case TIMESTAMP:
316-
Assert.assertEquals(row, fields.get(i).getLongV());
322+
assertEquals(row, fields.get(i).getLongV());
317323
break;
318324
case BLOB:
319325
Assert.assertArrayEquals(bytes, fields.get(i).getBinaryV().getValues());
320326
break;
321327
case STRING:
322-
Assert.assertEquals("" + row, fields.get(i).getBinaryV().toString());
328+
assertEquals("" + row, fields.get(i).getBinaryV().toString());
323329
break;
324330
default:
325331
fail("Unsupported data type");
@@ -328,11 +334,92 @@ public void testInsertTablet() {
328334
}
329335
row++;
330336
}
331-
Assert.assertEquals(20, row);
337+
assertEquals(20, row);
332338
}
333339
} catch (Exception e) {
334340
e.printStackTrace();
335341
fail(e.getMessage());
336342
}
337343
}
344+
345+
@Test
346+
public void testSessionMisuse() throws StatementExecutionException, IoTDBConnectionException {
347+
final DataNodeWrapper dataNode = EnvFactory.getEnv().getDataNodeWrapperList().get(0);
348+
final Session session = new Builder().host(dataNode.getIp()).port(dataNode.getPort()).build();
349+
// operate before open
350+
try {
351+
session.executeNonQueryStatement("INSERT INTO root.db1.d1 (time, s1) VALUES (1,1)");
352+
} catch (IoTDBConnectionException e) {
353+
assertEquals("Session is not open, please invoke Session.open() first", e.getMessage());
354+
}
355+
356+
try (SessionDataSet ignored = session.executeQueryStatement("SELECT * FROM root.**")) {
357+
} catch (IoTDBConnectionException e) {
358+
assertEquals("Session is not open, please invoke Session.open() first", e.getMessage());
359+
}
360+
361+
try {
362+
session.deleteData("root.ab", 100);
363+
} catch (IoTDBConnectionException e) {
364+
assertEquals("Session is not open, please invoke Session.open() first", e.getMessage());
365+
}
366+
367+
try {
368+
session.insertTablet(new Tablet("root.db1.d1", Collections.emptyList()));
369+
} catch (IoTDBConnectionException e) {
370+
assertEquals("Session is not open, please invoke Session.open() first", e.getMessage());
371+
}
372+
373+
try {
374+
session.deleteDatabase("root.db");
375+
} catch (IoTDBConnectionException e) {
376+
assertEquals("Session is not open, please invoke Session.open() first", e.getMessage());
377+
}
378+
379+
// close before open
380+
try {
381+
session.close();
382+
} catch (IoTDBConnectionException e) {
383+
assertEquals("Session is not open, please invoke Session.open() first", e.getMessage());
384+
}
385+
386+
// operate after close
387+
session.open();
388+
session.close();
389+
390+
try {
391+
session.executeNonQueryStatement("INSERT INTO root.db1.d1 (time, s1) VALUES (1,1)");
392+
} catch (IoTDBConnectionException e) {
393+
assertEquals("Session is not open, please invoke Session.open() first", e.getMessage());
394+
}
395+
396+
try (SessionDataSet ignored = session.executeQueryStatement("SELECT * FROM root.**")) {
397+
} catch (IoTDBConnectionException e) {
398+
assertEquals("Session is not open, please invoke Session.open() first", e.getMessage());
399+
}
400+
401+
try {
402+
session.deleteData("root.ab", 100);
403+
} catch (IoTDBConnectionException e) {
404+
assertEquals("Session is not open, please invoke Session.open() first", e.getMessage());
405+
}
406+
407+
try {
408+
session.insertTablet(
409+
new Tablet(
410+
"root.db1.d1",
411+
Collections.singletonList(new MeasurementSchema("s1", TSDataType.INT64))));
412+
} catch (IoTDBConnectionException e) {
413+
assertEquals("Session is not open, please invoke Session.open() first", e.getMessage());
414+
}
415+
416+
try {
417+
session.deleteDatabase("root.db");
418+
} catch (IoTDBConnectionException e) {
419+
assertEquals("Session is not open, please invoke Session.open() first", e.getMessage());
420+
}
421+
422+
// double close is okay
423+
session.close();
424+
}
338425
}

iotdb-client/isession/src/main/java/org/apache/iotdb/isession/ISession.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ void open(
7676

7777
void close() throws IoTDBConnectionException;
7878

79-
String getTimeZone();
79+
String getTimeZone() throws IoTDBConnectionException;
8080

8181
void setTimeZone(String zoneId) throws StatementExecutionException, IoTDBConnectionException;
8282

83-
void setTimeZoneOfSession(String zoneId);
83+
void setTimeZoneOfSession(String zoneId) throws IoTDBConnectionException;
8484

8585
/**
8686
* @deprecated Use {@link #createDatabase(String)} instead.
@@ -244,7 +244,7 @@ void insertAlignedRecord(
244244
void insertRecord(String deviceId, long time, List<String> measurements, List<String> values)
245245
throws IoTDBConnectionException, StatementExecutionException;
246246

247-
String getTimestampPrecision() throws TException;
247+
String getTimestampPrecision() throws TException, IoTDBConnectionException;
248248

249249
void insertAlignedRecord(
250250
String deviceId, long time, List<String> measurements, List<String> values)

0 commit comments

Comments
 (0)