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<>();
        CompletableFuture<Integer> nTicks0 = new CompletableFuture<>();
        Platform.runLater(() -> {
            EventCounter counter = new EventCounter();
            Subscription sub = EventStreams.ticks(Duration.ofMillis(100)).subscribe(counter::accept);
            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()));

            EventCounter counter0 = new EventCounter();
            Subscription sub0 = EventStreams.ticks0(Duration.ofMillis(100)).subscribe(counter0::accept);
            // 000 (tick 1) -> 100 (tick 2) -> 200 (tick 3) -> 300 (tick 4) -> 350 (interrupted) = 4 ticks
            FxTimer.runLater(Duration.ofMillis(350), sub0::unsubscribe); // stop after 4 ticks
            // wait a little more to test that no more than 4 ticks arrive anyway
            FxTimer.runLater(Duration.ofMillis(550), () -> nTicks0.complete(counter0.get()));
        });
        assertEquals(3, nTicks.get().intValue());
        assertEquals(4, nTicks0.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::accept);
            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();
    }
}

@@ -870,16 +870,21 @@
 void fxT
+icksT
 est() th
@@ -1006,80 +1006,8 @@
 ();%0A
-        CompletableFuture%3CInteger%3E nTicks0 = new CompletableFuture%3C%3E();%0A
     
@@ -1457,29 +1457,290 @@
 er.get()));%0A
-%0A
     
+    %7D);%0A        assertEquals(3, nTicks.get().intValue());%0A    %7D%0A%0A    @Test%0A    public void fxTicks0Test() throws InterruptedException, ExecutionException %7B%0A        CompletableFuture%3CInteger%3E nTicks = new CompletableFuture%3C%3E();%0A        Platform.runLater(() -%3E %7B%0A%09
         Even
@@ -1755,17 +1755,16 @@
  counter
-0
  = new E
@@ -1774,33 +1774,33 @@
 tCounter();%0A
- 
+%09
         
    Subscript
@@ -1779,35 +1779,32 @@
 ter();%0A%09        
-   
 Subscription sub
@@ -1803,17 +1803,16 @@
 tion sub
-0
  = Event
@@ -1867,17 +1867,16 @@
 (counter
-0
 ::accept
@@ -1870,36 +1870,33 @@
 unter::accept);%0A
-    
+%09
         // 000 (
@@ -1974,36 +1974,33 @@
 pted) = 4 ticks%0A
-    
+%09
         FxTimer.
@@ -2035,17 +2035,16 @@
 50), sub
-0
 ::unsubs
@@ -2069,28 +2069,25 @@
 ter 4 ticks%0A
-    
+%09
         // w
@@ -2144,36 +2144,33 @@
 s arrive anyway%0A
-    
+%09
         FxTimer.
@@ -2214,17 +2214,16 @@
 %3E nTicks
-0
 .complet
@@ -2231,17 +2231,16 @@
 (counter
-0
 .get()))
@@ -2257,58 +2257,8 @@
 %7D);%0A
-        assertEquals(3, nTicks.get().intValue());%0A
     
@@ -2283,17 +2283,16 @@
 , nTicks
-0
 .get().i

Split tests of ticks and ticks0.