com.github.davidmoten.rx.Transformers Java Examples

The following examples show how to use com.github.davidmoten.rx.Transformers. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OperatorBufferToFileTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
private static void checkRateForOneKMessagesWithOptions(Options options) {
    Scheduler scheduler = createSingleThreadScheduler();
    DataSerializer<Integer> serializer = createSerializer1K();

    int max = Integer.parseInt(System.getProperty("max.medium", "3000"));
    long t = System.currentTimeMillis();
    int last = Observable.range(1, max)
            //
            .compose(Transformers.onBackpressureBufferToFile(serializer, scheduler, options))
            // log
            // .lift(Logging.<Integer>
            // logger().showCount().every(1000).showMemory().log())
            .last().toBlocking().single();
    t = System.currentTimeMillis() - t;
    assertEquals(max, last);
    System.out.println("rate = " + df((double) max * MEDIUM_MESSAGE_SIZE / 1000 / (t))
            + "MB/s (1K messages, " + rolloverStatus(options) + ") duration="
            + format(t / 1000.0));
    waitUntilWorkCompleted(scheduler);
}
 
Example #2
Source File: OnSubscribeMapLastTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void testMapLastRequestAmount() {
    List<Long> list = new ArrayList<Long>();
    Observable.range(1, 10) //
            .doOnRequest(Actions.addTo(list))//
            .compose(Transformers.mapLast(new Func1<Integer, Integer>() {
                @Override
                public Integer call(Integer x) {
                    return x + 1;
                }
            })).to(TestingHelper.<Integer>testWithRequest(1)) //
            .assertNotCompleted() //
            .assertValuesAndClear(1) //
            .requestMore(3) //
            .assertValues(2, 3, 4);
    assertEquals(Arrays.asList(2L, 3L), list);
}
 
Example #3
Source File: TransformerStateMachineTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnsubscriptionFromTransition() {
    Func0<Integer> initialState = Functions.constant0(1);
    Func3<Integer, Integer, Subscriber<Integer>, Integer> transition = new Func3<Integer, Integer, Subscriber<Integer>, Integer>() {

        @Override
        public Integer call(Integer collection, Integer t, Subscriber<Integer> subscriber) {
            subscriber.onNext(123);
            subscriber.unsubscribe();
            return 1;
        }

    };
    Func2<Integer, Observer<Integer>, Boolean> completion = Functions.alwaysTrue2();
    Transformer<Integer, Integer> transformer = Transformers.stateMachine(initialState,
            transition, completion);
    TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
    Observable.just(1, 1, 1).repeat().compose(transformer).subscribe(ts);
    ts.assertValue(123);
    ts.assertCompleted();
    ts.assertUnsubscribed();
}
 
Example #4
Source File: OrderedMergeTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
private void checkAllCombinationsFromPowerSet(Scheduler scheduler) {
	// this test covers everything!
	for (int n = 0; n <= 10; n++) {
		Set<Integer> numbers = Sets.newTreeSet();
		for (int i = 1; i <= n; i++) {
			numbers.add(i);
		}
		for (Set<Integer> a : Sets.powerSet(numbers)) {
			TreeSet<Integer> x = Sets.newTreeSet(a);
			TreeSet<Integer> y = Sets.newTreeSet(Sets.difference(numbers, x));
			Observable<Integer> o1 = from(x).subscribeOn(scheduler);
			Observable<Integer> o2 = from(y).subscribeOn(scheduler);
			List<Integer> list = o1.compose(Transformers.orderedMergeWith(o2, comparator)).toList().toBlocking()
					.single();
			// System.out.println(x + " " + y);
			assertEquals(Lists.newArrayList(numbers), list);
		}
	}
}
 
