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

Add a memoizedAcquire method to Resource #3890

Open
wants to merge 3 commits into
base: series/3.x
Choose a base branch
from

Conversation

Daenyth
Copy link
Contributor

@Daenyth Daenyth commented Nov 8, 2023

Ref #3513

I'm not sure how to unit test this exactly, I'm open to guidance or commits for that

Comment on lines 824 to 829
Concurrent[Resource[F, *]].memoize[A](self).map {
case Resource.Eval(fa) => fa
case unexpected =>
throw new IllegalStateException(
s"Memoized Resource is not Resource.Eval: $unexpected")
}
Copy link
Member

Choose a reason for hiding this comment

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

Actually, we can avoid this silliness if we go the other way: put the juicy implementation in here, and then memoize can delegate to memoizedAcquire and wrap it up in Resource.eval.

Copy link
Member

Choose a reason for hiding this comment

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

Also then, the existing memoize tests seem like sufficient coverage to me.

@armanbilge armanbilge linked an issue Nov 8, 2023 that may be closed by this pull request
@armanbilge armanbilge changed the title Add a memoizedAcquire method to Resource Add a memoizedAcquire method to Resource Nov 8, 2023
@SystemFw
Copy link
Contributor

To test this I would write to Refs in your acquire and use, and assert on the state of those refs to check that it's behaving as you expect. I wouldnt worry about concurrent testing since memoize is pulling most of the weight

@armanbilge
Copy link
Member

armanbilge commented Nov 14, 2023

I don't think we need to add any new tests specifically because of this PR, unless there are existing gaps in the existing memoize tests.

To be clear, what I'm proposing is that we refactor so it looks like this:

def memoizedAcquire(ra: Resource[F, A]): Resource[F, F[A]] =
  /* move existing memoize implementation here */

def memoize(ra: Resource[F, A]): Resource[F, Resource[F, A]] = 
  memoizedAcquire.map(Resource.eval(_))

Then all the existing memoize tests can just be in terms of memoizedAcquire.

@SystemFw
Copy link
Contributor

Yeah fair enough, with that refactor I would agree that we don't need any new tests

@Daenyth
Copy link
Contributor Author

Daenyth commented Nov 14, 2023

I'm a huge fan of not having to add new tests. I will get to that refactor when/as I have time but if anyone wants to beat me to it, the branch is open for maintainer commits ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add specialized signature of memoize to Resource
3 participants