|
16 | 16 | */ |
17 | 17 | package org.apache.camel.component.hazelcast.policy; |
18 | 18 |
|
19 | | -import java.util.ArrayList; |
20 | 19 | import java.util.List; |
| 20 | +import java.util.concurrent.CopyOnWriteArrayList; |
21 | 21 | import java.util.concurrent.CountDownLatch; |
| 22 | +import java.util.concurrent.ExecutorService; |
22 | 23 | import java.util.concurrent.Executors; |
23 | | -import java.util.concurrent.ScheduledExecutorService; |
24 | 24 | import java.util.concurrent.ThreadLocalRandom; |
25 | 25 | import java.util.concurrent.TimeUnit; |
26 | | -import java.util.stream.IntStream; |
27 | 26 |
|
28 | 27 | import com.hazelcast.config.Config; |
29 | 28 | import com.hazelcast.core.Hazelcast; |
|
32 | 31 | import org.apache.camel.impl.DefaultCamelContext; |
33 | 32 | import org.apache.camel.test.infra.hazelcast.services.HazelcastService; |
34 | 33 | import org.apache.camel.test.infra.hazelcast.services.HazelcastServiceFactory; |
35 | | -import org.junit.jupiter.api.Assertions; |
36 | 34 | import org.junit.jupiter.api.Test; |
| 35 | +import org.junit.jupiter.api.Timeout; |
37 | 36 | import org.junit.jupiter.api.extension.RegisterExtension; |
38 | 37 | import org.slf4j.Logger; |
39 | 38 | import org.slf4j.LoggerFactory; |
40 | 39 |
|
| 40 | +import static org.assertj.core.api.Assertions.assertThat; |
| 41 | + |
41 | 42 | /** |
42 | 43 | * Integration test for {@link HazelcastRoutePolicy} that verifies leader election and route management using Hazelcast |
43 | 44 | * distributed locks. |
44 | 45 | */ |
45 | 46 | public class HazelcastRoutePolicyIT { |
46 | 47 |
|
47 | 48 | private static final Logger LOGGER = LoggerFactory.getLogger(HazelcastRoutePolicyIT.class); |
| 49 | + private static final List<String> CLIENTS = List.of("0", "1", "2"); |
48 | 50 |
|
49 | 51 | @RegisterExtension |
50 | 52 | public static HazelcastService hazelcastService = HazelcastServiceFactory.createService(); |
51 | 53 |
|
52 | | - private static final List<String> CLIENTS = IntStream.range(0, 3).mapToObj(Integer::toString).toList(); |
53 | | - private static final List<String> RESULTS = new ArrayList<>(); |
54 | | - private static final ScheduledExecutorService SCHEDULER = Executors.newScheduledThreadPool(CLIENTS.size() * 2); |
55 | | - private static final CountDownLatch LATCH = new CountDownLatch(CLIENTS.size()); |
56 | | - |
57 | 54 | @Test |
58 | | - public void test() throws Exception { |
| 55 | + @Timeout(value = 2, unit = TimeUnit.MINUTES) |
| 56 | + public void testLeaderElectionWithMultipleNodes() throws Exception { |
| 57 | + List<String> results = new CopyOnWriteArrayList<>(); |
| 58 | + CountDownLatch latch = new CountDownLatch(CLIENTS.size()); |
| 59 | + ExecutorService executor = Executors.newFixedThreadPool(CLIENTS.size()); |
| 60 | + |
59 | 61 | for (String id : CLIENTS) { |
60 | | - SCHEDULER.submit(() -> run(id)); |
| 62 | + executor.submit(() -> run(id, results, latch)); |
61 | 63 | } |
62 | 64 |
|
63 | | - LATCH.await(1, TimeUnit.MINUTES); |
64 | | - SCHEDULER.shutdownNow(); |
| 65 | + assertThat(latch.await(1, TimeUnit.MINUTES)).as("All nodes should complete within timeout").isTrue(); |
| 66 | + executor.shutdown(); |
| 67 | + assertThat(executor.awaitTermination(30, TimeUnit.SECONDS)).as("Executor should terminate cleanly").isTrue(); |
65 | 68 |
|
66 | | - Assertions.assertEquals(CLIENTS.size(), RESULTS.size()); |
67 | | - Assertions.assertTrue(RESULTS.containsAll(CLIENTS)); |
| 69 | + assertThat(results).containsExactlyInAnyOrderElementsOf(CLIENTS); |
68 | 70 | } |
69 | 71 |
|
70 | | - private static void run(String id) { |
| 72 | + private static void run(String id, List<String> results, CountDownLatch latch) { |
| 73 | + HazelcastInstance instance = null; |
71 | 74 | try { |
72 | 75 | int events = ThreadLocalRandom.current().nextInt(2, 6); |
73 | 76 | CountDownLatch contextLatch = new CountDownLatch(events); |
74 | 77 |
|
75 | 78 | Config config = hazelcastService.createConfiguration(null, 0, "node-" + id, "set"); |
76 | | - HazelcastInstance instance = Hazelcast.newHazelcastInstance(config); |
| 79 | + instance = Hazelcast.newHazelcastInstance(config); |
77 | 80 |
|
78 | 81 | HazelcastRoutePolicy policy = new HazelcastRoutePolicy(instance); |
79 | 82 | policy.setLockMapName("camel-route-policy"); |
80 | 83 | policy.setLockKey("my-lock"); |
81 | 84 | policy.setLockValue("node-" + id); |
82 | 85 | policy.setTryLockTimeout(5, TimeUnit.SECONDS); |
83 | 86 |
|
84 | | - DefaultCamelContext context = new DefaultCamelContext(); |
85 | | - context.disableJMX(); |
86 | | - context.getCamelContextExtension().setName("context-" + id); |
87 | | - context.addRoutes(new RouteBuilder() { |
88 | | - @Override |
89 | | - public void configure() { |
90 | | - from("timer:hazelcast?delay=1000&period=1000") |
91 | | - .routeId("route-" + id) |
92 | | - .routePolicy(policy) |
93 | | - .log("From ${routeId}") |
94 | | - .process(e -> contextLatch.countDown()); |
| 87 | + try (DefaultCamelContext context = new DefaultCamelContext()) { |
| 88 | + context.disableJMX(); |
| 89 | + context.getCamelContextExtension().setName("context-" + id); |
| 90 | + context.addRoutes(new RouteBuilder() { |
| 91 | + @Override |
| 92 | + public void configure() { |
| 93 | + from("timer:hazelcast?delay=1000&period=1000") |
| 94 | + .routeId("route-" + id) |
| 95 | + .routePolicy(policy) |
| 96 | + .log("From ${routeId}") |
| 97 | + .process(e -> contextLatch.countDown()); |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + // Deterministic staggered startup based on node index |
| 102 | + Thread.sleep(Integer.parseInt(id) * 200L); |
| 103 | + |
| 104 | + LOGGER.info("Starting CamelContext on node: {}", id); |
| 105 | + context.start(); |
| 106 | + LOGGER.info("Started CamelContext on node: {}", id); |
| 107 | + |
| 108 | + if (contextLatch.await(30, TimeUnit.SECONDS)) { |
| 109 | + LOGGER.info("Node {} completed {} events successfully", id, events); |
| 110 | + results.add(id); |
| 111 | + } else { |
| 112 | + LOGGER.warn("Node {} timed out waiting for route events (expected {} events)", id, events); |
95 | 113 | } |
96 | | - }); |
97 | | - |
98 | | - // Staggered startup |
99 | | - Thread.sleep(ThreadLocalRandom.current().nextInt(500)); |
100 | | - |
101 | | - LOGGER.info("Starting CamelContext on node: {}", id); |
102 | | - context.start(); |
103 | | - LOGGER.info("Started CamelContext on node: {}", id); |
104 | | - |
105 | | - contextLatch.await(30, TimeUnit.SECONDS); |
106 | | - |
107 | | - LOGGER.info("Shutting down node {}", id); |
108 | | - RESULTS.add(id); |
109 | | - context.stop(); |
110 | | - instance.shutdown(); |
111 | | - LATCH.countDown(); |
| 114 | + } |
112 | 115 | } catch (Exception e) { |
113 | | - LOGGER.warn("{}", e.getMessage(), e); |
| 116 | + LOGGER.warn("Node {} failed: {}", id, e.getMessage(), e); |
| 117 | + } finally { |
| 118 | + if (instance != null) { |
| 119 | + instance.shutdown(); |
| 120 | + } |
| 121 | + latch.countDown(); |
114 | 122 | } |
115 | 123 | } |
116 | 124 | } |
0 commit comments