Example #5
Source File: OperatorBufferPredicateBoundaryTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void bufferWhileWith1() {
    TestSubscriber<List<Integer>> ts = new TestSubscriber<List<Integer>>();
    
    Observable.range(1, 10)
    .compose(Transformers.bufferWhile(UtilityFunctions.<Integer>alwaysTrue()))
    .subscribe(ts);
    
    ts.assertValues(
            Arrays.asList(1),
            Arrays.asList(2),
            Arrays.asList(3),
            Arrays.asList(4),
            Arrays.asList(5),
            Arrays.asList(6),
            Arrays.asList(7),
            Arrays.asList(8),
            Arrays.asList(9),
            Arrays.asList(10)
        );
    ts.assertNoErrors();
    ts.assertCompleted();
}
 
Example #6
Source File: OperatorBufferPredicateBoundaryTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void bufferWhileWith5() {
    TestSubscriber<List<Integer>> ts = new TestSubscriber<List<Integer>>();
    
    Observable.range(1, 10)
    .compose(Transformers.bufferWhile(new Func1<Integer, Boolean>() {
        @Override
        public Boolean call(Integer v) {
            return v == 5;
        }
    }))
    .subscribe(ts);
    
    ts.assertValues(
            Arrays.asList(1, 2, 3, 4),
            Arrays.asList(5, 6, 7, 8, 9, 10)
        );
    ts.assertNoErrors();
    ts.assertCompleted();
}
 
Example #7
Source File: OperatorBufferPredicateBoundaryTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void bufferWhileWith20() {
    TestSubscriber<List<Integer>> ts = new TestSubscriber<List<Integer>>();
    
    Observable.range(1, 10)
    .compose(Transformers.bufferWhile(new Func1<Integer, Boolean>() {
        @Override
        public Boolean call(Integer v) {
            return v == 20;
        }
    }))
    .subscribe(ts);
    
    ts.assertValues(
            Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
        );
    ts.assertNoErrors();
    ts.assertCompleted();
}
 
Example #8
Source File: OperatorBufferPredicateBoundaryTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void bufferUntilWith5() {
    TestSubscriber<List<Integer>> ts = new TestSubscriber<List<Integer>>();
    
    Observable.range(1, 10)
    .compose(Transformers.bufferUntil(new Func1<Integer, Boolean>() {
        @Override
        public Boolean call(Integer v) {
            return v == 5;
        }
    }))
    .subscribe(ts);
    
    ts.assertValues(
            Arrays.asList(1, 2, 3, 4, 5),
            Arrays.asList(6, 7, 8, 9, 10)
        );
    ts.assertNoErrors();
    ts.assertCompleted();
}
 
Example #9
Source File: OperatorBufferPredicateBoundaryTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void bufferUntilWith20() {
    TestSubscriber<List<Integer>> ts = new TestSubscriber<List<Integer>>();
    
    Observable.range(1, 10)
    .compose(Transformers.bufferUntil(new Func1<Integer, Boolean>() {
        @Override
        public Boolean call(Integer v) {
            return v == 20;
        }
    }))
    .subscribe(ts);
    
    ts.assertValues(
            Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
        );
    ts.assertNoErrors();
    ts.assertCompleted();
}
 
Example #10
Source File: OperatorBufferPredicateBoundaryTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void bufferWhileSomeNull() {
    TestSubscriber<List<Integer>> ts = new TestSubscriber<List<Integer>>();
    
    Observable.just(1, 2, 3, null, 4, 5)
    .compose(Transformers.bufferUntil(new Func1<Integer, Boolean>() {
        @Override
        public Boolean call(Integer v) {
            return v != null && v == 20;
        }
    }))
    .subscribe(ts);
    
    ts.assertValues(
            Arrays.asList(1, 2, 3, null, 4, 5)
        );
    ts.assertNoErrors();
    ts.assertCompleted();
}
 
Example #11
Source File: OperatorBufferPredicateBoundaryTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void bufferUntilSomeNull() {
    TestSubscriber<List<Integer>> ts = new TestSubscriber<List<Integer>>();
    
    Observable.just(1, 2, 3, null, 4, 5)
    .compose(Transformers.bufferUntil(new Func1<Integer, Boolean>() {
        @Override
        public Boolean call(Integer v) {
            return v != null && v == 20;
        }
    }))
    .subscribe(ts);
    
    ts.assertValues(
            Arrays.asList(1, 2, 3, null, 4, 5)
        );
    ts.assertNoErrors();
    ts.assertCompleted();
}
 
