Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@adamgfraser
Copy link
Contributor

Implements generators for ZIO and its various type aliases.

I took a somewhat different approach from the existing generators in the test suites. I think we need to make a distinction between generators that ZIO uses internally to test its own correctness and generators that users of ZIO should use. I would argue that for a user, if a ZIO produces an A and doesn't perform any actual effects then it should be indistinguishable to a user how that A was produced (e.g. if it was there all along or created through a complex chain or transformations). If it is possible for a user to distinguish those then that is an internal error in ZIO that needs to be addressed. If this is correct then to generate ZIO values we only need to be concerned with producing the right results and not in producing different computations to produce those results. This simplifies the implementation considerably and will also make these generators faster and shrink better for users.

Internally, ZIO can use more complex generators to verify that various different sequences of computations produce the expected results, but that shouldn't be exposed to users. At least that is my current thinking. Interested if others have different points of view.

Resolves #1653.

@adamgfraser adamgfraser requested a review from jdegoes September 13, 2019 20:19
@iravid
Copy link
Member

iravid commented Sep 14, 2019

@adamgfraser

I would argue that for a user, if a ZIO produces an A and doesn't perform any actual effects then it should be indistinguishable to a user how that A was produced (e.g. if it was there all along or created through a complex chain or transformations).

It actually matters quite a bit when testing concurrent code. As a user of the library, I want to be in control of whether the generated IO contains concurrency and interruption, so I can test concurrent code against that. E.g., consider Stream's combinators- it'd be a shame if we could not test them using IO values that generate concurrency.

@iravid
Copy link
Member

iravid commented Sep 14, 2019

And, of course, we can exchange "concurrency" which I wrote above with any other feature of ZIO - interruption, defects, sleeps, etc.

@adamgfraser
Copy link
Contributor Author

@iravid I think maybe there is a synthesis between what we are saying.

When I look at the existing generators of effects. I see us building up a trees of sub-effects that are essentially no-ops (e.g. race against never, map with identity, etc...). Those are the kind of steps that I'm not sure add value to say testing Stream. On the other hand, I think you are 100% right that it would be good if we had generators for things like die effects, effects that sleep for an arbitrary amount of time, or effects that never terminate and then users could compose those together.

Does that make sense?

@adamgfraser
Copy link
Contributor Author

On interruption in particular there is a difference where a may not be interruptible but a.flatMap(ZIO.succeed) could be interrupted so we do need something for that.

/**
* A generator of throwables.
*/
final def throwable: Gen[Random, Throwable] =
Copy link
Member

Choose a reason for hiding this comment

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

This can be val.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes.

@jdegoes
Copy link
Member

jdegoes commented Sep 15, 2019

What about a combinator based approach:

  • successes (ZIO.succeed)
  • failures (ZIO.fail)
  • chained(zio) - effect chained with itself n times (Sized)
  • died (ZIO.die)
  • concurrent(zio) - random no-op concurrency applied (race, etc.)
  • parallel(zio) - random no-op parallelism applied (race, etc.)
  • etc.

@adamgfraser
Copy link
Contributor Author

I like that a lot.

@iravid
Copy link
Member

iravid commented Sep 15, 2019

@adamgfraser Yes, completely agree!

@jdegoes @adamgfraser Yes - this is what I want! 😍

@adamgfraser
Copy link
Contributor Author

This is ready for another review.

Copy link
Member

@jdegoes jdegoes left a comment

Choose a reason for hiding this comment

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

Looks great to me, @adamgfraser! Will let @iravid do final signoff here.

Copy link
Member

@iravid iravid left a comment

Choose a reason for hiding this comment

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

Looks great. Nice work @adamgfraser!

@iravid iravid merged commit 8178151 into zio:master Sep 18, 2019
@adamgfraser adamgfraser deleted the zio branch September 20, 2019 19:02
fsvehla pushed a commit to fsvehla/zio that referenced this pull request Sep 21, 2019
* implement generators for effects

* fix issue with Scala.js

* add combinators
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ZIO Test: Add support for effect generators

3 participants