Skip to content

Commit 232612c

Browse files
committed
Incoporate review suggestions.
Splits a compound unit test into to parts. Uses mockito instead of a bespoke test object. Removes unused import statements. Changes the order of the Finally action w.r.t. onComplete/onError.
1 parent e858315 commit 232612c

File tree

1 file changed

+23
-32
lines changed

1 file changed

+23
-32
lines changed

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

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@
1818
import static org.junit.Assert.*;
1919
import static org.mockito.Mockito.*;
2020

21-
import java.lang.reflect.Array;
22-
import java.util.ArrayList;
23-
import java.util.List;
24-
import java.util.concurrent.CountDownLatch;
25-
21+
import org.junit.Before;
2622
import org.junit.Test;
2723

2824
import rx.Observable;
@@ -47,9 +43,10 @@ public final class OperationFinally {
4743
* @param sequence An observable sequence of elements
4844
* @param action An action to be taken when the sequence is complete or throws an exception
4945
* @return An observable sequence with the same elements as the input.
50-
* After the last element is consumed (just before {@link Observer#onComplete} is called),
51-
* or when an exception is thrown (just before {@link Observer#onError}), the action will be called.
52-
* @see http://msdn.microsoft.com/en-us/library/hh212133(v=vs.103).aspx
46+
* After the last element is consumed (and {@link Observer#onCompleted} has been called),
47+
* or after an exception is thrown (and {@link Observer#onError} has been called),
48+
* the given action will be called.
49+
* @see <a href="http://msdn.microsoft.com/en-us/library/hh212133(v=vs.103).aspx">MSDN Observable.Finally method</a>
5350
*/
5451
public static <T> Func1<Observer<T>, Subscription> finally0(final Observable<T> sequence, final Action0 action) {
5552
return new Func1<Observer<T>, Subscription>() {
@@ -94,14 +91,14 @@ private class FinallyObserver implements Observer<T> {
9491

9592
@Override
9693
public void onCompleted() {
97-
finalAction.call();
9894
observer.onCompleted();
95+
finalAction.call();
9996
}
10097

10198
@Override
10299
public void onError(Exception e) {
103-
finalAction.call();
104100
observer.onError(e);
101+
finalAction.call();
105102
}
106103

107104
@Override
@@ -112,30 +109,24 @@ public void onNext(T args) {
112109
}
113110

114111
public static class UnitTest {
115-
private static class TestAction implements Action0 {
116-
public int called = 0;
117-
@Override public void call() {
118-
called++;
119-
}
112+
private Action0 aAction0;
113+
private Observer<String> aObserver;
114+
@Before
115+
public void before() {
116+
aAction0 = mock(Action0.class);
117+
aObserver = mock(Observer.class);
118+
}
119+
private void checkActionCalled(Observable<String> input) {
120+
Observable.create(finally0(input, aAction0)).subscribe(aObserver);
121+
verify(aAction0, times(1)).call();
122+
}
123+
@Test
124+
public void testFinallyCalledOnComplete() {
125+
checkActionCalled(Observable.toObservable(new String[] {"1", "2", "3"}));
120126
}
121-
122127
@Test
123-
public void testFinally() {
124-
final String[] n = {"1", "2", "3"};
125-
final Observable<String> nums = Observable.toObservable(n);
126-
TestAction action = new TestAction();
127-
action.called = 0;
128-
Observable<String> fin = Observable.create(finally0(nums, action));
129-
@SuppressWarnings("unchecked")
130-
Observer<String> aObserver = mock(Observer.class);
131-
fin.subscribe(aObserver);
132-
assertEquals(1, action.called);
133-
134-
action.called = 0;
135-
Observable<String> error = Observable.<String>error(new RuntimeException("expected"));
136-
fin = Observable.create(finally0(error, action));
137-
fin.subscribe(aObserver);
138-
assertEquals(1, action.called);
128+
public void testFinallyCalledOnError() {
129+
checkActionCalled(Observable.<String>error(new RuntimeException("expected")));
139130
}
140131
}
141132
}

0 commit comments

Comments
 (0)