Example #12
Source File: OperatorBufferPredicateBoundaryTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void bufferUntilPredicateThrows() {
    TestSubscriber<List<Integer>> ts = new TestSubscriber<List<Integer>>();
    
    Observable.just(1, 2, 3, null, 4, 5)
    .compose(Transformers.bufferUntil(new Func1<Integer, Boolean>() {
        @Override
        public Boolean call(Integer v) {
            return v == 20;
        }
    }))
    .subscribe(ts);
    
    ts.assertNoValues();
    ts.assertError(NullPointerException.class);
    ts.assertNotCompleted();
}
 
Example #13
Source File: OperatorBufferPredicateBoundaryTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void bufferWhileBackpressureFullBuffer() {
    TestSubscriber<List<Integer>> ts = TestSubscriber.create(0);
    
    int count = RxRingBuffer.SIZE * 4;
    
    Observable.range(1, count)
    .compose(Transformers.bufferWhile(UtilityFunctions.<Integer>alwaysFalse()))
    .subscribe(ts);
    
    ts.assertNoValues();
    ts.assertNoErrors();
    ts.assertNotCompleted();
    
    ts.requestMore(1);
    
    ts.assertValueCount(1);
    ts.assertNoErrors();
    ts.assertCompleted();
    
    Assert.assertEquals(count, ts.getOnNextEvents().get(0).size());
}
 
Example #14
Source File: OperatorBufferPredicateBoundaryTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void bufferUntilBackpressureFullBuffer() {
    TestSubscriber<List<Integer>> ts = TestSubscriber.create(0);
    
    int count = RxRingBuffer.SIZE * 4;
    
    Observable.range(1, count)
    .compose(Transformers.bufferUntil(UtilityFunctions.<Integer>alwaysFalse()))
    .subscribe(ts);
    
    ts.assertNoValues();
    ts.assertNoErrors();
    ts.assertNotCompleted();
    
    ts.requestMore(1);
    
    ts.assertValueCount(1);
    ts.assertNoErrors();
    ts.assertCompleted();
    
    Assert.assertEquals(count, ts.getOnNextEvents().get(0).size());
}
 
Example #15
Source File: OperatorBufferToFileTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void handlesEmpty() {
    System.out.println("handlesEmpty");
    Scheduler scheduler = createSingleThreadScheduler();
    for (int i = 0; i < loops(); i++) {
        Observable.<String> empty()
                .compose(Transformers.onBackpressureBufferToFile(DataSerializers.string(),
                        scheduler))
                .to(TestingHelper.<String> test()) //
                .requestMore(1) //
                .awaitTerminalEvent() //
                .assertNoErrors() //
                .assertNoValues() //
                .assertCompleted();
        waitUntilWorkCompleted(scheduler);
    }
}
 
Example #16
Source File: OperatorBufferToFileTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void handlesEmptyUsingJavaIOSerialization() {
    System.out.println("handlesEmptyUsingJavaIOSerialization");
    Scheduler scheduler = createSingleThreadScheduler();
    for (int i = 0; i < loops(); i++) {
        TestSubscriber<String> ts = TestSubscriber.create(0);
        Observable.<String> empty().compose(Transformers
                .onBackpressureBufferToFile(DataSerializers.<String> javaIO(), scheduler))
                .subscribe(ts);
        ts.requestMore(1);
        ts.awaitTerminalEvent();
        ts.assertNoErrors();
        ts.assertNoValues();
        ts.assertCompleted();
        waitUntilWorkCompleted(scheduler);
    }
}
 
