<commit_before>package org.jgroups.blocks;

import org.jgroups.Global;
import org.jgroups.JChannel;
import org.jgroups.blocks.locking.LockService;
import org.jgroups.protocols.CENTRAL_LOCK;
import org.jgroups.stack.ProtocolStack;
import org.jgroups.tests.ChannelTestBase;
import org.jgroups.util.Util;
import org.testng.annotations.*;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

/** Tests {@link org.jgroups.blocks.locking.LockService}
 * @author <NAME>
 */
@Test(groups=Global.STACK_DEPENDENT,sequential=true)
public class LockServiceTest extends ChannelTestBase {
    protected JChannel c1, c2, c3;
    protected LockService s1, s2, s3;
    protected Lock lock;
    protected static final String LOCK="sample-lock";


    @BeforeClass
    protected void init() throws Exception {
        c1=createChannel(true, 3, "A");
        addLockingProtocol(c1);
        s1=new LockService(c1);
        c1.connect("LockServiceTest");

        c2=createChannel(c1, "B");
        s2=new LockService(c2);
        c2.connect("LockServiceTest");

        c3=createChannel(c1, "C");
        s3=new LockService(c3);
        c3.connect("LockServiceTest");
        
        lock=s1.getLock(LOCK);
    }


    @AfterClass
    protected void cleanup() {
        Util.close(c3,c2,c1);
    }

    @BeforeMethod
    protected void unlockAll() {
        s3.unlockAll();
        s2.unlockAll();
        s1.unlockAll();
    }


    public void testSimpleLock() {
        lock(lock, LOCK);
        unlock(lock, LOCK);
    }

    public void testLockingOfAlreadyAcquiredLock() {
        lock(lock, LOCK);
        lock(lock, LOCK);
        unlock(lock, LOCK);
    }

    public void testUnsuccessfulTryLock() {
        System.out.println("s1:\n" + s1.printLocks() +
                             "\ns2:\n" + s2.printLocks() +
                             "\ns3:\n" + s3.printLocks());


        Lock lock2=s2.getLock(LOCK);
        lock(lock2, LOCK);
        try {
            boolean rc=tryLock(lock, LOCK);
            assert !rc;
            unlock(lock, LOCK);
        }
        finally {
            unlock(lock2, LOCK);
        }
    }

    public void testUnsuccessfulTryLockTimeout() throws InterruptedException {
        Lock lock2=s2.getLock(LOCK);
        lock(lock2, LOCK);
        try {
            boolean rc=tryLock(lock, 1000, LOCK);
            assert !rc;
        }
        finally {
            unlock(lock2, LOCK);
        }
    }

    public void testSuccessfulTryLockTimeout() throws InterruptedException, BrokenBarrierException {
        final CyclicBarrier barrier=new CyclicBarrier(2);
        Thread locker=new Locker(barrier);
        locker.start();
        barrier.await();
        boolean rc=tryLock(lock, 10000, LOCK);
        assert rc;<commit_msg>added a test for interrupted locks<commit_after>package org.jgroups.blocks;

import org.jgroups.Global;
import org.jgroups.JChannel;
import org.jgroups.blocks.locking.LockService;
import org.jgroups.protocols.CENTRAL_LOCK;
import org.jgroups.stack.ProtocolStack;
import org.jgroups.tests.ChannelTestBase;
import org.jgroups.util.Util;
import org.testng.annotations.*;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

/** Tests {@link org.jgroups.blocks.locking.LockService}
 * @author <NAME>
 */
@Test(groups=Global.STACK_DEPENDENT,sequential=true)
public class LockServiceTest extends ChannelTestBase {
    protected JChannel c1, c2, c3;
    protected LockService s1, s2, s3;
    protected Lock lock;
    protected static final String LOCK="sample-lock";


    @BeforeClass
    protected void init() throws Exception {
        c1=createChannel(true, 3, "A");
        addLockingProtocol(c1);
        s1=new LockService(c1);
        c1.connect("LockServiceTest");

        c2=createChannel(c1, "B");
        s2=new LockService(c2);
        c2.connect("LockServiceTest");

        c3=createChannel(c1, "C");
        s3=new LockService(c3);
        c3.connect("LockServiceTest");
        
        lock=s1.getLock(LOCK);
    }


    @AfterClass
    protected void cleanup() {
        Util.close(c3,c2,c1);
    }

    @BeforeMethod
    protected void unlockAll() {
        s3.unlockAll();
        s2.unlockAll();
        s1.unlockAll();
    }


    public void testSimpleLock() {
        lock(lock, LOCK);
        unlock(lock, LOCK);
    }

    public void testLockingOfAlreadyAcquiredLock() {
        lock(lock, LOCK);
        lock(lock, LOCK);
        unlock(lock, LOCK);
    }

    public void testUnsuccessfulTryLock() {
        System.out.println("s1:\n" + s1.printLocks() +
                             "\ns2:\n" + s2.printLocks() +
                             "\ns3:\n" + s3.printLocks());


        Lock lock2=s2.getLock(LOCK);
        lock(lock2, LOCK);
        try {
            boolean rc=tryLock(lock, LOCK);
            assert !rc;
            unlock(lock, LOCK);
        }
        finally {
            unlock(lock2, LOCK);
        }
    }

    public void testUnsuccessfulTryLockTimeout() throws InterruptedException {
        Lock lock2=s2.getLock(LOCK);
        lock(lock2, LOCK);
        try {
            boolean rc=tryLock(lock, 1000, LOCK);
            assert !rc;
        }
        finally {
            unlock(lock2, LOCK);
        }
    }


    public void testLockInterrupt() {
        // Interrupt ourselves before trying to acquire lock
        Thread.currentThread().interrupt();

        lock.lock();
        try {
            System.out.println("Locks we have: " + s1.printLocks());
            if(Thread.interrupted()) {
                System.out.println("We still have interrupt flag set, as it should be");
            }
            else {
                assert false : "Interrupt status was lost - we don't want this!";
            }
        }
        finally {
            lock.unlock();
        }
    }

     public void testTryLockInterrupt() {
        // Interrupt ourselves before trying to acquire lock
        Thread.currentThread().interrupt();

        lock.tryLock();
        try {
            System.out.println("Locks we have: " + s1.printLocks());
            if(Thread.interrupted()) {
                System.out.println("We still have interrupt flag set, as it should be");
            }
            else {
                assert false : "Interrupt status was lost - we don't want this!";
            }
        }
        finally {
            lock.unlock();
        }
    }

    @Test(expectedExceptions=InterruptedException.class)
    public void testLockInterruptibly() throws InterruptedException {
        // Interrupt ourselves before trying to acquire lock
        Thread.currentThread().interrupt();

        lock.lockInterruptibly();
        try {
            System.out.println("Locks we have: " + s1.printLocks());
            if(Thread.interrupted()) {
                System.out.println("We still have interrupt flag set, as it should be");
            }
            else {
                assert false : "Interrupt status was lost - we don't want this!";
            }
        }
        finally {
            lock.unlock();
        }
    }


    public void testSuccessfulTryLockTimeout() throws InterruptedException, BrokenBarrierException {
        final CyclicBarrier barrier=new CyclicBarrier(2);
        Thread locker=new Locker(barrier);
        locker.start();
        barrier.await();
        boolean rc=tryLock(lock, 10000, LOCK);
        assert rc;
{'fraction_non_alphanumeric': 0.112, 'fraction_numerical': 0.014295081967213115, 'mean_word_length': 2.156456953642384, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1211387', 'n_tokens_mistral': 2300, 'n_tokens_neox': 2234, 'n_words': 472}