package com.atlassian.stash.internal.activity;

import com.atlassian.bitbucket.util.concurrent.LockGuard;
import org.junit.Test;

import static org.junit.Assert.assertSame;

public class NoOpActivityLockTest {

    private final NoOpActivityLock lock = new NoOpActivityLock();

    //There's not really any reason to test this; it's here because it can be
    @Test
    public void testLockIsSingleInstance() {
        assertSame(lock.forCreate(), lock.forDelete());
    }

    //Since this lock will be used the majority of the time, verify that "holding" either "lock" doesn't
    //actually prevent the other type of operation from running
    @Test(timeout = 10000) //10 second timeout in case things go sideways
    public void testNonBlocking() throws Exception {
        LockGuard create = lock.forCreate();

        Thread deleter = new Thread("noop-deleter") {

            @Override
            public void run() {
                //Acquire and release the delete lock while the create lock is also being "held"
                LockGuard delete = lock.forDelete();
                delete.close();
            }
        };
        deleter.setDaemon(true);
        deleter.start();
        deleter.join();

        create.close();
    }
}