Created
January 9, 2020 05:13
-
-
Save nnao45/58292601ddd398a071991a26c8ca6ed7 to your computer and use it in GitHub Desktop.
this program is wrong?
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
package saga.sample01 | |
import cats.effect.{Concurrent, ContextShift, IO} | |
import com.vladkopanev.cats.saga.Saga._ | |
import scala.concurrent.ExecutionContext | |
object Main extends App { | |
implicit val ec: ContextShift[IO] = IO.contextShift(ExecutionContext.global) | |
var pay = 0d | |
var point = 0d | |
var order = 0 | |
val err = new Exception("error") | |
def reset: IO[Unit] = { | |
pay = 0d | |
point = 0d | |
order = 0 | |
IO.unit | |
} | |
def collectPayments(v1: Double, count: Int, isError: Boolean): IO[Unit] = if (isError) IO.raiseError(err) else IO(pay += v1 * count) | |
def refundPayments(v1: Double, count: Int): IO[Unit] = { | |
pay = 0d | |
IO.unit | |
} | |
def assignLoyaltyPoints(v1: Double, count: Int, isError: Boolean): IO[Unit] = if (isError) IO.raiseError(err) else IO(point += v1 * count) | |
def cancelLoyaltyPoints(v1: Double, count: Int): IO[Unit] = { | |
point = 0d | |
IO.unit | |
} | |
def openOrder(v1 : Int): IO[Unit] = IO(order += v1) | |
def reopenOrder(v1 : Int): IO[Unit] = { | |
order = 0 | |
IO.unit | |
} | |
def closeOrder(v1 : Int, isError: Boolean): IO[Unit] = if (isError) IO.raiseError(err) else { | |
order = 0 | |
IO.unit | |
} | |
def orderSaga1(): IO[Unit] = | |
(for { | |
_ <- reset.noCompensate | |
_ <- openOrder(1) compensate closeOrder(1, false) | |
_ <- collectPayments(2d, 2, false) compensate refundPayments(2d, 2) | |
_ <- assignLoyaltyPoints(1d, 1, false) compensate cancelLoyaltyPoints(1d, 1) | |
_ <- closeOrder(1, false) compensate reopenOrder(1) | |
} yield ()).transact(implicitly[Concurrent[IO]]).handleErrorWith(err => IO(println(err))) | |
def orderSaga2(): IO[Unit] = | |
(for { | |
_ <- reset.noCompensate | |
_ <- openOrder(1) compensate closeOrder(1, false) | |
_ <- collectPayments(2d, 2, true) compensate refundPayments(2d, 2) | |
_ <- assignLoyaltyPoints(1d, 1, false) compensate cancelLoyaltyPoints(1d, 1) | |
_ <- closeOrder(1, false) compensate reopenOrder(1) | |
} yield ()).transact(implicitly[Concurrent[IO]]).handleErrorWith(err => IO(println(err))) | |
def orderSaga3(): IO[Unit] = | |
(for { | |
_ <- reset.noCompensate | |
_ <- openOrder(1) compensate closeOrder(1, false) | |
_ <- collectPayments(2d, 2, false) compensate refundPayments(2d, 2) | |
_ <- assignLoyaltyPoints(1d, 1, true) compensate cancelLoyaltyPoints(1d, 1) | |
_ <- closeOrder(1, false) compensate reopenOrder(1) | |
} yield ()).transact(implicitly[Concurrent[IO]]).handleErrorWith(err => IO(println(err))) | |
orderSaga1().unsafeRunSync() | |
assert(pay == 4d) | |
assert(point == 1d) | |
assert(order == 0) | |
orderSaga2().unsafeRunSync() | |
assert(pay == 0d) | |
assert(point == 0d) | |
assert(order == 1) | |
orderSaga3().unsafeRunSync() | |
assert(pay == 4d) | |
assert(point == 0d) | |
assert(order == 1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment