<commit_before>package com.hazelcast.stabilizer.tests.queue;

import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IList;
import com.hazelcast.core.ILock;
import com.hazelcast.core.IQueue;
import com.hazelcast.core.TransactionalQueue;
import com.hazelcast.spi.exception.TargetDisconnectedException;
import com.hazelcast.stabilizer.tests.TestContext;
import com.hazelcast.stabilizer.tests.TestRunner;
import com.hazelcast.stabilizer.tests.annotations.Run;
import com.hazelcast.stabilizer.tests.annotations.Setup;
import com.hazelcast.stabilizer.tests.annotations.Teardown;
import com.hazelcast.stabilizer.tests.annotations.Verify;
import com.hazelcast.stabilizer.tests.queue.helpers.TxnCounter;
import com.hazelcast.stabilizer.tests.utils.ThreadSpawner;
import com.hazelcast.transaction.TransactionContext;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

/**
 * This stabilizer test simulates the issue #2287
 */
public class TxnQueueWithLockTest {

    public String basename = this.getClass().getName();
    public int threadCount = 5;

    private HazelcastInstance instance=null;
    private TestContext testContext = null;
    private ILock firstLock = null;
    private ILock secondLock = null;
    private IQueue queue = null;
    private IList<TxnCounter> results = null;

    @Setup
    public void setup(TestContext testContext) throws Exception {
        this.testContext = testContext;
        this.instance = testContext.getTargetInstance();

        firstLock = instance.getLock(basename +"lock1");
        secondLock = instance.getLock(basename +"lock2");
        queue = instance.getQueue(basename +"q");
        results =  instance.getList(basename +"results");
    }

    @Teardown
    public void teardown() throws Exception {
        firstLock.destroy();
        secondLock.destroy();
        queue.destroy();
    }

    @Run
    public void run() {
        ThreadSpawner spawner = new ThreadSpawner(testContext.getTestId());
        for (int k = 0; k < threadCount; k++) {
            spawner.spawn(new Worker());
        }
        spawner.awaitCompletion();
    }

    private class Worker implements Runnable {
        private TxnCounter  counter = new TxnCounter();

        @Override
        public void run() {
            while (!testContext.isStopped()) {

                try{
                    firstLock.lock();
                    TransactionContext ctx = instance.newTransactionContext();
                    ctx.beginTransaction();
                    try {
                        TransactionalQueue<Integer> queue = ctx.getQueue(basename +"q");

                        queue.offer(1);
                        secondLock.lock();
                        secondLock.unlock();

                        ctx.commitTransaction();
                        counter.committed++;

                    } catch (Exception e) {
                        ctx.rollbackTransaction();
                        counter.rolled++;

                        System.out.println(basename+": ThreadLocal txn No. "+ counter.committed+1+" ThreadLocal roles ="+counter.rolled);
                        System.out.println(basename+": "+e);

                    } finally {
                        firstLock.unlock();
                    }
                }catch(TargetDisconnectedException e){
                    System.out.println(e);
                }
            }
            results.add(counter);
        }
    }

    @Verify(global = true)
    public void verify() {
        TxnCounter  total = new TxnCounter();
        for(TxnCounter counter : results){<commit_msg>testing<commit_after>package com.hazelcast.stabilizer.tests.queue;

import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.HazelcastInstanceNotActiveException;
import com.hazelcast.core.IList;
import com.hazelcast.core.ILock;
import com.hazelcast.core.IQueue;
import com.hazelcast.core.TransactionalQueue;
import com.hazelcast.spi.exception.TargetDisconnectedException;
import com.hazelcast.stabilizer.tests.TestContext;
import com.hazelcast.stabilizer.tests.TestRunner;
import com.hazelcast.stabilizer.tests.annotations.Run;
import com.hazelcast.stabilizer.tests.annotations.Setup;
import com.hazelcast.stabilizer.tests.annotations.Teardown;
import com.hazelcast.stabilizer.tests.annotations.Verify;
import com.hazelcast.stabilizer.tests.queue.helpers.TxnCounter;
import com.hazelcast.stabilizer.tests.utils.ThreadSpawner;
import com.hazelcast.transaction.TransactionContext;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

/**
 * This stabilizer test simulates the issue #2287
 */
public class TxnQueueWithLockTest {

    public String basename = this.getClass().getName();
    public int threadCount = 5;

    private HazelcastInstance instance=null;
    private TestContext testContext = null;

    @Setup
    public void setup(TestContext testContext) throws Exception {
        this.testContext = testContext;
        this.instance = testContext.getTargetInstance();


    }

    @Run
    public void run() {
        ThreadSpawner spawner = new ThreadSpawner(testContext.getTestId());
        for (int k = 0; k < threadCount; k++) {
            spawner.spawn(new Worker());
        }
        spawner.awaitCompletion();
    }

    private class Worker implements Runnable {
        private TxnCounter  counter = new TxnCounter();

        @Override
        public void run() {
            while (!testContext.isStopped()) {

                try{
                    ILock firstLock = instance.getLock(basename +"l1");
                    firstLock.lock();

                    TransactionContext ctx = instance.newTransactionContext();
                    ctx.beginTransaction();

                    try {
                        TransactionalQueue<Integer> queue = ctx.getQueue(basename +"q");

                        queue.offer(1);

                        ILock secondLock = instance.getLock(basename +"l2");
                        secondLock.lock();
                        secondLock.unlock();

                        ctx.commitTransaction();
                        counter.committed++;

                    } catch (Exception e) {
                        ctx.rollbackTransaction();
                        counter.rolled++;

                        System.out.println(basename+": ThreadLocal txn No. "+ counter.committed+1+" ThreadLocal roles ="+counter.rolled);
                        System.out.println(basename+": "+e);

                    } finally {
                        firstLock.unlock();
                    }
                }catch(TargetDisconnectedException e){
                    System.out.println(e);
                }catch(HazelcastInstanceNotActiveException e){
                    System.out.println(e);
                }
            }
            IList<TxnCounter> results =  instance.getList(basename +"results");
            results.add(counter);
        }
    }

    @Verify(global = true)
    public void verify() {

        IQueue queue = instance.getQueue(basename +"q");
        ILock firstLock = instance.getLock(basename +"l1");
        ILock secondLock = instance.getLock(basename +"l2");


        IList<TxnCounter> results =  instance.getList(basename +"results");

        TxnCounter  total = new TxnCounter();
        for(TxnCounter counter : results){
{'fraction_non_alphanumeric': 0.09913560237709346, 'fraction_numerical': 0.002971366828741221, 'mean_word_length': 2.204240588489831, 'pattern_counts': {'":': 4, '<': 10, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4744666', 'n_tokens_mistral': 2064, 'n_tokens_neox': 1984, 'n_words': 383}