Skip to content
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: RxJavaPlugins unwrapRunnable #5734

Merged
merged 24 commits into from
Dec 4, 2017
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions src/main/java/io/reactivex/Scheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

package io.reactivex;

import java.util.concurrent.TimeUnit;

import io.reactivex.annotations.*;
import io.reactivex.disposables.Disposable;
import io.reactivex.exceptions.Exceptions;
Expand All @@ -23,6 +21,9 @@
import io.reactivex.internal.schedulers.*;
import io.reactivex.internal.util.ExceptionHelper;
import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.schedulers.SchedulerRunnableIntrospection;

import java.util.concurrent.TimeUnit;

/**
* A {@code Scheduler} is an object that specifies an API for scheduling
Expand Down Expand Up @@ -197,7 +198,7 @@ public Disposable schedulePeriodicallyDirect(@NonNull Runnable run, long initial
* <p>
* Limit the amount concurrency two at a time without creating a new fix
* size thread pool:
*
*
* <pre>
* Scheduler limitScheduler = Schedulers.computation().when(workers -&gt; {
* // use merge max concurrent to limit the number of concurrent
Expand All @@ -215,20 +216,20 @@ public Disposable schedulePeriodicallyDirect(@NonNull Runnable run, long initial
* {@link Flowable#zip(org.reactivestreams.Publisher, org.reactivestreams.Publisher, io.reactivex.functions.BiFunction)} where
* subscribing to the first {@link Flowable} could deadlock the
* subscription to the second.
*
*
* <pre>
* Scheduler limitScheduler = Schedulers.computation().when(workers -&gt; {
* // use merge max concurrent to limit the number of concurrent
* // Flowables two at a time
* return Completable.merge(Flowable.merge(workers, 2));
* });
* </pre>
*
*
* Slowing down the rate to no more than than 1 a second. This suffers from
* the same problem as the one above I could find an {@link Flowable}
* operator that limits the rate without dropping the values (aka leaky
* bucket algorithm).
*
*
* <pre>
* Scheduler slowScheduler = Schedulers.computation().when(workers -&gt; {
* // use concatenate to make each worker happen one at a time.
Expand All @@ -238,7 +239,7 @@ public Disposable schedulePeriodicallyDirect(@NonNull Runnable run, long initial
* }));
* });
* </pre>
*
*
* <p>History: 2.0.1 - experimental
* @param <S> a Scheduler and a Subscription
* @param combine the function that takes a two-level nested Flowable sequence of a Completable and returns
Expand Down Expand Up @@ -347,7 +348,7 @@ public long now(@NonNull TimeUnit unit) {
* Holds state and logic to calculate when the next delayed invocation
* of this task has to happen (accounting for clock drifts).
*/
final class PeriodicTask implements Runnable {
final class PeriodicTask implements Runnable, SchedulerRunnableIntrospection {
@NonNull
final Runnable decoratedRun;
@NonNull
Expand Down Expand Up @@ -393,11 +394,16 @@ public void run() {
sd.replace(schedule(this, delay, TimeUnit.NANOSECONDS));
}
}

@Override
public Runnable getWrappedRunnable() {
return this.decoratedRun;
}
}
}

