-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
2.x: Fix Completable.andThen(Completable) not running on observeOn scheduler. #6362
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
src/main/java/io/reactivex/internal/operators/completable/CompletableAndThenCompletable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* Copyright (c) 2016-present, RxJava Contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in | ||
* compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is | ||
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See | ||
* the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.reactivex.internal.operators.completable; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import io.reactivex.*; | ||
import io.reactivex.disposables.Disposable; | ||
import io.reactivex.internal.disposables.DisposableHelper; | ||
|
||
public final class CompletableAndThenCompletable extends Completable { | ||
|
||
final CompletableSource source; | ||
|
||
final CompletableSource next; | ||
|
||
public CompletableAndThenCompletable(CompletableSource source, CompletableSource next) { | ||
this.source = source; | ||
this.next = next; | ||
} | ||
|
||
@Override | ||
protected void subscribeActual(CompletableObserver observer) { | ||
source.subscribe(new SourceObserver(observer, next)); | ||
} | ||
|
||
static final class SourceObserver | ||
extends AtomicReference<Disposable> | ||
implements CompletableObserver, Disposable { | ||
|
||
private static final long serialVersionUID = -4101678820158072998L; | ||
|
||
final CompletableObserver actualObserver; | ||
|
||
final CompletableSource next; | ||
|
||
SourceObserver(CompletableObserver actualObserver, CompletableSource next) { | ||
this.actualObserver = actualObserver; | ||
this.next = next; | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Disposable d) { | ||
if (DisposableHelper.setOnce(this, d)) { | ||
actualObserver.onSubscribe(this); | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(Throwable e) { | ||
actualObserver.onError(e); | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
next.subscribe(new NextObserver(this, actualObserver)); | ||
} | ||
|
||
@Override | ||
public void dispose() { | ||
DisposableHelper.dispose(this); | ||
} | ||
|
||
@Override | ||
public boolean isDisposed() { | ||
return DisposableHelper.isDisposed(get()); | ||
} | ||
} | ||
|
||
static final class NextObserver implements CompletableObserver { | ||
|
||
final AtomicReference<Disposable> parent; | ||
|
||
final CompletableObserver downstream; | ||
|
||
public NextObserver(AtomicReference<Disposable> parent, CompletableObserver downstream) { | ||
this.parent = parent; | ||
this.downstream = downstream; | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Disposable d) { | ||
DisposableHelper.replace(parent, d); | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
downstream.onComplete(); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable e) { | ||
downstream.onError(e); | ||
} | ||
} | ||
} |
185 changes: 185 additions & 0 deletions
185
...java/io/reactivex/internal/operators/completable/CompletableAndThenCompletableabTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
/** | ||
* Copyright (c) 2016-present, RxJava Contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in | ||
* compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is | ||
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See | ||
* the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.reactivex.internal.operators.completable; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import org.junit.Test; | ||
|
||
import io.reactivex.*; | ||
import io.reactivex.exceptions.TestException; | ||
import io.reactivex.functions.Action; | ||
import io.reactivex.observers.TestObserver; | ||
import io.reactivex.schedulers.Schedulers; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class CompletableAndThenCompletableabTest { | ||
@Test(expected = NullPointerException.class) | ||
public void andThenCompletableCompleteNull() { | ||
Completable.complete() | ||
.andThen((Completable) null); | ||
} | ||
|
||
@Test | ||
public void andThenCompletableCompleteComplete() { | ||
Completable.complete() | ||
.andThen(Completable.complete()) | ||
.test() | ||
.assertComplete(); | ||
} | ||
|
||
@Test | ||
public void andThenCompletableCompleteError() { | ||
Completable.complete() | ||
.andThen(Completable.error(new TestException("test"))) | ||
.test() | ||
.assertNotComplete() | ||
.assertNoValues() | ||
.assertError(TestException.class) | ||
.assertErrorMessage("test"); | ||
} | ||
|
||
@Test | ||
public void andThenCompletableCompleteNever() { | ||
Completable.complete() | ||
.andThen(Completable.never()) | ||
.test() | ||
.assertNoValues() | ||
.assertNoErrors() | ||
.assertNotComplete(); | ||
} | ||
|
||
@Test | ||
public void andThenCompletableErrorComplete() { | ||
Completable.error(new TestException("bla")) | ||
.andThen(Completable.complete()) | ||
.test() | ||
.assertNotComplete() | ||
.assertNoValues() | ||
.assertError(TestException.class) | ||
.assertErrorMessage("bla"); | ||
} | ||
|
||
@Test | ||
public void andThenCompletableErrorNever() { | ||
Completable.error(new TestException("bla")) | ||
.andThen(Completable.never()) | ||
.test() | ||
.assertNotComplete() | ||
.assertNoValues() | ||
.assertError(TestException.class) | ||
.assertErrorMessage("bla"); | ||
} | ||
|
||
@Test | ||
public void andThenCompletableErrorError() { | ||
Completable.error(new TestException("error1")) | ||
.andThen(Completable.error(new TestException("error2"))) | ||
.test() | ||
.assertNotComplete() | ||
.assertNoValues() | ||
.assertError(TestException.class) | ||
.assertErrorMessage("error1"); | ||
} | ||
|
||
@Test | ||
public void andThenCanceled() { | ||
final AtomicInteger completableRunCount = new AtomicInteger(); | ||
Completable.fromRunnable(new Runnable() { | ||
@Override | ||
public void run() { | ||
completableRunCount.incrementAndGet(); | ||
} | ||
}) | ||
.andThen(Completable.complete()) | ||
.test(true) | ||
.assertEmpty(); | ||
assertEquals(1, completableRunCount.get()); | ||
} | ||
|
||
@Test | ||
public void andThenFirstCancels() { | ||
final TestObserver<Void> to = new TestObserver<Void>(); | ||
Completable.fromRunnable(new Runnable() { | ||
@Override | ||
public void run() { | ||
to.cancel(); | ||
} | ||
}) | ||
.andThen(Completable.complete()) | ||
.subscribe(to); | ||
to | ||
.assertNotComplete() | ||
.assertNoErrors(); | ||
} | ||
|
||
@Test | ||
public void andThenSecondCancels() { | ||
final TestObserver<Void> to = new TestObserver<Void>(); | ||
Completable.complete() | ||
.andThen(Completable.fromRunnable(new Runnable() { | ||
@Override | ||
public void run() { | ||
to.cancel(); | ||
} | ||
})) | ||
.subscribe(to); | ||
to | ||
.assertNotComplete() | ||
.assertNoErrors(); | ||
} | ||
|
||
@Test | ||
public void andThenDisposed() { | ||
TestHelper.checkDisposed(Completable.complete() | ||
.andThen(Completable.complete())); | ||
} | ||
|
||
@Test | ||
public void andThenNoInterrupt() throws InterruptedException { | ||
for (int k = 0; k < 100; k++) { | ||
final int count = 10; | ||
final CountDownLatch latch = new CountDownLatch(count); | ||
final boolean[] interrupted = {false}; | ||
|
||
for (int i = 0; i < count; i++) { | ||
Completable.complete() | ||
.subscribeOn(Schedulers.io()) | ||
.observeOn(Schedulers.io()) | ||
.andThen(Completable.fromAction(new Action() { | ||
@Override | ||
public void run() throws Exception { | ||
try { | ||
Thread.sleep(30); | ||
} catch (InterruptedException e) { | ||
System.out.println("Interrupted! " + Thread.currentThread()); | ||
interrupted[0] = true; | ||
} | ||
} | ||
})) | ||
.subscribe(new Action() { | ||
@Override | ||
public void run() throws Exception { | ||
latch.countDown(); | ||
} | ||
}); | ||
} | ||
|
||
latch.await(); | ||
assertFalse("The second Completable was interrupted!", interrupted[0]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this test removed without explanation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved it to CompletableAndThenCompletableTest since it specifically tested
.andThen(Completable)
.