package com.github.davidmoten.rx2;

import java.util.function.LongFunction;

import org.junit.Test;

import com.github.davidmoten.junit.Asserts;
import com.github.davidmoten.rx2.exceptions.ThrowingException;

import io.reactivex.Flowable;
import io.reactivex.functions.BiFunction;

public class FlowablesTest {

    @Test
    public void isUtilityClass() {
        Asserts.assertIsUtilityClass(Flowables.class);
    }

    @Test
    public void testFetchByRequest() {
        final BiFunction<Long, Long, Flowable<Integer>> fetch = new BiFunction<Long, Long, Flowable<Integer>>() {
            @Override
            public Flowable<Integer> apply(Long start, Long n) {
                return Flowable.just(n.intValue()).repeat(n);
            }

        };
        Flowables.fetchPagesByRequest(fetch) //
                .test(0) //
                .assertNoValues() //
                .requestMore(1) //
                .assertValue(1) //
                .requestMore(2) //
                .assertValues(1, 2, 2) //
                .requestMore(3) //
                .assertValues(1, 2, 2, 3, 3, 3) //
                .assertNotTerminated();
    }

    @Test
    public void testFetchByRequestError() {
        final BiFunction<Long, Long, Flowable<Integer>> fetch = new BiFunction<Long, Long, Flowable<Integer>>() {
            @Override
            public Flowable<Integer> apply(Long start, Long n) {
                throw new ThrowingException();
            }

        };
        Flowables.fetchPagesByRequest(fetch) //
                .test(1) //
                .assertNoValues() //
                .assertError(ThrowingException.class);
    }

}

@@ -33,49 +33,8 @@
 2;%0A%0A
-import java.util.function.LongFunction;%0A%0A
 impo
@@ -458,39 +458,36 @@
  Long, Flowable%3C
-Integer
+Long
 %3E%3E fetch = new B
@@ -509,39 +509,36 @@
  Long, Flowable%3C
-Integer
+Long
 %3E%3E() %7B%0A         
@@ -570,39 +570,36 @@
 public Flowable%3C
-Integer
+Long
 %3E apply(Long sta
@@ -599,33 +599,39 @@
 ong start, Long 
-n
+request
 ) %7B%0A            
@@ -654,35 +654,32 @@
 ble.
-just(n.intValue()).repeat(n
+rangeLong(start, request
 );%0A 
@@ -880,17 +880,18 @@
 rtValue(
-1
+0L
 ) //%0A   
@@ -952,23 +952,26 @@
 tValues(
-1, 2
+0L, 1L
 , 2
+L
 ) //%0A   
@@ -1036,24 +1036,30 @@
 ues(
-1, 2
+0L, 1L
 , 2
+L
 , 3
+L
 , 
-3, 3
+4L, 5L
 ) //

add paging