<commit_before>/*
 * 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.ignite.internal.processors.cache.datastructures;

import java.util.concurrent.Callable;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteAtomicLong;
import org.apache.ignite.IgniteAtomicSequence;
import org.apache.ignite.IgniteCountDownLatch;
import org.apache.ignite.IgniteLock;
import org.apache.ignite.IgniteQueue;
import org.apache.ignite.IgniteSemaphore;
import org.apache.ignite.IgniteSet;
import org.apache.ignite.configuration.CollectionConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.internal.IgniteInternalFuture;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.ipfinder.TcpDiscoveryIpFinder;
import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;
import org.apache.ignite.testframework.GridTestUtils;
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;

/**
 *
 */
public abstract class IgniteClientDataStructuresAbstractTest extends GridCommonAbstractTest {
    /** */
    protected static TcpDiscoveryIpFinder ipFinder = new TcpDiscoveryVmIpFinder(true);

    /** */
    private static final int NODE_CNT = 4;

    /** {@inheritDoc} */
    @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
        IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);

        if (igniteInstanceName.equals(getTestIgniteInstanceName(NODE_CNT - 1))) {
            cfg.setClientMode(true);

            if (!clientDiscovery())
                ((TcpDiscoverySpi)cfg.getDiscoverySpi()).setForceServerMode(true);
        }

        ((TcpDiscoverySpi)cfg.getDiscoverySpi()).setIpFinder(ipFinder);

        ((TcpCommunicationSpi)cfg.getCommunicationSpi()).setSharedMemoryPort(-1);

        return cfg;
    }

    /** {@inheritDoc} */
    @Override protected void beforeTestsStarted() throws Exception {
        super.beforeTestsStarted();

        startGrids(NODE_CNT);
    }

    /** {@inheritDoc} */
    @Override protected void afterTestsStopped() throws Exception {
        stopAllGrids();
    }

    /**
     * @return {@code True} if use client discovery.
     */
    protected abstract boolean clientDiscovery();

    /**
     * @throws Exception If failed.
     */
    public void testSequence() throws Exception {
        Ignite clientNode = clientIgnite();
        Ignite srvNode = serverNode();

        testSequence(clientNode, srvNode);
        testSequence(srvNode, clientNode);
    }

    /**
     * @param creator Creator node.
     * @param other Other node.
     * @throws Exception If failed.
     */
    private void testSequence(Ignite creator, Ignite other) throws Exception {
        assertNull(creator.atomicSequence("seq1", 1L, false));
        assertNull(other.atomicSequence("seq1", 1L, false));

        try (IgniteAtomicSequence seq = creator.atomicSequence("seq1", 1L, true)) {
            assertNotNull(seq);

            assertEquals(1L, seq.get());

            assertEquals(1L, seq.getAndAdd(1));

            assertEquals(2L, seq.get());

            IgniteAtomicSequence seq0 = other.atomicSequence("seq1", 1L, false);

            assertNotNull(seq0);
        }

        assertNull(creator.atomicSequence("seq1", 1L, false));
        assertNull(other.atomicSequence("seq1", 1L, false));
    }

    /**
     * @throws Exception If failed.
     */
    public void testAtomicLong() throws Exception {
        Ignite clientNode = clientIgnite();
        Ignite srvNode = serverNode();

        testAtomicLong(clientNode, srvNode);
        testAtomicLong(srvNode, clientNode);
    }

    /**
     * @param creator Creator node.
     * @param other Other node.
     * @throws Exception If failed.
     */
    private void testAtomicLong(Ignite creator, Ignite other) throws Exception {
        assertNull(creator.atomicLong("long1", 1L, false));
        assertNull(other.atomicLong("long1", 1L, false));

        try (IgniteAtomicLong cntr = creator.atomicLong("long1", 1L, true)) {
            assertNotNull(cntr);

            assertEquals(1L, cntr.get());

            assertEquals(1L, cntr.getAndAdd(1));

            assertEquals(2L, cntr.get());

            IgniteAtomicLong cntr0 = other.atomicLong("long1", 1L, false);

            assertNotNull(cntr0);

            assertEquals(2L, cntr0.get());

            assertEquals(3L, cntr0.incrementAndGet());

            assertEquals(3L, cntr.get());
        }

        assertNull(creator.atomicLong("long1", 1L, false));
        assertNull(other.atomicLong("long1", 1L, false));<commit_msg>IGNITE-7845 fix checking atomicLong in IgniteClientDataStructuresTest#testAtomicLong. - Fixes #3606.<commit_after>/*
 * 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.ignite.internal.processors.cache.datastructures;

import java.util.concurrent.Callable;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteAtomicLong;
import org.apache.ignite.IgniteAtomicSequence;
import org.apache.ignite.IgniteCountDownLatch;
import org.apache.ignite.IgniteException;
import org.apache.ignite.IgniteLock;
import org.apache.ignite.IgniteQueue;
import org.apache.ignite.IgniteSemaphore;
import org.apache.ignite.IgniteSet;
import org.apache.ignite.configuration.CollectionConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.internal.IgniteInternalFuture;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.ipfinder.TcpDiscoveryIpFinder;
import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;
import org.apache.ignite.testframework.GridTestUtils;
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;

/**
 *
 */
public abstract class IgniteClientDataStructuresAbstractTest extends GridCommonAbstractTest {
    /** */
    protected static TcpDiscoveryIpFinder ipFinder = new TcpDiscoveryVmIpFinder(true);

    /** */
    private static final int NODE_CNT = 4;

    /** {@inheritDoc} */
    @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
        IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);

        if (igniteInstanceName.equals(getTestIgniteInstanceName(NODE_CNT - 1))) {
            cfg.setClientMode(true);

            if (!clientDiscovery())
                ((TcpDiscoverySpi)cfg.getDiscoverySpi()).setForceServerMode(true);
        }

        ((TcpDiscoverySpi)cfg.getDiscoverySpi()).setIpFinder(ipFinder);

        ((TcpCommunicationSpi)cfg.getCommunicationSpi()).setSharedMemoryPort(-1);

        return cfg;
    }

    /** {@inheritDoc} */
    @Override protected void beforeTestsStarted() throws Exception {
        super.beforeTestsStarted();

        startGrids(NODE_CNT);
    }

    /** {@inheritDoc} */
    @Override protected void afterTestsStopped() throws Exception {
        stopAllGrids();
    }

    /**
     * @return {@code True} if use client discovery.
     */
    protected abstract boolean clientDiscovery();

    /**
     * @throws Exception If failed.
     */
    public void testSequence() throws Exception {
        Ignite clientNode = clientIgnite();
        Ignite srvNode = serverNode();

        testSequence(clientNode, srvNode);
        testSequence(srvNode, clientNode);
    }

    /**
     * @param creator Creator node.
     * @param other Other node.
     * @throws Exception If failed.
     */
    private void testSequence(Ignite creator, Ignite other) throws Exception {
        assertNull(creator.atomicSequence("seq1", 1L, false));
        assertNull(other.atomicSequence("seq1", 1L, false));

        try (IgniteAtomicSequence seq = creator.atomicSequence("seq1", 1L, true)) {
            assertNotNull(seq);

            assertEquals(1L, seq.get());

            assertEquals(1L, seq.getAndAdd(1));

            assertEquals(2L, seq.get());

            IgniteAtomicSequence seq0 = other.atomicSequence("seq1", 1L, false);

            assertNotNull(seq0);
        }

        assertNull(creator.atomicSequence("seq1", 1L, false));
        assertNull(other.atomicSequence("seq1", 1L, false));
    }

    /**
     * @throws Exception If failed.
     */
    public void testAtomicLong() throws Exception {
        Ignite clientNode = clientIgnite();
        Ignite srvNode = serverNode();

        testAtomicLong(clientNode, srvNode);
        testAtomicLong(srvNode, clientNode);
    }

    /**
     * @param creator Creator node.
     * @param other Other node.
     * @throws Exception If failed.
     */
    private void testAtomicLong(Ignite creator, Ignite other) throws Exception {
        assertNull(creator.atomicLong("long1", 1L, false));
        assertNull(other.atomicLong("long1", 1L, false));

        try (IgniteAtomicLong cntr = creator.atomicLong("long1", 1L, true)) {
            assertNotNull(cntr);

            assertEquals(1L, cntr.get());

            assertEquals(1L, cntr.getAndAdd(1));

            assertEquals(2L, cntr.get());

            IgniteAtomicLong cntr0 = other.atomicLong("long1", 1L, false);

            assertNotNull(cntr0);

            assertEquals(2L, cntr0.get());

            assertEquals(3L, cntr0.incrementAndGet());

            assertEquals(3L, cntr.get());
        }

        assertAtomicLongClosedCorrect(creator.atomicLong("long1", 1L, false));
        assertAtomicLongClosedCorrect(other.atomicLong("long1", 1L, false));
    }

    /**
     * It is possible 3 variants:
     * * input value is null, because it already delete.
     * * input value is not null, but call 'get' method causes IllegalStateException because IgniteAtomicLong marked as delete.
     * * input value is not null, but call 'get' method causes IgniteException
     * because IgniteAtomicLong have not marked as delete yet but already removed from cache.
     */
    private void assertAtomicLongClosedCorrect(IgniteAtomicLong atomicLong) {
        if (atomicLong == null)
            assertNull(atomicLong);
        else {
            try {
                atomicLong.get();

                fail("Always should be exception because atomicLong was closed");
            }
            catch (IllegalStateException e) {
                String expectedMessage = "Sequence was removed from cache";

                assertTrue(
                    String.format("Exception should start with '%s' but was '%s'", expectedMessage, e.getMessage()),
                    e.getMessage().startsWith(expectedMessage)
                );
            }
            catch (IgniteException e){
                String expectedMessage = "Failed to find atomic long:";

                assertTrue(
                    String.format("Exception should start with '%s' but was '%s'", expectedMessage, e.getMessage()),
                    e.getMessage().startsWith(expectedMessage)
                );
            }
        }
{'fraction_non_alphanumeric': 0.10288766741503445, 'fraction_numerical': 0.008128822482000465, 'mean_word_length': 3.093155893536122, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2283646', 'n_tokens_mistral': 3679, 'n_tokens_neox': 3543, 'n_words': 866}