ReactiveX / rxdart

The Reactive Extensions for Dart
http://reactivex.io
Apache License 2.0
3.36k stars 271 forks source link

Stream tests lack time manipulation #695

Open feinstein opened 1 year ago

feinstein commented 1 year ago

RxJava has the ability to inject a scheduler to some streams, this way we can manipulate time in tests. If I have an interval of 5 minutes in my code, I can't test it without a very long test, or by changing my interval in the test to some milliseconds, which won't simulate 100% what's happening in the app.

The same way that Flutter tests allows us to manipulate time for testing animations, without having to actually wait for them, rxdart should give us similar controls, as other rx libraries already do.

frankpepermans commented 1 year ago

You can actually use fake time already, most of the tests actually use fake async.

On my phone atm, but please take a look at the runZoned api in Dart, for example, which allows you to override timers.

feinstein commented 1 year ago

I took a look at the source code for some streams and I can see it uses very small durations, so I assumed there was no way to inject a fake time.

frankpepermans commented 1 year ago

Maybe cause they're already quite old then, it's been a while since I've contributed to this lib actually.

I'd try a simple case with a Zone and some Rx, if it doesn't work we could always update the inner parts, basically it should rely on the wrapping zones for timings

hoc081098 commented 1 year ago

I think we can use clock package in rxdart, but some built-in Stream constructors don't receive any schduler, such as Stream.periodic

Sent from my 2201117TG using FastHub

feinstein commented 1 year ago

I think tests should be easy to setup, zones can get cumbersome to manipulate. The clock package should be better IMO.

feinstein commented 1 year ago

Also, I don't think we should reinvent the wheel, studying how RxJava solves this should be better, as the mechanism probably covers lots of use cases for asynchronous testing of reactive streams.