-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Improve performance of NewThreadWorker, disable search for setRemoveOnCancelPolicy() on Android API < 21 #3121
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
Merged
akarnokd
merged 1 commit into
ReactiveX:1.x
from
artem-zinnatullin:android-setRemoveOnCancelPolicy
Aug 1, 2015
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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
65 changes: 65 additions & 0 deletions
65
src/test/java/rx/internal/schedulers/NewThreadWorkerTest.java
This file contains hidden or 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,65 @@ | ||
package rx.internal.schedulers; | ||
|
||
import org.junit.Test; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Method; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.ScheduledThreadPoolExecutor; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import static java.lang.reflect.Modifier.FINAL; | ||
import static org.junit.Assert.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
public class NewThreadWorkerTest { | ||
|
||
@Test | ||
public void findSetRemoveOnCancelPolicyMethodShouldFindMethod() { | ||
ScheduledExecutorService executor = spy(new ScheduledThreadPoolExecutor(1)); | ||
Method setRemoveOnCancelPolicyMethod = NewThreadWorker.findSetRemoveOnCancelPolicyMethod(executor); | ||
|
||
assertNotNull(setRemoveOnCancelPolicyMethod); | ||
assertEquals("setRemoveOnCancelPolicy", setRemoveOnCancelPolicyMethod.getName()); | ||
assertEquals(1, setRemoveOnCancelPolicyMethod.getParameterTypes().length); | ||
assertEquals(Boolean.TYPE, setRemoveOnCancelPolicyMethod.getParameterTypes()[0]); | ||
verifyZeroInteractions(executor); | ||
} | ||
|
||
@Test | ||
public void findSetRemoveOnCancelPolicyMethodShouldNotFindMethod() { | ||
ScheduledExecutorService executor = mock(ScheduledExecutorService.class); | ||
|
||
Method setRemoveOnCancelPolicyMethod = NewThreadWorker.findSetRemoveOnCancelPolicyMethod(executor); | ||
assertNull(setRemoveOnCancelPolicyMethod); | ||
verifyZeroInteractions(executor); | ||
} | ||
|
||
private static abstract class ScheduledExecutorServiceWithSetRemoveOnCancelPolicy implements ScheduledExecutorService { | ||
// Just declaration of required method to allow run tests on JDK 6 | ||
public void setRemoveOnCancelPolicy(@SuppressWarnings("UnusedParameters") boolean value) {} | ||
} | ||
|
||
@Test | ||
public void tryEnableCancelPolicyShouldInvokeMethodOnExecutor() { | ||
ScheduledExecutorServiceWithSetRemoveOnCancelPolicy executor | ||
= mock(ScheduledExecutorServiceWithSetRemoveOnCancelPolicy.class); | ||
|
||
boolean result = NewThreadWorker.tryEnableCancelPolicy(executor); | ||
|
||
assertTrue(result); | ||
verify(executor).setRemoveOnCancelPolicy(true); | ||
verifyNoMoreInteractions(executor); | ||
} | ||
|
||
@Test | ||
public void tryEnableCancelPolicyShouldNotInvokeMethodOnExecutor() { | ||
// This executor does not have setRemoveOnCancelPolicy method | ||
ScheduledExecutorService executor = mock(ScheduledExecutorService.class); | ||
|
||
boolean result = NewThreadWorker.tryEnableCancelPolicy(executor); | ||
|
||
assertFalse(result); | ||
verifyZeroInteractions(executor); | ||
} | ||
} |
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.
This will fail on a Java 6 build but I'm not sure if people actually build RxJava from source on Java 6. What I would do is subclass ScheduledThreadPoolExecutor and add the setRemoveOnCancelPolicy(boolean) method. For Java 7+, this will count as a simple override.
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.
Nice workaround!