Skip to content

Commit

Permalink
2.x: Assert instead of print Undeliverable in some tests (#6205)
Browse files Browse the repository at this point in the history
* 2.x: Assert instead of print Undeliverable in some tests

* Fix error count not guaranteed to be 4.
  • Loading branch information
akarnokd authored Sep 7, 2018
1 parent cc45975 commit b9c00a8
Show file tree
Hide file tree
Showing 4 changed files with 271 additions and 176 deletions.
162 changes: 96 additions & 66 deletions src/test/java/io/reactivex/maybe/MaybeCreateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@

import static org.junit.Assert.assertTrue;

import java.util.List;

import org.junit.Test;

import io.reactivex.*;
import io.reactivex.disposables.*;
import io.reactivex.exceptions.TestException;
import io.reactivex.functions.Cancellable;
import io.reactivex.plugins.RxJavaPlugins;

public class MaybeCreateTest {
@Test(expected = NullPointerException.class)
Expand All @@ -30,84 +33,111 @@ public void nullArgument() {

@Test
public void basic() {
final Disposable d = Disposables.empty();

Maybe.<Integer>create(new MaybeOnSubscribe<Integer>() {
@Override
public void subscribe(MaybeEmitter<Integer> e) throws Exception {
e.setDisposable(d);

e.onSuccess(1);
e.onError(new TestException());
e.onSuccess(2);
e.onError(new TestException());
}
}).test().assertResult(1);

assertTrue(d.isDisposed());
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
final Disposable d = Disposables.empty();

Maybe.<Integer>create(new MaybeOnSubscribe<Integer>() {
@Override
public void subscribe(MaybeEmitter<Integer> e) throws Exception {
e.setDisposable(d);

e.onSuccess(1);
e.onError(new TestException());
e.onSuccess(2);
e.onError(new TestException());
}
}).test().assertResult(1);

assertTrue(d.isDisposed());
TestHelper.assertUndeliverable(errors, 0, TestException.class);
} finally {
RxJavaPlugins.reset();
}
}

@Test
public void basicWithCancellable() {
final Disposable d1 = Disposables.empty();
final Disposable d2 = Disposables.empty();

Maybe.<Integer>create(new MaybeOnSubscribe<Integer>() {
@Override
public void subscribe(MaybeEmitter<Integer> e) throws Exception {
e.setDisposable(d1);
e.setCancellable(new Cancellable() {
@Override
public void cancel() throws Exception {
d2.dispose();
}
});

e.onSuccess(1);
e.onError(new TestException());
e.onSuccess(2);
e.onError(new TestException());
}
}).test().assertResult(1);

assertTrue(d1.isDisposed());
assertTrue(d2.isDisposed());
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
final Disposable d1 = Disposables.empty();
final Disposable d2 = Disposables.empty();

Maybe.<Integer>create(new MaybeOnSubscribe<Integer>() {
@Override
public void subscribe(MaybeEmitter<Integer> e) throws Exception {
e.setDisposable(d1);
e.setCancellable(new Cancellable() {
@Override
public void cancel() throws Exception {
d2.dispose();
}
});

e.onSuccess(1);
e.onError(new TestException());
e.onSuccess(2);
e.onError(new TestException());
}
}).test().assertResult(1);

assertTrue(d1.isDisposed());
assertTrue(d2.isDisposed());

TestHelper.assertUndeliverable(errors, 0, TestException.class);
} finally {
RxJavaPlugins.reset();
}
}

@Test
public void basicWithError() {
final Disposable d = Disposables.empty();

Maybe.<Integer>create(new MaybeOnSubscribe<Integer>() {
@Override
public void subscribe(MaybeEmitter<Integer> e) throws Exception {
e.setDisposable(d);

e.onError(new TestException());
e.onSuccess(2);
e.onError(new TestException());
}
}).test().assertFailure(TestException.class);

assertTrue(d.isDisposed());
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
final Disposable d = Disposables.empty();

Maybe.<Integer>create(new MaybeOnSubscribe<Integer>() {
@Override
public void subscribe(MaybeEmitter<Integer> e) throws Exception {
e.setDisposable(d);

e.onError(new TestException());
e.onSuccess(2);
e.onError(new TestException());
}
}).test().assertFailure(TestException.class);

assertTrue(d.isDisposed());

TestHelper.assertUndeliverable(errors, 0, TestException.class);
} finally {
RxJavaPlugins.reset();
}
}

@Test
public void basicWithCompletion() {
final Disposable d = Disposables.empty();

Maybe.<Integer>create(new MaybeOnSubscribe<Integer>() {
@Override
public void subscribe(MaybeEmitter<Integer> e) throws Exception {
e.setDisposable(d);

e.onComplete();
e.onSuccess(2);
e.onError(new TestException());
}
}).test().assertComplete();

assertTrue(d.isDisposed());
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
final Disposable d = Disposables.empty();

Maybe.<Integer>create(new MaybeOnSubscribe<Integer>() {
@Override
public void subscribe(MaybeEmitter<Integer> e) throws Exception {
e.setDisposable(d);

e.onComplete();
e.onSuccess(2);
e.onError(new TestException());
}
}).test().assertComplete();

assertTrue(d.isDisposed());

TestHelper.assertUndeliverable(errors, 0, TestException.class);
} finally {
RxJavaPlugins.reset();
}
}

