-
Notifications
You must be signed in to change notification settings - Fork 529
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 Ref.lens constructor #827
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
272 changes: 272 additions & 0 deletions
272
core/shared/src/test/scala/cats/effect/concurrent/LensRefTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,272 @@ | ||
/* | ||
* Copyright (c) 2017-2019 The Typelevel Cats-effect Project Developers | ||
* | ||
* 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 cats.effect.concurrent | ||
|
||
import cats.data.State | ||
import cats.effect.IO | ||
import org.scalatest.Succeeded | ||
import org.scalatest.compatible.Assertion | ||
import org.scalatest.funsuite.AsyncFunSuite | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
import scala.concurrent.Future | ||
|
||
class LensRefTests extends AsyncFunSuite with Matchers { | ||
|
||
private def run(t: IO[Unit]): Future[Assertion] = t.as(Succeeded).unsafeToFuture() | ||
|
||
case class Foo(bar: Integer, baz: Integer) | ||
|
||
object Foo { | ||
def get(foo: Foo): Integer = foo.bar | ||
|
||
def set(foo: Foo)(bar: Integer): Foo = foo.copy(bar = bar) | ||
} | ||
|
||
test("get - returns B") { | ||
val op = for { | ||
refA <- Ref[IO].of(Foo(0, -1)) | ||
refB = Ref.lens[IO, Foo, Integer](refA)(Foo.get, Foo.set) | ||
result <- refB.get | ||
} yield result | ||
|
||
run(op.map(_ shouldEqual 0)) | ||
} | ||
|
||
test("set - modifies underlying Ref") { | ||
val op = for { | ||
refA <- Ref[IO].of(Foo(0, -1)) | ||
refB = Ref.lens[IO, Foo, Integer](refA)(Foo.get, Foo.set) | ||
_ <- refB.set(1) | ||
result <- refA.get | ||
} yield result | ||
|
||
run(op.map(_ shouldEqual Foo(1, -1))) | ||
} | ||
|
||
test("getAndSet - modifies underlying Ref and returns previous value") { | ||
val op = for { | ||
refA <- Ref[IO].of(Foo(0, -1)) | ||
refB = Ref.lens[IO, Foo, Integer](refA)(Foo.get, Foo.set) | ||
oldValue <- refB.getAndSet(1) | ||
a <- refA.get | ||
} yield (oldValue, a) | ||
|
||
run(op.map { | ||
case (oldValue, a) => | ||
oldValue shouldBe 0 | ||
a shouldEqual Foo(1, -1) | ||
}) | ||
} | ||
|
||
test("update - modifies underlying Ref") { | ||
val op = for { | ||
refA <- Ref[IO].of(Foo(0, -1)) | ||
refB = Ref.lens[IO, Foo, Integer](refA)(Foo.get, Foo.set) | ||
_ <- refB.update(_ + 1) | ||
a <- refA.get | ||
} yield a | ||
|
||
run(op.map(_ shouldBe Foo(1, -1))) | ||
} | ||
|
||
test("modify - modifies underlying Ref and returns a value") { | ||
val op = for { | ||
refA <- Ref[IO].of(Foo(0, -1)) | ||
refB = Ref.lens[IO, Foo, Integer](refA)(Foo.get, Foo.set) | ||
result <- refB.modify(bar => (bar + 1, 10)) | ||
a <- refA.get | ||
} yield (result, a) | ||
|
||
run(op.map { | ||
case (result, a) => | ||
result shouldBe 10 | ||
a shouldEqual Foo(1, -1) | ||
}) | ||
} | ||
|
||
test("tryUpdate - successfully modifies underlying Ref") { | ||
val op = for { | ||
refA <- Ref[IO].of(Foo(0, -1)) | ||
refB = Ref.lens[IO, Foo, Integer](refA)(Foo.get, Foo.set) | ||
result <- refB.tryUpdate(_ + 1) | ||
a <- refA.get | ||
} yield (result, a) | ||
|
||
run(op.map { | ||
case (result, a) => | ||
result shouldBe true | ||
a shouldBe Foo(1, -1) | ||
}) | ||
} | ||
|
||
test("tryUpdate - fails to modify original value if it's already been modified concurrently") { | ||
val updateRefUnsafely: Ref[IO, Integer] => Unit = _.set(5).unsafeRunSync() | ||
|
||
val op = for { | ||
refA <- Ref[IO].of(Foo(0, -1)) | ||
refB = Ref.lens[IO, Foo, Integer](refA)(Foo.get, Foo.set) | ||
result <- refB.tryUpdate { currentValue => | ||
updateRefUnsafely(refB) | ||
currentValue + 1 | ||
} | ||
a <- refA.get | ||
} yield (result, a) | ||
|
||
run(op.map { | ||
case (result, a) => | ||
result shouldBe false | ||
a shouldBe Foo(5, -1) | ||
}) | ||
} | ||
|
||
test("tryModify - successfully modifies underlying Ref") { | ||
val op = for { | ||
refA <- Ref[IO].of(Foo(0, -1)) | ||
refB = Ref.lens[IO, Foo, Integer](refA)(Foo.get, Foo.set) | ||
result <- refB.tryModify(bar => (bar + 1, "A")) | ||
a <- refA.get | ||
} yield (result, a) | ||
|
||
run(op.map { | ||
case (result, a) => | ||
result shouldBe Some("A") | ||
a shouldBe Foo(1, -1) | ||
}) | ||
} | ||
|
||
test("tryModify - fails to modify original value if it's already been modified concurrently") { | ||
val updateRefUnsafely: Ref[IO, Integer] => Unit = _.set(5).unsafeRunSync() | ||
|
||
val op = for { | ||
refA <- Ref[IO].of(Foo(0, -1)) | ||
refB = Ref.lens[IO, Foo, Integer](refA)(Foo.get, Foo.set) | ||
result <- refB.tryModify { currentValue => | ||
updateRefUnsafely(refB) | ||
(currentValue + 1, 10) | ||
} | ||
a <- refA.get | ||
} yield (result, a) | ||
|
||
run(op.map { | ||
case (result, a) => | ||
result shouldBe None | ||
a shouldBe Foo(5, -1) | ||
}) | ||
} | ||
|
||
test("tryModifyState - successfully modifies underlying Ref") { | ||
val op = for { | ||
refA <- Ref[IO].of(Foo(0, -1)) | ||
refB = Ref.lens[IO, Foo, Integer](refA)(Foo.get, Foo.set) | ||
result <- refB.tryModifyState(State.apply(x => (x + 1, "A"))) | ||
a <- refA.get | ||
} yield (result, a) | ||
|
||
run(op.map { | ||
case (result, a) => | ||
result shouldBe Some("A") | ||
a shouldBe Foo(1, -1) | ||
}) | ||
} | ||
|
||
test("modifyState - successfully modifies underlying Ref") { | ||
val op = for { | ||
refA <- Ref[IO].of(Foo(0, -1)) | ||
refB = Ref.lens[IO, Foo, Integer](refA)(Foo.get, Foo.set) | ||
result <- refB.modifyState(State.apply(x => (x + 1, "A"))) | ||
a <- refA.get | ||
} yield (result, a) | ||
|
||
run(op.map { | ||
case (result, a) => | ||
result shouldBe "A" | ||
a shouldBe Foo(1, -1) | ||
}) | ||
} | ||
|
||
test("access - successfully modifies underlying Ref") { | ||
val op = for { | ||
refA <- Ref[IO].of(Foo(0, -1)) | ||
refB = Ref.lens[IO, Foo, Integer](refA)(Foo.get, Foo.set) | ||
valueAndSetter <- refB.access | ||
(value, setter) = valueAndSetter | ||
success <- setter(value + 1) | ||
a <- refA.get | ||
} yield (success, a) | ||
run(op.map { | ||
case (success, a) => | ||
success shouldBe true | ||
a shouldBe Foo(1, -1) | ||
}.void) | ||
} | ||
|
||
test("access - successfully modifies underlying Ref after A is modified without affecting B") { | ||
val op = for { | ||
refA <- Ref[IO].of(Foo(0, -1)) | ||
refB = Ref.lens[IO, Foo, Integer](refA)(Foo.get, Foo.set) | ||
valueAndSetter <- refB.access | ||
(value, setter) = valueAndSetter | ||
_ <- refA.update(_.copy(baz = -2)) | ||
success <- setter(value + 1) | ||
a <- refA.get | ||
} yield (success, a) | ||
run(op.map { | ||
case (success, a) => | ||
success shouldBe true | ||
a shouldBe Foo(1, -2) | ||
}.void) | ||
} | ||
|
||
test("access - setter fails to modify underlying Ref if value is modified before setter is called") { | ||
val op = for { | ||
refA <- Ref[IO].of(Foo(0, -1)) | ||
refB = Ref.lens[IO, Foo, Integer](refA)(Foo.get, Foo.set) | ||
valueAndSetter <- refB.access | ||
(value, setter) = valueAndSetter | ||
_ <- refA.set(Foo(5, -1)) | ||
success <- setter(value + 1) | ||
a <- refA.get | ||
} yield (success, a) | ||
|
||
run(op.map { | ||
case (success, result) => | ||
success shouldBe false | ||
result shouldBe Foo(5, -1) | ||
}.void) | ||
} | ||
|
||
test("access - setter fails the second time") { | ||
val op = for { | ||
refA <- Ref[IO].of(Foo(0, -1)) | ||
refB = Ref.lens[IO, Foo, Integer](refA)(Foo.get, Foo.set) | ||
valueAndSetter <- refB.access | ||
(_, setter) = valueAndSetter | ||
result1 <- setter(1) | ||
result2 <- setter(2) | ||
a <- refA.get | ||
} yield (result1, result2, a) | ||
|
||
run(op.map { | ||
case (result1, result2, a) => | ||
result1 shouldBe true | ||
result2 shouldBe false | ||
a shouldBe Foo(1, -1) | ||
}.void) | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should briefly mention that updates in the parent that don't change the child won't disable the setter in the child's
access
.