static class PeriodicDirectTask
implements Runnable, Disposable {
implements Disposable, Runnable, SchedulerRunnableIntrospection {
final Runnable run;
@NonNull
final Worker worker;
Expand Down Expand Up @@ -432,9 +438,14 @@ public void dispose() {
public boolean isDisposed() {
return disposed;
}

@Override
public Runnable getWrappedRunnable() {
return run;
}
}

static final class DisposeTask implements Runnable, Disposable {
static final class DisposeTask implements Disposable, Runnable, SchedulerRunnableIntrospection {
final Runnable decoratedRun;
final Worker w;

Expand Down Expand Up @@ -469,5 +480,10 @@ public void dispose() {
public boolean isDisposed() {
return w.isDisposed();
}

@Override
public Runnable getWrappedRunnable() {
return this.decoratedRun;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 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.schedulers;

import io.reactivex.Scheduler;
import io.reactivex.annotations.*;
import io.reactivex.plugins.RxJavaPlugins;

/**
* Marker interface to indicate wrapped action inside internal scheduler's task.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a marker interface if it declares method(s) that can be consumed :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True :)

*
* Inside of the {@link RxJavaPlugins#onSchedule(Runnable)}, you can unwrap runnable from internal wrappers like
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe change to something like:

You can check if runnable implements this interface and unwrap original runnable?

* {@link Scheduler.DisposeTask}.
*
* @since 2.1.7 - experimental
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add "see also" with references to RxJavaPlugins.setScheduleHandler() and RxJavaPlugins. getScheduleHandler()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about something like this?

/**
 * Interface to wrap an action inside internal scheduler's task.
 *
 * You can check if runnable implements this interface and unwrap original runnable.
 * For example inside of the {@link RxJavaPlugins#setScheduleHandler(Function)}
 *
 * @since 2.1.7 - experimental
 */

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sgtm

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I corrected it.

*/
@Experimental
public interface SchedulerRunnableIntrospection {

/**
* Returns the wrapped action.
*
* @return the wrapped action, may be null.
*/
@Nullable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually is @NotNull now since it's not a generic Object based API, schedulers don't expect that Runnable can be null (I'm adding PR that'll enforce that)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I corrected it

Runnable getWrappedRunnable();
}
62 changes: 62 additions & 0 deletions src/test/java/io/reactivex/schedulers/SchedulerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,66 @@ public void customScheduleDirectDisposed() {

assertTrue(d.isDisposed());
}

@Test
public void unwrapDefaultPeriodicTask() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we move these tests to AbstractSchedulerTests @akarnokd?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I'm waiting for your decision :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're waiting for @akarnokd to answer I'd recommend you to try to move this code to AbstractSchedulerTests and make sure to use getScheduler() instead of TestScheduler, that way we check that all schedulers properly support this introspection

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we want to check that all schedulers support this API?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can get complicated and this is an optional introspection. If @lukaszguz wants to expand it, then sure.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but I'd encourage @lukaszguz to try that and ask for help if needed, would be great to provide as robust API experience as possible

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's enough for now? I can always add a new PR with additional tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with this as is. We can always expand the feature set later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my first time when I'm doing PR to your library. Thanks to your tips, I know more or less what your requirements are and I can write better code. I think the better option would be to leave those tests in SchedulerTest because they are showed how Scheduler works but also good idea is to add tests in AbstractSchedulerTests which they will guarantee compatibility with all rx's schedulers

TestScheduler scheduler = new TestScheduler();

Runnable runnable = new Runnable() {
@Override
public void run() {
}
};
SchedulerRunnableIntrospection wrapper = (SchedulerRunnableIntrospection) scheduler.schedulePeriodicallyDirect(runnable, 100, 100, TimeUnit.MILLISECONDS);

assertEquals(runnable, wrapper.getWrappedRunnable());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertSame?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed it.

}

@Test
public void unwrapScheduleDirectTask() {
TestScheduler scheduler = new TestScheduler();

Runnable runnable = new Runnable() {
@Override
public void run() {
}
};
SchedulerRunnableIntrospection wrapper = (SchedulerRunnableIntrospection) scheduler.scheduleDirect(runnable, 100, TimeUnit.MILLISECONDS);
assertEquals(runnable, wrapper.getWrappedRunnable());
}

@Test
public void unwrapWorkerPeriodicTask() {
final Runnable runnable = new Runnable() {
@Override
public void run() {
}
};

Scheduler scheduler = new Scheduler() {
@Override
public Worker createWorker() {
return new Worker() {
@Override
public Disposable schedule(Runnable run, long delay, TimeUnit unit) {
SchedulerRunnableIntrospection outerWrapper = (SchedulerRunnableIntrospection) run;
SchedulerRunnableIntrospection innerWrapper = (SchedulerRunnableIntrospection) outerWrapper.getWrappedRunnable();
assertEquals(runnable, innerWrapper.getWrappedRunnable());
return (Disposable) innerWrapper;
}

@Override
public void dispose() {
}

@Override
public boolean isDisposed() {
return false;
}
};
}
};

scheduler.schedulePeriodicallyDirect(runnable, 100, 100, TimeUnit.MILLISECONDS);
}
}