Skip to content

Commit 19f683e

Browse files
committed
Refactor test cleanup logic and improve resilience
Moved task cleanup logic to a shared method in BaseApiTest for reuse and improved error handling during cleanup. TaskApiTest now also cleans up the 'man' vertex label after tests. Cleanup methods are more resilient to exceptions, ensuring test environments are reliably reset.
1 parent bcb6bbd commit 19f683e

File tree

3 files changed

+57
-53
lines changed

3 files changed

+57
-53
lines changed

hugegraph-client/src/test/java/org/apache/hugegraph/api/BaseApiTest.java

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -103,43 +103,48 @@ public static void clear() throws Exception {
103103

104104
protected static void clearData() {
105105
// Clear edge
106-
edgeAPI.list(-1).results().forEach(edge -> {
107-
edgeAPI.delete(edge.id());
108-
});
106+
edgeAPI.list(-1).results().forEach(edge -> edgeAPI.delete(edge.id()));
107+
109108
// Clear vertex
110-
vertexAPI.list(-1).results().forEach(vertex -> {
111-
vertexAPI.delete(vertex.id());
112-
});
109+
vertexAPI.list(-1).results().forEach(vertex -> vertexAPI.delete(vertex.id()));
113110

114-
// Clear schema
111+
// Clear schema (order matters: index -> edge -> vertex -> property)
115112
List<Long> ilTaskIds = new ArrayList<>();
116-
indexLabelAPI.list().forEach(indexLabel -> {
117-
ilTaskIds.add(indexLabelAPI.delete(indexLabel.name()));
118-
});
113+
indexLabelAPI.list().forEach(il -> ilTaskIds.add(indexLabelAPI.delete(il.name())));
119114
ilTaskIds.forEach(BaseApiTest::waitUntilTaskCompleted);
120115

121116
List<Long> elTaskIds = new ArrayList<>();
122-
edgeLabelAPI.list().forEach(edgeLabel -> {
123-
elTaskIds.add(edgeLabelAPI.delete(edgeLabel.name()));
124-
});
117+
edgeLabelAPI.list().forEach(el -> elTaskIds.add(edgeLabelAPI.delete(el.name())));
125118
elTaskIds.forEach(BaseApiTest::waitUntilTaskCompleted);
126119

127120
List<Long> vlTaskIds = new ArrayList<>();
128-
vertexLabelAPI.list().forEach(vertexLabel -> {
129-
vlTaskIds.add(vertexLabelAPI.delete(vertexLabel.name()));
130-
});
121+
vertexLabelAPI.list().forEach(vl -> vlTaskIds.add(vertexLabelAPI.delete(vl.name())));
131122
// Vertex label deletion may take longer, use extended timeout
132123
vlTaskIds.forEach(taskId -> waitUntilTaskCompleted(taskId, 30));
133124

134125
List<Long> pkTaskIds = new ArrayList<>();
135-
propertyKeyAPI.list().forEach(propertyKey -> {
136-
pkTaskIds.add(propertyKeyAPI.delete(propertyKey.name()));
137-
});
126+
propertyKeyAPI.list().forEach(pk -> pkTaskIds.add(propertyKeyAPI.delete(pk.name())));
138127
pkTaskIds.forEach(BaseApiTest::waitUntilTaskCompleted);
139128

140-
// Clear system
129+
// Clear all tasks (cancel running ones first)
130+
cleanupTasks();
131+
}
132+
133+
protected static void cleanupTasks() {
141134
taskAPI.list(null, -1).forEach(task -> {
142-
taskAPI.delete(task.id());
135+
if (!task.completed()) {
136+
try {
137+
taskAPI.cancel(task.id());
138+
Thread.sleep(1000);
139+
} catch (Exception ignored) {
140+
// Task may have completed during cancellation
141+
}
142+
}
143+
try {
144+
taskAPI.delete(task.id());
145+
} catch (Exception ignored) {
146+
// Task may have been deleted by another process
147+
}
143148
});
144149
}
145150

@@ -154,7 +159,13 @@ protected static void waitUntilTaskCompleted(long taskId, long timeout) {
154159
if (taskId == 0L) {
155160
return;
156161
}
157-
taskAPI.waitUntilTaskSuccess(taskId, timeout);
162+
try {
163+
taskAPI.waitUntilTaskSuccess(taskId, timeout);
164+
} catch (Exception e) {
165+
// Cleanup should be resilient - log warning but continue
166+
System.err.println("Warning: Task " + taskId +
167+
" did not complete successfully: " + e.getMessage());
168+
}
158169
}
159170

160171
protected RestClient client() {

hugegraph-client/src/test/java/org/apache/hugegraph/api/JobApiTest.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,7 @@ public static void prepareSchema() {
3838

3939
@After
4040
public void teardown() throws Exception {
41-
taskAPI.list(null, -1).forEach(task -> {
42-
// Cancel running/cancelling tasks before deletion
43-
if (!task.completed()) {
44-
try {
45-
taskAPI.cancel(task.id());
46-
// Wait for cancellation to complete
47-
Thread.sleep(1000);
48-
} catch (Exception e) {
49-
// Ignore if already completed or cancellation fails
50-
}
51-
}
52-
taskAPI.delete(task.id());
53-
});
41+
cleanupTasks();
5442
}
5543

5644
@Test

hugegraph-client/src/test/java/org/apache/hugegraph/api/TaskApiTest.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,26 @@ public static void prepareSchema() {
4646

4747
@After
4848
public void teardown() throws Exception {
49-
taskAPI.list(null, -1).forEach(task -> {
50-
// Cancel running/cancelling tasks before deletion
51-
if (!task.completed()) {
52-
try {
53-
taskAPI.cancel(task.id());
54-
// Wait for cancellation to complete
55-
Thread.sleep(1000);
56-
} catch (Exception e) {
57-
// Ignore if already completed or cancellation fails
49+
// Clean up all tasks (especially async tasks from testCancel)
50+
cleanupTasks();
51+
52+
// Clean up 'man' vertex label created in testCancel
53+
cleanupManVertexLabel();
54+
}
55+
56+
private void cleanupManVertexLabel() {
57+
try {
58+
if (schema().getVertexLabel("man") != null) {
59+
// Drop vertices first, then delete label
60+
gremlin().execute(new GremlinRequest("g.V().hasLabel('man').drop()"));
61+
long taskId = vertexLabelAPI.delete("man");
62+
if (taskId != 0L) {
63+
waitUntilTaskCompleted(taskId, 30);
5864
}
5965
}
60-
taskAPI.delete(task.id());
61-
});
66+
} catch (Exception ignored) {
67+
// Label may not exist or already deleted
68+
}
6269
}
6370

6471
@Test
@@ -221,15 +228,13 @@ public void testCancel() {
221228
schema().vertexLabel("man").useAutomaticId().ifNotExist().create();
222229

223230
// Clean up any existing 'man' vertices from previous tests
224-
String groovy = "g.V().hasLabel('man').drop()";
225-
GremlinRequest request = new GremlinRequest(groovy);
226-
gremlin().execute(request);
231+
gremlin().execute(new GremlinRequest("g.V().hasLabel('man').drop()"));
227232

228-
groovy = "for (int i = 0; i < 10; i++) {" +
229-
"g.addV('man').iterate();" +
230-
"}";
231233
// Insert 10 records in sync mode
232-
request = new GremlinRequest(groovy);
234+
String groovy = "for (int i = 0; i < 10; i++) {" +
235+
"g.addV('man').iterate();" +
236+
"}";
237+
GremlinRequest request = new GremlinRequest(groovy);
233238
gremlin().execute(request);
234239
// Verify insertion takes effect
235240
groovy = "g.V()";

0 commit comments

Comments
 (0)