|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.hugegraph.unit.util; |
| 19 | + |
| 20 | +import java.util.concurrent.ExecutorService; |
| 21 | +import java.util.concurrent.Executors; |
| 22 | +import java.util.concurrent.atomic.AtomicInteger; |
| 23 | + |
| 24 | +import org.apache.hugegraph.testutil.Assert; |
| 25 | +import org.apache.hugegraph.util.Consumers; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +public class ConsumersTest { |
| 29 | + |
| 30 | + @Test(timeout = 1000) |
| 31 | + public void testStartProvideAwaitNormal() throws Throwable { |
| 32 | + ExecutorService executor = Executors.newFixedThreadPool(2); |
| 33 | + try { |
| 34 | + AtomicInteger processed = new AtomicInteger(); |
| 35 | + |
| 36 | + Consumers<Integer> consumers = new Consumers<>(executor, v -> { |
| 37 | + processed.incrementAndGet(); |
| 38 | + }); |
| 39 | + |
| 40 | + consumers.start("test"); |
| 41 | + for (int i = 0; i < 50; i++) { |
| 42 | + consumers.provide(i); |
| 43 | + } |
| 44 | + consumers.await(); |
| 45 | + |
| 46 | + Assert.assertEquals("Should process all provided elements", |
| 47 | + 50, processed.get()); |
| 48 | + } finally { |
| 49 | + executor.shutdownNow(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Regression test for deadlock: |
| 55 | + * |
| 56 | + * ContextCallable fails before entering runAndDone(). |
| 57 | + * await() must still return because latch is decremented in safeRun(). |
| 58 | + */ |
| 59 | + @Test(timeout = 1000) |
| 60 | + public void testAwaitDoesNotHangWhenContextCallableFails() throws Throwable { |
| 61 | + ExecutorService executor = Executors.newFixedThreadPool(1); |
| 62 | + try { |
| 63 | + // Use AssertionError to bypass the inner catch(Exception) loop in runAndDone() |
| 64 | + // This simulates a scenario where an exception escapes the task logic |
| 65 | + // (similar to how a ContextCallable failure would behave from safeRun's perspective) |
| 66 | + Consumers<Integer> consumers = new Consumers<>(executor, v -> { |
| 67 | + throw new AssertionError("Simulated fatal error (OOM/StackOverflow/etc)"); |
| 68 | + }); |
| 69 | + consumers.start("test-fatal-error"); |
| 70 | + consumers.provide(1); |
| 71 | + // Verification: |
| 72 | + // Without the fix, the latch would never be decremented (because runAndDone crashes), causing await() to hang. |
| 73 | + // With the fix (safeRun wrapper), the finally block ensures latch.countDown() is called. |
| 74 | + consumers.await(); |
| 75 | + |
| 76 | + // Note: consumer.exception will be null because safeRun only catches Exception, not Error. |
| 77 | + // This is acceptable behavior for fatal errors, as long as it doesn't deadlock. |
| 78 | + } finally { |
| 79 | + executor.shutdownNow(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Test(timeout = 1000) |
| 84 | + public void testAwaitThrowsWhenConsumerThrows() throws Throwable { |
| 85 | + ExecutorService executor = Executors.newFixedThreadPool(2); |
| 86 | + try { |
| 87 | + final String msg = "Injected exception for test"; |
| 88 | + |
| 89 | + Consumers<Integer> consumers = new Consumers<>(executor, v -> { |
| 90 | + throw new RuntimeException(msg); |
| 91 | + }); |
| 92 | + |
| 93 | + consumers.start("test"); |
| 94 | + consumers.provide(1); |
| 95 | + |
| 96 | + try { |
| 97 | + consumers.await(); |
| 98 | + Assert.fail("Expected await() to throw when consumer throws"); |
| 99 | + } catch (Throwable t) { |
| 100 | + Throwable root = t.getCause() != null ? t.getCause() : t; |
| 101 | + Assert.assertTrue("Expected RuntimeException, but got: " + root, |
| 102 | + root instanceof RuntimeException); |
| 103 | + Assert.assertTrue("Exception message should contain injected message", |
| 104 | + root.getMessage() != null && |
| 105 | + root.getMessage().contains(msg)); |
| 106 | + } |
| 107 | + } finally { |
| 108 | + executor.shutdownNow(); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments