-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add EventStreams.ticks(Duration) factory method.
- Loading branch information
1 parent
e754843
commit 7753d84
Showing
5 changed files
with
167 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.reactfx; | ||
|
||
import java.util.function.Consumer; | ||
|
||
class EventCounter implements Consumer<Object> { | ||
private int count = 0; | ||
|
||
@Override | ||
public void accept(Object o) { | ||
++count; | ||
} | ||
|
||
public int get() { | ||
return count; | ||
} | ||
|
||
public int getAndReset() { | ||
int res = count; | ||
count = 0; | ||
return res; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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(); | ||
} | ||
} |