Package org.reactfx

Source Code of org.reactfx.TicksTest

package org.reactfx;

import static org.junit.Assert.*;

import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.reactfx.util.FxTimer;


public class TicksTest {
    private ScheduledExecutorService scheduler;

    @Before
    public void setUp() throws Exception {
        scheduler = Executors.newSingleThreadScheduledExecutor();
        new JFXPanel(); // initializes JavaFX toolkit
    }

    @After
    public void tearDown() throws Exception {
        scheduler.shutdown();
    }

    @Test
    public void fxTest() throws InterruptedException, ExecutionException {
        CompletableFuture<Integer> nTicks = new CompletableFuture<>();
        Platform.runLater(() -> {
            EventCounter counter = new EventCounter();
            Subscription sub = EventStreams.ticks(Duration.ofMillis(100)).subscribe(counter);
            FxTimer.runLater(Duration.ofMillis(350), sub::unsubscribe); // stop after 3 ticks
            // wait a little more to test that no more than 3 ticks arrive anyway
            FxTimer.runLater(Duration.ofMillis(550), () -> nTicks.complete(counter.get()));
        });
        assertEquals(3, nTicks.get().intValue());
    }

    @Test
    public void executorTest() throws InterruptedException, ExecutionException {
        ExecutorService executor = Executors.newSingleThreadExecutor();

        CompletableFuture<Integer> nTicks = new CompletableFuture<>();
        executor.execute(() -> {
            EventCounter counter = new EventCounter();
            Subscription sub = EventStreams.ticks(Duration.ofMillis(100), scheduler, executor).subscribe(counter);
            ScheduledExecutorServiceTimer.create(Duration.ofMillis(350), sub::unsubscribe, scheduler, executor).restart(); // stop after 3 ticks
            // wait a little more to test that no more than 3 ticks arrive anyway
            ScheduledExecutorServiceTimer.create(Duration.ofMillis(550), () -> nTicks.complete(counter.get()), scheduler, executor).restart();
        });
        assertEquals(3, nTicks.get().intValue());

        executor.shutdown();
    }
}
TOP

Related Classes of org.reactfx.TicksTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.