// Path: ersatz/src/main/java/io/github/cjstehno/ersatz/util/Timeout.java
// static boolean isTrueBefore(final Supplier<Boolean> condition, final long timeout, final TimeUnit unit) {
//     val started = currentTimeMillis();
//     val timeoutMs = unit.toMillis(timeout);
// 
//     if (!condition.get()) {
//         val executor = newSingleThreadScheduledExecutor();
//         try {
//             var future = executor.schedule(condition::get, 250, MILLISECONDS);
// 
//             while (!future.get(timeout, unit) && !timedOut(started, timeoutMs)) {
//                 future = executor.schedule(condition::get, 250, MILLISECONDS);
//             }
// 
//             return !timedOut(started, timeoutMs);
// 
//         } catch (final Exception e) {
//             return false;
//         } finally {
//             executor.shutdown();
//         }
// 
//     } else {
//         return true;
//     }
// }


import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;
import static io.github.cjstehno.ersatz.util.Timeout.isTrueBefore;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
 * Copyright (C) 2022 Christopher J. Stehno
 *
 * 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 io.github.cjstehno.ersatz.util;




class TimeoutTest {

    private AtomicBoolean state;
    private ScheduledExecutorService executor;

    @BeforeEach void beforeEach() {
        state = new AtomicBoolean(false);
        executor = Executors.newSingleThreadScheduledExecutor();
    }

    @AfterEach void afterEach() {
        executor.shutdown();
    }

    @Test @DisplayName("when ready immediately")
    void readyImmediately() {
        changeStateIn(0);

// Path: ersatz/src/main/java/io/github/cjstehno/ersatz/util/Timeout.java
// static boolean isTrueBefore(final Supplier<Boolean> condition, final long timeout, final TimeUnit unit) {
//     val started = currentTimeMillis();
//     val timeoutMs = unit.toMillis(timeout);
// 
//     if (!condition.get()) {
//         val executor = newSingleThreadScheduledExecutor();
//         try {
//             var future = executor.schedule(condition::get, 250, MILLISECONDS);
// 
//             while (!future.get(timeout, unit) && !timedOut(started, timeoutMs)) {
//                 future = executor.schedule(condition::get, 250, MILLISECONDS);
//             }
// 
//             return !timedOut(started, timeoutMs);
// 
//         } catch (final Exception e) {
//             return false;
//         } finally {
//             executor.shutdown();
//         }
// 
//     } else {
//         return true;
//     }
// }


// Path: ersatz/src/test/java/io/github/cjstehno/ersatz/util/TimeoutTest.java
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;
import static io.github.cjstehno.ersatz.util.Timeout.isTrueBefore;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * Copyright (C) 2022 Christopher J. Stehno
 *
 * 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 io.github.cjstehno.ersatz.util;




class TimeoutTest {

    private AtomicBoolean state;
    private ScheduledExecutorService executor;

    @BeforeEach void beforeEach() {
        state = new AtomicBoolean(false);
        executor = Executors.newSingleThreadScheduledExecutor();
    }

    @AfterEach void afterEach() {
        executor.shutdown();
    }

    @Test @DisplayName("when ready immediately")
    void readyImmediately() {
        changeStateIn(0);

        assertTrue(isTrueBefore(() -> state.get(), 1, SECONDS));