Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the issue that 'take' does not call 'onError' #441

Merged
merged 1 commit into from
Oct 22, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 72 additions & 4 deletions rxjava-core/src/main/java/rx/operators/OperationTake.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,30 @@
*/
package rx.operators;

import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Test;
import org.mockito.InOrder;

import rx.Observable;
import rx.Observable.OnSubscribeFunc;
import rx.Observer;
import rx.Subscription;
import rx.subscriptions.Subscriptions;
import rx.util.functions.Func1;

/**
* Returns an Observable that emits the first <code>num</code> items emitted by the source
Expand Down Expand Up @@ -114,30 +124,47 @@ private class ItemObserver implements Observer<T> {
private final Observer<? super T> observer;

private final AtomicInteger counter = new AtomicInteger();
private volatile boolean hasEmitedError = false;

public ItemObserver(Observer<? super T> observer) {
this.observer = observer;
}

@Override
public void onCompleted() {
if (hasEmitedError) {
return;
}
if (counter.getAndSet(num) < num) {
observer.onCompleted();
}
}

@Override
public void onError(Throwable e) {
if (hasEmitedError) {
return;
}
if (counter.getAndSet(num) < num) {
observer.onError(e);
}
}

@Override
public void onNext(T args) {
if (hasEmitedError) {
return;
}
final int count = counter.incrementAndGet();
if (count <= num) {
observer.onNext(args);
try {
observer.onNext(args);
} catch (Throwable ex) {
hasEmitedError = true;
observer.onError(ex);
subscription.unsubscribe();
return;
}
if (count == num) {
observer.onCompleted();
}
Expand Down Expand Up @@ -184,6 +211,47 @@ public void testTake2() {
verify(aObserver, times(1)).onCompleted();
}

@Test(expected = IllegalArgumentException.class)
public void testTakeWithError() {
Observable.from(1, 2, 3).take(1).map(new Func1<Integer, Integer>() {
public Integer call(Integer t1) {
throw new IllegalArgumentException("some error");
}
}).toBlockingObservable().single();
}

@Test
public void testTakeWithErrorHappeningInOnNext() {
Observable<Integer> w = Observable.from(1, 2, 3).take(2).map(new Func1<Integer, Integer>() {
public Integer call(Integer t1) {
throw new IllegalArgumentException("some error");
}
});

@SuppressWarnings("unchecked")
Observer<Integer> observer = mock(Observer.class);
w.subscribe(observer);
InOrder inOrder = inOrder(observer);
inOrder.verify(observer, times(1)).onError(any(IllegalArgumentException.class));
inOrder.verifyNoMoreInteractions();
}

@Test
public void testTakeWithErrorHappeningInTheLastOnNext() {
Observable<Integer> w = Observable.from(1, 2, 3).take(1).map(new Func1<Integer, Integer>() {
public Integer call(Integer t1) {
throw new IllegalArgumentException("some error");
}
});

@SuppressWarnings("unchecked")
Observer<Integer> observer = mock(Observer.class);
w.subscribe(observer);
InOrder inOrder = inOrder(observer);
inOrder.verify(observer, times(1)).onError(any(IllegalArgumentException.class));
inOrder.verifyNoMoreInteractions();
}

@Test
public void testTakeDoesntLeakErrors() {
Observable<String> source = Observable.create(new OnSubscribeFunc<String>()
Expand Down