@Test(expected = IllegalArgumentException.class)
Expand Down
144 changes: 87 additions & 57 deletions src/test/java/io/reactivex/maybe/MaybeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1496,68 +1496,91 @@ public void nullArgument() {

@Test
public void basic() {
final Disposable d = Disposables.empty();
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
final Disposable d = Disposables.empty();

Maybe.<Integer>create(new MaybeOnSubscribe<Integer>() {
@Override
public void subscribe(MaybeEmitter<Integer> e) throws Exception {
e.setDisposable(d);
Maybe.<Integer>create(new MaybeOnSubscribe<Integer>() {
@Override
public void subscribe(MaybeEmitter<Integer> e) throws Exception {
e.setDisposable(d);

e.onSuccess(1);
e.onError(new TestException());
e.onSuccess(2);
e.onError(new TestException());
e.onComplete();
}
})
.test()
.assertResult(1);

e.onSuccess(1);
e.onError(new TestException());
e.onSuccess(2);
e.onError(new TestException());
e.onComplete();
}
})
.test()
.assertResult(1);
assertTrue(d.isDisposed());

assertTrue(d.isDisposed());
TestHelper.assertUndeliverable(errors, 0, TestException.class);
TestHelper.assertUndeliverable(errors, 1, TestException.class);
} finally {
RxJavaPlugins.reset();
}
}

@Test
public void basicWithError() {
final Disposable d = Disposables.empty();
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
final Disposable d = Disposables.empty();

Maybe.<Integer>create(new MaybeOnSubscribe<Integer>() {
@Override
public void subscribe(MaybeEmitter<Integer> e) throws Exception {
e.setDisposable(d);
Maybe.<Integer>create(new MaybeOnSubscribe<Integer>() {
@Override
public void subscribe(MaybeEmitter<Integer> e) throws Exception {
e.setDisposable(d);

e.onError(new TestException());
e.onSuccess(2);
e.onError(new TestException());
e.onComplete();
}
})
.test()
.assertFailure(TestException.class);
e.onError(new TestException());
e.onSuccess(2);
e.onError(new TestException());
e.onComplete();
}
})
.test()
.assertFailure(TestException.class);

assertTrue(d.isDisposed());

assertTrue(d.isDisposed());
TestHelper.assertUndeliverable(errors, 0, TestException.class);
} finally {
RxJavaPlugins.reset();
}
}

@Test
public void basicWithComplete() {
final Disposable d = Disposables.empty();
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
final Disposable d = Disposables.empty();

Maybe.<Integer>create(new MaybeOnSubscribe<Integer>() {
@Override
public void subscribe(MaybeEmitter<Integer> e) throws Exception {
e.setDisposable(d);
Maybe.<Integer>create(new MaybeOnSubscribe<Integer>() {
@Override
public void subscribe(MaybeEmitter<Integer> e) throws Exception {
e.setDisposable(d);

e.onComplete();
e.onSuccess(1);
e.onError(new TestException());
e.onComplete();
e.onSuccess(2);
e.onError(new TestException());
}
})
.test()
.assertResult();

e.onComplete();
e.onSuccess(1);
e.onError(new TestException());
e.onComplete();
e.onSuccess(2);
e.onError(new TestException());
}
})
.test()
.assertResult();
assertTrue(d.isDisposed());

assertTrue(d.isDisposed());
TestHelper.assertUndeliverable(errors, 0, TestException.class);
TestHelper.assertUndeliverable(errors, 1, TestException.class);
} finally {
RxJavaPlugins.reset();
}
}

@Test(expected = IllegalArgumentException.class)
Expand Down Expand Up @@ -2359,21 +2382,28 @@ public void accept(Integer v, Throwable e) throws Exception {

@Test
public void doOnEventError() {
final List<Object> list = new ArrayList<Object>();
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
final List<Object> list = new ArrayList<Object>();

TestException ex = new TestException();
TestException ex = new TestException();

assertTrue(Maybe.<Integer>error(ex)
.doOnEvent(new BiConsumer<Integer, Throwable>() {
@Override
public void accept(Integer v, Throwable e) throws Exception {
list.add(v);
list.add(e);
}
})
.subscribe().isDisposed());
assertTrue(Maybe.<Integer>error(ex)
.doOnEvent(new BiConsumer<Integer, Throwable>() {
@Override
public void accept(Integer v, Throwable e) throws Exception {
list.add(v);
list.add(e);
}
})
.subscribe().isDisposed());

assertEquals(Arrays.asList(null, ex), list);

assertEquals(Arrays.asList(null, ex), list);
TestHelper.assertError(errors, 0, OnErrorNotImplementedException.class);
} finally {
RxJavaPlugins.reset();
}
}

@Test
Expand Down
Loading

0 comments on commit b9c00a8

Please sign in to comment.