/*
 * Copyright 2016 Crown Copyright
 *
 * Licensed 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 stroom.cluster.lock.impl.db;


import org.junit.jupiter.api.Test;
import stroom.test.common.util.db.DbTestUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import static org.assertj.core.api.Assertions.assertThat;

class TestDbClusterLock {
    @Test
    void test() throws InterruptedException {

        final ClusterLockDbConnProvider clusterLockDbConnProvider = DbTestUtil.getTestDbDatasource(
                new ClusterLockDbModule(), new ClusterLockConfig());

        final DbClusterLock dbClusterLock = new DbClusterLock(clusterLockDbConnProvider);
        final DbClusterLockThreads dbClusterLockThreads = new DbClusterLockThreads(dbClusterLock);

        final CountDownLatch countDownLatch = new CountDownLatch(2);
        final List<Integer> sequence = new ArrayList<>(3);

        // This thread should acquire the lock first stopping the second thread
        // from adding to the sequence until after this thread completes.
        final Thread thread1 = new Thread(() -> {
            dbClusterLockThreads.thread1("TEST", sequence);
            countDownLatch.countDown();
        });

        final Thread thread2 = new Thread(() -> {
            dbClusterLockThreads.thread2("TEST", sequence);
            countDownLatch.countDown();
        });

        // Start the threads.
        thread1.start();
        thread2.start();

        // Now make sure the sequence is as expected.
        countDownLatch.await();
        assertThat(sequence.size()).isEqualTo(3);
        assertThat(sequence.get(0).intValue()).isEqualTo(1);
        assertThat(sequence.get(1).intValue()).isEqualTo(2);
        assertThat(sequence.get(2).intValue()).isEqualTo(3);
    }
}

@@ -636,43 +636,8 @@
 ;%0A%0A%0A
-import org.junit.jupiter.api.Test;%0A
 impo
@@ -679,16 +679,52 @@
 tUtil;%0A%0A
+import org.junit.jupiter.api.Test;%0A%0A
 import j
@@ -809,16 +809,54 @@
 wnLatch;
+%0Aimport java.util.concurrent.TimeUnit;
 %0A%0Aimport
@@ -2098,24 +2098,133 @@
 ted.%0A       
+ // Use a timeout so if it goes wrong the test won't just sit there for ages.%0A        final boolean success =
  countDownLa
@@ -2233,19 +2233,40 @@
 h.await(
+20, TimeUnit.SECONDS
 );%0A
+%0A
         
@@ -2281,38 +2281,124 @@
 at(s
-equence.size()).isEqualTo(3
+uccess)%0A                .withFailMessage(%22Gave up waiting for the latch to count down%22)%0A                .isTrue(
 );%0A
+%0A
     
@@ -2424,27 +2424,91 @@
 ence
-.get(0).intValue())
+)%0A                .hasSize(3);%0A        assertThat(sequence.get(0))%0A                
 .isE
@@ -2552,28 +2552,34 @@
 e.get(1)
-.intValue())
+)%0A                
 .isEqual
@@ -2623,20 +2623,26 @@
 t(2)
-.intValue())
+)%0A                
 .isE

Add timeout to TestDbClusterLock