-
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 Single.timeout unnecessary dispose calls #5586
Merged
+223
−60
Merged
Changes from all commits
Commits
Show all changes
2 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
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 |
---|---|---|
|
@@ -15,15 +15,18 @@ | |
|
||
import static org.junit.Assert.*; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.junit.Test; | ||
|
||
import io.reactivex.Single; | ||
import io.reactivex.*; | ||
import io.reactivex.exceptions.TestException; | ||
import io.reactivex.functions.Action; | ||
import io.reactivex.observers.TestObserver; | ||
import io.reactivex.plugins.RxJavaPlugins; | ||
import io.reactivex.schedulers.TestScheduler; | ||
import io.reactivex.subjects.PublishSubject; | ||
import io.reactivex.subjects.*; | ||
|
||
public class SingleTimeoutTest { | ||
|
||
|
@@ -69,4 +72,141 @@ public void mainError() { | |
.awaitDone(5, TimeUnit.SECONDS) | ||
.assertFailure(TestException.class); | ||
} | ||
|
||
@Test | ||
public void disposeWhenFallback() { | ||
TestScheduler sch = new TestScheduler(); | ||
|
||
SingleSubject<Integer> subj = SingleSubject.create(); | ||
|
||
subj.timeout(1, TimeUnit.SECONDS, sch, Single.just(1)) | ||
.test(true) | ||
.assertEmpty(); | ||
|
||
assertFalse(subj.hasObservers()); | ||
} | ||
|
||
@Test | ||
public void isDisposed() { | ||
TestHelper.checkDisposed(SingleSubject.create().timeout(1, TimeUnit.DAYS)); | ||
} | ||
|
||
@Test | ||
public void fallbackDispose() { | ||
TestScheduler sch = new TestScheduler(); | ||
|
||
SingleSubject<Integer> subj = SingleSubject.create(); | ||
|
||
SingleSubject<Integer> fallback = SingleSubject.create(); | ||
|
||
TestObserver<Integer> to = subj.timeout(1, TimeUnit.SECONDS, sch, fallback) | ||
.test(); | ||
|
||
assertFalse(fallback.hasObservers()); | ||
|
||
sch.advanceTimeBy(1, TimeUnit.SECONDS); | ||
|
||
assertFalse(subj.hasObservers()); | ||
assertTrue(fallback.hasObservers()); | ||
|
||
to.cancel(); | ||
|
||
assertFalse(fallback.hasObservers()); | ||
} | ||
|
||
@Test | ||
public void normalSuccessDoesntDisposeMain() { | ||
final int[] calls = { 0 }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I know you often use arrays for that, but |
||
|
||
Single.just(1) | ||
.doOnDispose(new Action() { | ||
@Override | ||
public void run() throws Exception { | ||
calls[0]++; | ||
} | ||
}) | ||
.timeout(1, TimeUnit.DAYS) | ||
.test() | ||
.assertResult(1); | ||
|
||
assertEquals(0, calls[0]); | ||
} | ||
|
||
@Test | ||
public void successTimeoutRace() { | ||
for (int i = 0; i < 1000; i++) { | ||
final SingleSubject<Integer> subj = SingleSubject.create(); | ||
SingleSubject<Integer> fallback = SingleSubject.create(); | ||
|
||
final TestScheduler sch = new TestScheduler(); | ||
|
||
TestObserver<Integer> to = subj.timeout(1, TimeUnit.MILLISECONDS, sch, fallback).test(); | ||
|
||
Runnable r1 = new Runnable() { | ||
@Override | ||
public void run() { | ||
subj.onSuccess(1); | ||
} | ||
}; | ||
|
||
Runnable r2 = new Runnable() { | ||
@Override | ||
public void run() { | ||
sch.advanceTimeBy(1, TimeUnit.MILLISECONDS); | ||
} | ||
}; | ||
|
||
TestHelper.race(r1, r2); | ||
|
||
if (!fallback.hasObservers()) { | ||
to.assertResult(1); | ||
} else { | ||
to.assertEmpty(); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void errorTimeoutRace() { | ||
final TestException ex = new TestException(); | ||
List<Throwable> errors = TestHelper.trackPluginErrors(); | ||
try { | ||
|
||
for (int i = 0; i < 1000; i++) { | ||
final SingleSubject<Integer> subj = SingleSubject.create(); | ||
SingleSubject<Integer> fallback = SingleSubject.create(); | ||
|
||
final TestScheduler sch = new TestScheduler(); | ||
|
||
TestObserver<Integer> to = subj.timeout(1, TimeUnit.MILLISECONDS, sch, fallback).test(); | ||
|
||
Runnable r1 = new Runnable() { | ||
@Override | ||
public void run() { | ||
subj.onError(ex); | ||
} | ||
}; | ||
|
||
Runnable r2 = new Runnable() { | ||
@Override | ||
public void run() { | ||
sch.advanceTimeBy(1, TimeUnit.MILLISECONDS); | ||
} | ||
}; | ||
|
||
TestHelper.race(r1, r2); | ||
|
||
if (!fallback.hasObservers()) { | ||
to.assertFailure(TestException.class); | ||
} else { | ||
to.assertEmpty(); | ||
} | ||
if (!errors.isEmpty()) { | ||
TestHelper.assertUndeliverable(errors, 0, TestException.class); | ||
} | ||
} | ||
} finally { | ||
RxJavaPlugins.reset(); | ||
} | ||
} | ||
} |
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.
Hmm, are you trying to avoid memory leak here? Reference to other is still held as a final field in
SingleTimeout
I mean, final reference to
other
inTimeoutMainObserver
will make code slightly more readableThere 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.
The outer class has to reference other because of multiple subscribers. The inner observer has no use to the other once it has switched to it. There was an issue that some time ago that other was some cached resource that didn't go away when the user wanted it. This will somewhat help in that situation.