Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,23 @@ public void start(String name) {
LOG.info("Starting {} workers[{}] with queue size {}...",
this.workers, name, this.queueSize);
for (int i = 0; i < this.workers; i++) {
this.runningFutures.add(
this.executor.submit(new ContextCallable<>(this::runAndDone)));
this.runningFutures.add(this.executor.submit(this::safeRun));
}

}
private Void safeRun() {
Comment thread
bitflicker64 marked this conversation as resolved.
Outdated
try {
new ContextCallable<>(this::runAndDone).call();
Comment thread
bitflicker64 marked this conversation as resolved.
Outdated
} catch (Throwable e) {
Comment thread
bitflicker64 marked this conversation as resolved.
Outdated
LOG.error("Worker failed before runAndDone()", e);
Comment thread
bitflicker64 marked this conversation as resolved.
Outdated
if (this.exception == null) {
this.exception = e;
}
exceptionHandle(e);
Comment thread
bitflicker64 marked this conversation as resolved.
Outdated
} finally {
this.latch.countDown();
}
return null;
}

private Void runAndDone() {
Expand All @@ -124,7 +138,6 @@ private Void runAndDone() {
exceptionHandle(e);
} finally {
this.done();
this.latch.countDown();
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hugegraph.unit.util;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.hugegraph.testutil.Assert;
import org.apache.hugegraph.util.Consumers;
import org.junit.Test;

public class ConsumersTest {

Comment thread
bitflicker64 marked this conversation as resolved.
@Test(timeout = 5000)
public void testStartProvideAwaitNormal() throws Throwable {
ExecutorService executor = Executors.newFixedThreadPool(2);
try {
AtomicInteger processed = new AtomicInteger();

Consumers<Integer> consumers = new Consumers<>(executor, v -> {
processed.incrementAndGet();
});

consumers.start("test");
for (int i = 0; i < 50; i++) {
consumers.provide(i);
}
consumers.await();

Assert.assertEquals("Should process all provided elements",
50, processed.get());
} finally {
executor.shutdownNow();
}
}

@Test(timeout = 5000)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Minor: The test doesn't verify the specific deadlock scenario mentioned in the PR description - where ContextCallable fails before entering runAndDone().

Consider adding a test case that explicitly simulates the failure scenario, for example:

@Test(timeout = 1000)
public void testAwaitDoesNotHangWhenContextCallableFails() throws Throwable {
    // Test that simulates ContextCallable constructor/call() failure
    // before runAndDone() is entered
}

public void testAwaitThrowsWhenConsumerThrows() throws Throwable {
ExecutorService executor = Executors.newFixedThreadPool(2);
try {
final String msg = "Injected exception for test";

Consumers<Integer> consumers = new Consumers<>(executor, v -> {
throw new RuntimeException(msg);
});

consumers.start("test");
consumers.provide(1);

try {
consumers.await();
Assert.fail("Expected await() to throw when consumer throws");
} catch (Throwable t) {
Throwable root = t.getCause() != null ? t.getCause() : t;
Assert.assertTrue("Expected RuntimeException, but got: " + root,
root instanceof RuntimeException);
Assert.assertTrue("Exception message should contain injected message",
root.getMessage() != null &&
root.getMessage().contains(msg));
}
} finally {
executor.shutdownNow();
}
}
}
Loading