@Test
  public void test() {
    EventSource<Integer> source = new EventSource<>();
    SuspendableEventStream<Integer> suspendable = source.pausable();
    List<Integer> emitted = new ArrayList<>();
    suspendable.subscribe(emitted::add);

    source.push(1);
    suspendable.suspendWhile(
        () -> {
          source.push(2);
          source.push(3);
        });
    source.push(4);

    assertEquals(Arrays.asList(1, 2, 3, 4), emitted);
  }
Esempio n. 2
0
  @Test
  public void countDownTest() {
    EventSource<Void> src1 = new EventSource<Void>();
    EventSource<Void> src2 = new EventSource<Void>();
    EventSource<Void> reset = new EventSource<Void>();

    BiFunction<Integer, Void, Tuple2<Integer, Optional<String>>> countdown =
        (s, i) -> s == 1 ? t(3, Optional.of("COUNTDOWN REACHED")) : t(s - 1, Optional.empty());

    EventStream<String> countdowns =
        StateMachine.init(3)
            .on(src1)
            .transmit(countdown)
            .on(src2)
            .transmit(countdown)
            .on(reset)
            .transition((s, i) -> 3)
            .toEventStream();

    Counter counter = new Counter();
    Subscription sub = countdowns.hook(x -> counter.inc()).pin();

    src1.push(null);
    src2.push(null);
    assertEquals(0, counter.get());

    src1.push(null);
    assertEquals(1, counter.getAndReset());

    src2.push(null);
    src2.push(null);
    reset.push(null);
    assertEquals(0, counter.get());
    src2.push(null);
    assertEquals(0, counter.get());
    src1.push(null);
    assertEquals(0, counter.get());
    src2.push(null);
    assertEquals(1, counter.getAndReset());

    sub.unsubscribe();
    src1.push(null);
    src1.push(null);
    src1.push(null);
    src1.push(null);
    src1.push(null);
    src1.push(null);
    assertEquals(0, counter.get());
  }