Example #17
Source File: OperatorBufferToFileTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void handlesThreeUsingJavaIOSerialization() {
    System.out.println("handlesThreeUsingJavaIOSerialization");
    Scheduler scheduler = createSingleThreadScheduler();
    for (int i = 0; i < loops(); i++) {
        TestSubscriber<String> ts = TestSubscriber.create();
        Observable.just("a", "bc", "def").compose(Transformers
                .onBackpressureBufferToFile(DataSerializers.<String> javaIO(), scheduler))
                .subscribe(ts);
        ts.awaitTerminalEvent();
        ts.assertNoErrors();
        ts.assertValues("a", "bc", "def");
        ts.assertCompleted();
        waitUntilWorkCompleted(scheduler);
    }
}
 
Example #18
Source File: OperatorBufferToFileTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void handlesUnsubscription() throws InterruptedException {
    System.out.println("handlesUnsubscription");
    Scheduler scheduler = createSingleThreadScheduler();
    TestSubscriber<String> ts = TestSubscriber.create(0);
    Observable.just("abc", "def", "ghi")
            //
            .compose(Transformers.onBackpressureBufferToFile(DataSerializers.string(),
                    scheduler, Options.defaultInstance()))
            .subscribe(ts);
    ts.requestMore(2);
    TimeUnit.MILLISECONDS.sleep(500);
    ts.unsubscribe();
    TimeUnit.MILLISECONDS.sleep(500);
    ts.assertValues("abc", "def");
    waitUntilWorkCompleted(scheduler);
}
 
Example #19
Source File: OperatorBufferToFileTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
private static void checkRateForSmallMessagesWithOptions(Options options) {
    Scheduler scheduler = createSingleThreadScheduler();
    DataSerializer<Integer> serializer = DataSerializers.integer();

    int max = Integer.parseInt(System.getProperty("max.small", "3000000"));
    long t = System.currentTimeMillis();
    int last = Observable.range(1, max)
            //
            .compose(Transformers.onBackpressureBufferToFile(serializer, scheduler, options))
            // log
            // .lift(Logging.<Integer>
            // logger().showCount().every(1000).showMemory().log())
            .last().toBlocking().single();
    t = System.currentTimeMillis() - t;
    assertEquals(max, last);
    System.out.println("rate = " + df((double) max * 4 / (t) / 1000) + "MB/s (4B messages, "
            + rolloverStatus(options) + ") duration=" + format(t / 1000.0));
    waitUntilWorkCompleted(scheduler);
}
 
Example #20
Source File: OperatorBufferPredicateBoundaryTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void bufferWhilePredicateThrows() {
    TestSubscriber<List<Integer>> ts = new TestSubscriber<List<Integer>>();
    
    Observable.just(1, 2, 3, null, 4, 5)
    .compose(Transformers.bufferWhile(new Func1<Integer, Boolean>() {
        @Override
        public Boolean call(Integer v) {
            return v == 20;
        }
    }))
    .subscribe(ts);
    
    ts.assertNoValues();
    ts.assertError(NullPointerException.class);
    ts.assertNotCompleted();
}
 
Example #21
Source File: TransformerOnBackpressureBufferRequestLimiting.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    Observable.range(1, 10000) //
            .doOnRequest(new Action1<Long>() {
                @Override
                public void call(Long n) {
                    System.out.println("requested " + n);
                }
            }).doOnUnsubscribe(new Action0() {
                @Override
                public void call() {
                    System.out.println("unsubscribed");
                }
            }) //
            .compose(Transformers.<Integer> onBackpressureBufferRequestLimiting()) //
            .take(10) //
            .subscribeOn(Schedulers.io()) //
            .doOnNext(Actions.println()) //
            .count().toBlocking().single();
    Thread.sleep(2000);
}
 
