@Test public void testZipAsync() { int NUM = (int) (RxRingBuffer.SIZE * 2.1); AtomicInteger c1 = new AtomicInteger(); AtomicInteger c2 = new AtomicInteger(); TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); Observable<Integer> zipped = Observable.zip( incrementingIntegers(c1).subscribeOn(Schedulers.computation()), incrementingIntegers(c2).subscribeOn(Schedulers.computation()), new Func2<Integer, Integer, Integer>() { @Override public Integer call(Integer t1, Integer t2) { return t1 + t2; } }); zipped.take(NUM).subscribe(ts); ts.awaitTerminalEvent(); ts.assertNoErrors(); System.out.println( "testZipAsync => Received: " + ts.getOnNextEvents().size() + " Emitted: " + c1.get() + " / " + c2.get()); assertEquals(NUM, ts.getOnNextEvents().size()); assertTrue(c1.get() < RxRingBuffer.SIZE * 3); assertTrue(c2.get() < RxRingBuffer.SIZE * 3); }
@Test public void testZipAsync() { int NUM = (int) (Observable.bufferSize() * 2.1); AtomicInteger c1 = new AtomicInteger(); AtomicInteger c2 = new AtomicInteger(); TestSubscriber<Integer> ts = new TestSubscriber<>(); Observable<Integer> zipped = Observable.zip( incrementingIntegers(c1).subscribeOn(Schedulers.computation()), incrementingIntegers(c2).subscribeOn(Schedulers.computation()), (t1, t2) -> t1 + t2); zipped.take(NUM).subscribe(ts); ts.awaitTerminalEvent(); ts.assertNoErrors(); System.out.println( "testZipAsync => Received: " + ts.valueCount() + " Emitted: " + c1.get() + " / " + c2.get()); assertEquals(NUM, ts.valueCount()); int max = Observable.bufferSize() * 5; assertTrue("" + c1.get() + " >= " + max, c1.get() < max); assertTrue("" + c2.get() + " >= " + max, c2.get() < max); }
/** * Occasionally zip may be invoked with 0 observables. Test that we don't block indefinitely * instead of immediately invoking zip with 0 argument. * * <p>We now expect an NoSuchElementException since last() requires at least one value and nothing * will be emitted. */ @Test(expected = NoSuchElementException.class) public void nonBlockingObservable() { final Object invoked = new Object(); Collection<Observable<Object>> observables = Collections.emptyList(); Observable<Object> result = Observable.zip( observables, args -> { System.out.println("received: " + args); assertEquals("No argument should have been passed", 0, args.length); return invoked; }); assertSame(invoked, result.toBlocking().last()); }
/** This won't compile if super/extends isn't done correctly on generics */ @Test public void testCovarianceOfZip() { Observable<HorrorMovie> horrors = Observable.just(new HorrorMovie()); Observable<CoolRating> ratings = Observable.just(new CoolRating()); Observable.<Movie, CoolRating, Result>zip(horrors, ratings, combine) .toBlocking() .forEach(action); Observable.<Movie, CoolRating, Result>zip(horrors, ratings, combine) .toBlocking() .forEach(action); Observable.<Media, Rating, ExtendedResult>zip(horrors, ratings, combine) .toBlocking() .forEach(extendedAction); Observable.<Media, Rating, Result>zip(horrors, ratings, combine).toBlocking().forEach(action); Observable.<Media, Rating, ExtendedResult>zip(horrors, ratings, combine) .toBlocking() .forEach(action); Observable.<Movie, CoolRating, Result>zip(horrors, ratings, combine); }