/*
 *
 * Copyright (c) 2006-2019, Speedment, Inc. All Rights Reserved.
 *
 * 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 com.speedment.runtime.core.internal.component;

import org.junit.jupiter.api.Test;

import java.util.concurrent.atomic.AtomicBoolean;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

final class StatisticsReporterSchedulerComponentImplTest {

    @Test
    void guardedCallStalled() {

        final AtomicBoolean called = new AtomicBoolean();
        final StatisticsReporterSchedulerComponentImpl instance = new StatisticsReporterSchedulerComponentImpl(true);

        final Thread t0 = new Thread(() -> { instance.guardedCall(() -> dly(1000)); });
        final Thread t1 = new Thread(() -> { called.set(true); });

        t0.start();
        dly(100);
        t1.start();

        assertFalse(called.get());
    }


    @Test
    void guardedCallClean() {

        final AtomicBoolean called = new AtomicBoolean();
        final StatisticsReporterSchedulerComponentImpl instance = new StatisticsReporterSchedulerComponentImpl(true);

        instance.guardedCall(() -> {});
        instance.guardedCall(() -> called.set(true));

        assertTrue(called.get());
    }



    private void dly(long ms) {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException ignored) {

        }
    }

}
@@ -1242,17 +1242,17 @@
  -%3E dly(
-1
+2
 000)); %7D
@@ -1253,16 +1253,37 @@
 0)); %7D);
+    // Hog the thread
 %0A       
@@ -1319,16 +1319,43 @@
 (() -%3E %7B
+ instance.guardedCall(() -%3E
  called.
@@ -1363,21 +1363,82 @@
 et(true)
+)
 ; %7D);
+ // This should be ignored since there is a pending Runnable
 %0A%0A      
@@ -1470,30 +1470,136 @@
 (100
+0
 );
-%0A        t1.start();
+ // Make %22sure%22 the first thread has started%0A        t1.start();    // Start the second thread (who's run() shall be ignored)
 %0A%0A  

Fix and comment test that did not work as expected