Example #22
Source File: OperatorSampleFirstTest.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
@Test
public void testThrottlingWithError() {
    Observable<String> source = Observable.create(new OnSubscribe<String>() {
        @Override
        public void call(Subscriber<? super String> observer) {
            Exception error = new TestException();
            publishNext(observer, 100, "one"); // Should be published since
                                               // it is first
            publishNext(observer, 200, "two"); // Should be skipped since
                                               // onError will arrive before
                                               // the timeout expires
            publishError(observer, 300, error); // Should be published as
                                                // soon as the timeout
                                                // expires.
        }
    });

    Observable<String> sampled = source
            .compose(Transformers.<String> sampleFirst(400, TimeUnit.MILLISECONDS, scheduler));
    sampled.subscribe(observer);

    InOrder inOrder = inOrder(observer);

    scheduler.advanceTimeTo(400, TimeUnit.MILLISECONDS);
    inOrder.verify(observer).onNext("one");
    inOrder.verify(observer).onError(any(TestException.class));
    inOrder.verifyNoMoreInteractions();
}
 
Example #23
Source File: OperatorBufferPredicateBoundaryTest.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
@Test
public void emptySourceBufferWhile() {
    TestSubscriber<List<Integer>> ts = new TestSubscriber<List<Integer>>();

    Observable.<Integer>empty()
    .compose(Transformers.bufferWhile(UtilityFunctions.<Integer>alwaysTrue()))
    .subscribe(ts);
    
    ts.assertNoValues();
    ts.assertNoErrors();
    ts.assertCompleted();
}
 
Example #24
Source File: OperatorBufferPredicateBoundaryTest.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
@Test
public void errorSourceBufferWhile() {
    TestSubscriber<List<Integer>> ts = new TestSubscriber<List<Integer>>();

    Observable.<Integer>error(new IOException())
    .compose(Transformers.bufferWhile(UtilityFunctions.<Integer>alwaysTrue()))
    .subscribe(ts);
    
    ts.assertNoValues();
    ts.assertError(IOException.class);
    ts.assertNotCompleted();
}
 
Example #25
Source File: ObservableReverseTest.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
@Test
public void testEmpty() {
    Observable.empty() //
            .compose(Transformers.reverse()) //
            .to(TestingHelper.test()) //
            .assertNoValues() //
            .assertCompleted();
}
 
Example #26
Source File: StringSplitWithLimitTest.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
@Test
public void testLimitHonouredOnAllItemsIncludingLast() {
    Observable<String> o = Observable.just("boo:an", "d:you");
    List<String> expected = asList("bo", "an", "yo");
    List<String> list = o.compose(Transformers.split(2, ":", 1)).toList().toBlocking().single();
    assertEquals(expected, list);
}
 
Example #27
Source File: ObservableReverseTest.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
@Test
public void testMany() {
    Observable.just(1,2,3,4,5) //
            .compose(Transformers.reverse()) //
            .to(TestingHelper.test()) //
            .assertValues(5,4,3,2,1) //
            .assertCompleted();
}
 
Example #28
Source File: OperatorBufferToFileTest.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
private void checkHandlesThreeElements(Options options) {
    List<String> b = Observable.just("abc", "def", "ghi")
            //
            .compose(Transformers.onBackpressureBufferToFile(DataSerializers.string(),
                    Schedulers.immediate(), options))
            .toList().toBlocking().single();
    assertEquals(Arrays.asList("abc", "def", "ghi"), b);
}
 
Example #29
Source File: StringSplitWithLimitTest.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
@Test
public void testLimitOnOneLongItemSpread() {
    Observable<String> o = Observable.just("bot", "her");
    List<String> expected = asList("bo");
    List<String> list = o.compose(Transformers.split(2, ":", 1)).toList().toBlocking().single();
    assertEquals(expected, list);
}
 
Example #30
Source File: OperatorBufferToFileTest.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
@Test
public void testNullsInStreamHandledByJavaIOSerialization() {
    List<Integer> list = Observable.just(1, 2, (Integer) null, 4)
            .compose(Transformers.<Integer> onBackpressureBufferToFile()).toList().toBlocking()
            .single();
    assertEquals(Arrays.asList(1, 2, (Integer) null, 4), list);
}