Skip to content

Commit bcb6bbd

Browse files
committed
fix: improve test reliability and server initialization handling
Added a wait for HugeGraph server initialization in the install script and test cases to prevent race conditions. Enhanced teardown methods in JobApiTest and TaskApiTest to cancel running tasks before deletion. Updated TaskApiTest to clean up vertices before insertion, ensuring tests run in a clean state.
1 parent e96ba68 commit bcb6bbd

File tree

5 files changed

+71
-7
lines changed

5 files changed

+71
-7
lines changed

hugegraph-client/assembly/travis/install-hugegraph-from-source.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ sed -i 's|#auth.admin_pa=.*|auth.admin_pa=pa|' conf/rest-server.properties
4747
echo -e "pa" | bin/init-store.sh || exit 1
4848
bin/start-hugegraph.sh || exit 1
4949

50+
# Wait for server to initialize
51+
echo "Waiting 5 seconds for HugeGraph server to initialize..."
52+
sleep 5
53+
5054
cd ../${HTTPS_SERVER_DIR}
5155
REST_SERVER_CONFIG="conf/rest-server.properties"
5256
GREMLIN_SERVER_CONFIG="conf/gremlin-server.yaml"

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ protected static void clearData() {
128128
vertexLabelAPI.list().forEach(vertexLabel -> {
129129
vlTaskIds.add(vertexLabelAPI.delete(vertexLabel.name()));
130130
});
131-
vlTaskIds.forEach(BaseApiTest::waitUntilTaskCompleted);
131+
// Vertex label deletion may take longer, use extended timeout
132+
vlTaskIds.forEach(taskId -> waitUntilTaskCompleted(taskId, 30));
132133

133134
List<Long> pkTaskIds = new ArrayList<>();
134135
propertyKeyAPI.list().forEach(propertyKey -> {

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,33 @@ public class GraphsApiTest extends BaseApiTest {
4646
private static final String GRAPH3 = "hugegraph3";
4747
private static final String CONFIG3_PATH = "src/test/resources/hugegraph-clone.properties";
4848

49+
private static final int MAX_WAIT_SECONDS = 30;
50+
private static final int RETRY_INTERVAL_MS = 1000;
51+
52+
/**
53+
* Wait for HugeGraph server to be ready before running tests
54+
*/
55+
private static void waitForServerReady() {
56+
System.out.println("Waiting for HugeGraph server to be ready...");
57+
for (int i = 0; i < MAX_WAIT_SECONDS; i++) {
58+
try {
59+
// Try to get graph list, if successful, server is ready
60+
graphsAPI.list();
61+
System.out.println("Server is ready after " + i + " seconds");
62+
return;
63+
} catch (Exception e) {
64+
System.out.println("Waiting for server... (" + (i + 1) + "/" + MAX_WAIT_SECONDS + ")");
65+
try {
66+
Thread.sleep(RETRY_INTERVAL_MS);
67+
} catch (InterruptedException ie) {
68+
Thread.currentThread().interrupt();
69+
throw new RuntimeException("Interrupted while waiting for server", ie);
70+
}
71+
}
72+
}
73+
throw new RuntimeException("Server not ready after " + MAX_WAIT_SECONDS + " seconds");
74+
}
75+
4976
protected static void initPropertyKey(HugeClient client) {
5077
SchemaManager schema = client.schema();
5178
schema.propertyKey("name").asText().ifNotExist().create();
@@ -119,6 +146,7 @@ public void teardown() {
119146

120147
@Test
121148
public void testCreateAndDropGraph() {
149+
waitForServerReady();
122150
int initialGraphNumber = graphsAPI.list().size();
123151

124152
// Create new graph dynamically
@@ -190,6 +218,7 @@ public void testCreateAndDropGraph() {
190218

191219
@Test
192220
public void testCloneAndDropGraph() {
221+
waitForServerReady();
193222
int initialGraphNumber = graphsAPI.list().size();
194223

195224
// Clone a new graph from exist a graph dynamically
@@ -262,6 +291,7 @@ public void testCloneAndDropGraph() {
262291

263292
@Test
264293
public void testCloneAndDropGraphWithoutConfig() {
294+
waitForServerReady();
265295
int initialGraphNumber = graphsAPI.list().size();
266296

267297
// Clone a new graph from exist a graph dynamically

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

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

3939
@After
4040
public void teardown() throws Exception {
41-
taskAPI.list(null, -1).forEach(task -> taskAPI.delete(task.id()));
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+
});
4254
}
4355

4456
@Test

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

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

4747
@After
4848
public void teardown() throws Exception {
49-
taskAPI.list(null, -1).forEach(task -> taskAPI.delete(task.id()));
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
58+
}
59+
}
60+
taskAPI.delete(task.id());
61+
});
5062
}
5163

5264
@Test
@@ -208,12 +220,17 @@ public void testDelete() {
208220
public void testCancel() {
209221
schema().vertexLabel("man").useAutomaticId().ifNotExist().create();
210222

211-
String groovy = "for (int i = 0; i < 10; i++) {" +
212-
"g.addV('man').iterate();" +
213-
"}";
214-
// Insert 10 records in sync mode
223+
// Clean up any existing 'man' vertices from previous tests
224+
String groovy = "g.V().hasLabel('man').drop()";
215225
GremlinRequest request = new GremlinRequest(groovy);
216226
gremlin().execute(request);
227+
228+
groovy = "for (int i = 0; i < 10; i++) {" +
229+
"g.addV('man').iterate();" +
230+
"}";
231+
// Insert 10 records in sync mode
232+
request = new GremlinRequest(groovy);
233+
gremlin().execute(request);
217234
// Verify insertion takes effect
218235
groovy = "g.V()";
219236
request = new GremlinRequest(groovy);

0 commit comments

Comments
 (0)