Skip to content

Commit 02e14dc

Browse files
committed
Fixed the issue of takeLast(items, 0)
1 parent a317533 commit 02e14dc

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

rxjava-core/src/main/java/rx/operators/OperationTakeLast.java

+34
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import rx.Observable;
2828
import rx.Observable.OnSubscribeFunc;
29+
import rx.subscriptions.Subscriptions;
2930
import rx.Observer;
3031
import rx.Subscription;
3132

@@ -59,6 +60,26 @@ private static class TakeLast<T> implements OnSubscribeFunc<T> {
5960
}
6061

6162
public Subscription onSubscribe(Observer<? super T> observer) {
63+
if(count == 0) {
64+
items.subscribe(new Observer<T>() {
65+
66+
@Override
67+
public void onCompleted() {
68+
}
69+
70+
@Override
71+
public void onError(Throwable e) {
72+
}
73+
74+
@Override
75+
public void onNext(T args) {
76+
}
77+
78+
}).unsubscribe();
79+
observer.onCompleted();
80+
return Subscriptions.empty();
81+
}
82+
6283
return subscription.wrap(items.subscribe(new ItemObserver(observer)));
6384
}
6485

@@ -140,6 +161,19 @@ public void testTakeLast2() {
140161
verify(aObserver, times(1)).onCompleted();
141162
}
142163

164+
@Test
165+
public void testTakeLastWithZeroCount() {
166+
Observable<String> w = Observable.from("one");
167+
Observable<String> take = Observable.create(takeLast(w, 0));
168+
169+
@SuppressWarnings("unchecked")
170+
Observer<String> aObserver = mock(Observer.class);
171+
take.subscribe(aObserver);
172+
verify(aObserver, never()).onNext("one");
173+
verify(aObserver, never()).onError(any(Throwable.class));
174+
verify(aObserver, times(1)).onCompleted();
175+
}
176+
143177
}
144178

145179
}

0 commit comments

Comments
 (0)