[ 
https://issues.apache.org/jira/browse/CAMEL-22524?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18028770#comment-18028770
 ] 

Pasquale Congiusti commented on CAMEL-22524:
--------------------------------------------

I had worked on a draft test that may require some polish to finally work, see:
{code:java}
/*
 * 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.camel.component.hazelcast.policy;

import java.util.concurrent.TimeUnit;

import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

public class HazelcastRoutePolicyTest {

    private static HazelcastInstance hazelcastInstance;

    @BeforeAll
    public static void setup() {
        hazelcastInstance = Hazelcast.newHazelcastInstance();
    }

    @AfterAll
    public static void teardown() {
        if (hazelcastInstance != null) {
            hazelcastInstance.shutdown();
        }
    }

    @Disabled("The test implemented here is an attempt which is not yet 
working. TODO: make it work and enable.")
    @Test
    public void testRouteLocking() throws Exception {
        // Create first Camel context (node 1)
        CamelContext camel1 = new DefaultCamelContext();
        camel1.getRegistry().bind("hzInstance", hazelcastInstance);

        HazelcastRoutePolicy policy1 = new 
HazelcastRoutePolicy(hazelcastInstance);
        policy1.setLockMapName("testRouteLock");
        policy1.setLockKey("testRouteLockKey");
        policy1.setLockValue("node1");
        policy1.setTryLockTimeout(5, TimeUnit.SECONDS);

        camel1.addRoutes(new RouteBuilder() {
            @Override
            public void configure() {
                from("direct:route1")
                        .routeId("testRoute1")
                        .routePolicy(policy1)
                        .to("mock:result");
            }
        });

        // Create second Camel context (node 2)
        CamelContext camel2 = new DefaultCamelContext();
        camel2.getRegistry().bind("hzInstance", hazelcastInstance);

        HazelcastRoutePolicy policy2 = new 
HazelcastRoutePolicy(hazelcastInstance);
        policy2.setLockMapName("testRouteLock");
        policy2.setLockKey("testRouteLockKey");
        policy2.setLockValue("node2");
        policy2.setTryLockTimeout(5, TimeUnit.SECONDS);

        camel2.addRoutes(new RouteBuilder() {
            @Override
            public void configure() {
                from("direct:route2")
                        .routeId("testRoute2")
                        .routePolicy(policy2)
                        .to("mock:result");
            }
        });

        try {
            // Start both Camel contexts
            camel1.start();
            camel2.start();

            Thread.sleep(2000);

            // Check which route is started and which is stopped
            boolean camel1RouteRunning = 
camel1.getRouteController().getRouteStatus("testRoute1").isStarted()
                    && 
!camel1.getRouteController().getRouteStatus("testRoute1").isStopped();
            boolean camel2RouteRunning = 
camel2.getRouteController().getRouteStatus("testRoute2").isStarted()
                    && 
!camel2.getRouteController().getRouteStatus("testRoute2").isStopped();

            // Exactly one route should be started
            Assertions.assertTrue(camel1RouteRunning ^ camel2RouteRunning,
                    "Only one route should be started across the cluster");

            Thread.sleep(5000);
        } finally {
            camel1.stop();
            camel2.stop();

            camel1.close();
            camel2.close();
        }
    }
}

{code}

> [camel-hazelcast] Automate HazelcastRoutePolicy test
> ----------------------------------------------------
>
>                 Key: CAMEL-22524
>                 URL: https://issues.apache.org/jira/browse/CAMEL-22524
>             Project: Camel
>          Issue Type: Task
>            Reporter: Pasquale Congiusti
>            Priority: Minor
>
> https://github.com/apache/camel/blob/c2f180bea3accc23836dac1f6cfe615e53229b68/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/policy/HazelcastRoutePolicyMain.java



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to