package org.sagebionetworks.bridge.redis;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.UUID;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.sagebionetworks.bridge.dao.DistributedLockDao;

public class RedisDistributedLockDaoTest {

    private JedisStringOps strOps;
    private DistributedLockDao lockDao;
    private String id;

    @Before
    public void before() {
        strOps = new JedisStringOps();
        lockDao = new RedisDistributedLockDao();
        id = UUID.randomUUID().toString();
    }

    @After
    public void after() {
        if (strOps != null) {
            strOps.clearRedis(id + "*");
        }
    }

    @Test
    public void test() {
        // Acquire lock
        String lockId = lockDao.acquireLock(getClass(), id, 60);
        assertNotNull(lockId);
        String redisKey = RedisKey.LOCK.getRedisKey(
                id + RedisKey.SEPARATOR + getClass().getCanonicalName());
        String redisLockId = strOps.get(redisKey).execute();
        assertNotNull(redisLockId);
        assertEquals(redisLockId, lockId);
        assertTrue(strOps.ttl(redisKey).execute() > 0);
        // Acquire again should fail
        assertNull(lockDao.acquireLock(getClass(), id));
        // Release lock
        boolean released = lockDao.releaseLock(getClass(), id, "incorrect lock id");
        assertFalse(released);
        released = lockDao.releaseLock(getClass(), id, lockId);
        assertTrue(released);
        // Once released, can be re-acquired
        lockId = lockDao.acquireLock(getClass(), id, 1);
        lockDao.releaseLock(getClass(), id, lockId);
    }
}

@@ -256,16 +256,53 @@
 ertTrue;
+%0Aimport static org.junit.Assert.fail;
 %0A%0Aimport
@@ -448,16 +448,94 @@
 LockDao;
+%0Aimport org.sagebionetworks.bridge.exceptions.ConcurrentModificationException;
 %0A%0Apublic
@@ -1487,13 +1487,48 @@
 uld 
-fail%0A
+get back an exception%0A        try %7B%0A    
     
@@ -1576,24 +1576,215 @@
 ss(), id));%0A
+        %7D catch (ConcurrentModificationException e) %7B%0A            assertTrue(%22ConcurrentModificationException expected.%22, true);%0A        %7D catch (Throwable e) %7B%0A            fail();%0A        %7D%0A
         // R

Fix test failure