-
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
[0.16.x] Possible bug in Sample(Observable) operator #1045
Comments
Yes, this is a bug. Together with @headinthebox I also just found this out. We found that the following Java code prints out infinite amounts of Observable.concat(Observable.from(1, 2, 3), Observable.never()).sample(1L, TimeUnit.SECONDS).subscribe(System.out::println); The .NET variant works correctly:
and only prints |
The marble diagram describes correctly the current behavior, but seems to contradict the text description stating that a sample is taken "whenever the specified sampler Observable emits an item or completes." My current use case requires the behavior as defined in letter and is more intuitive to me. If the current one is the expected one, I guess the javadoc should be altered and a new operator/override could be provided. I haven't checked the rxnet behavior and the introtorx sample [0] only shows the case where sampler frequency < src frequency. [0] http://www.introtorx.com/content/v1.0.10621.0/13_TimeShiftedSequences.html#Sample |
@rvanheest Good to know you are looking into it guys ! |
If this is is indeed considered as a bug, I thing the culprit lays at https://github.com/Netflix/RxJava/blob/master/rxjava-core/src/main/java/rx/operators/OperationSample.java#L197 For some reason the code especially guards against sending the same source value twice using the |
It was a bit odd at the time, but I think L197 does this value-once thing correctly. L85 does not clear the value flag hence it will repeat the last value indefinitely. |
@akarnokd @DavidMGross Thanks for your quick answers and reaction. Still, the emit-value-from-source-whenever-sampler-emits behavior could also prove useful, would such a variant be considerable for inclusion ? |
We can get this fixed fairly easily in the next release, it should not be emitting anything (repeating the last value) if nothing has been emitted in a given time window from the source. |
This should be fixed as of 0.18.2 based on some basic testing I did. Can someone confirm? |
Yes, I can confirm that this bug is fixed now. When running the same program as shown above, I now get 3 only printed once, instead of over and over again. Also while running other programs that used this method, I noticed that the behavior is now correct. Thanks for fixing it! public class RxSampleTest {
public static void main(String[] args) throws IOException {
Observable.concat(Observable.from(1, 2, 3), Observable.never()).sample(1L, TimeUnit.SECONDS).subscribe(System.out::println);
System.in.read();
}
} |
Thanks for confirming @rvanheest |
The Observable.sample(Observable) operator behaves non-intuitively (at least to me):
Here is the javadoc description:
"Return an Observable that emits the results of sampling the items emitted by the source Observable whenever the specified sampler Observable emits an item or completes."
Which I understand as:
The resulting Observable emits the current value of
src
for each value emitted by sampler, repeatingsrc
last value if required (in casesampler
frequency >src
frequency).But the current behavior as of 0.16.1 is:
When sampling a source Observable
src
using asampler
Observable, the resulting Observableres
always emits only once the last value ofsrc
, even ifsampler
's frequency is superior tosrc
's .Code example:
Actual output:
Expected output:
Is my understanding of the javadoc wrong orcan we concider the current behavior as a bug ? Was it already reported and fixed in 0.17 ?
Thanks for your hard work!
The text was updated successfully, but these errors were encountered: