-
Notifications
You must be signed in to change notification settings - Fork 509
/
AsyncLaws.scala
79 lines (61 loc) · 2.72 KB
/
AsyncLaws.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* Copyright 2020-2022 Typelevel
*
* 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
package laws
import cats.effect.kernel.Async
import cats.syntax.all._
import scala.concurrent.ExecutionContext
import scala.util.{Left, Right}
trait AsyncLaws[F[_]] extends GenTemporalLaws[F, Throwable] with SyncLaws[F] {
implicit val F: Async[F]
// format: off
def asyncRightIsUncancelableSequencedPure[A](a: A, fu: F[Unit]) =
(F.async[A](k => F.delay(k(Right(a))) >> fu.as(None)) <* F.unit) <-> (F.uncancelable(_ => fu) >> F.pure(a))
// format: on
// format: off
def asyncLeftIsUncancelableSequencedRaiseError[A](e: Throwable, fu: F[Unit]) =
(F.async[A](k => F.delay(k(Left(e))) >> fu.as(None)) <* F.unit) <-> (F.uncancelable(_ => fu) >> F.raiseError(e))
// format: on
def asyncRepeatedCallbackIgnored[A](a: A) =
F.async[A](k => F.delay(k(Right(a))) >> F.delay(k(Right(a))).as(None)) <-> F.pure(a)
def asyncCancelTokenIsUnsequencedOnCompletion[A](a: A, fu: F[Unit]) =
F.async[A](k => F.delay(k(Right(a))) >> F.pure(Some(fu))) <-> F.pure(a)
def asyncCancelTokenIsUnsequencedOnError[A](e: Throwable, fu: F[Unit]) =
F.async[A](k => F.delay(k(Left(e))) >> F.pure(Some(fu))) <-> F.raiseError(e)
def neverIsDerivedFromAsync[A] =
F.never[A] <-> F.async[A](_ => F.pure(None))
def executionContextCommutativity[A](fa: F[A]) =
(fa *> F.executionContext) <-> (F.executionContext <* fa)
def evalOnLocalPure(ec: ExecutionContext) =
F.evalOn(F.executionContext, ec) <-> F.evalOn(F.pure(ec), ec)
def evalOnPureIdentity[A](a: A, ec: ExecutionContext) =
F.evalOn(F.pure(a), ec) <-> F.pure(a)
def evalOnRaiseErrorIdentity(e: Throwable, ec: ExecutionContext) =
F.evalOn(F.raiseError[Unit](e), ec) <-> F.raiseError[Unit](e)
def evalOnCanceledIdentity(ec: ExecutionContext) =
F.evalOn(F.canceled, ec) <-> F.canceled
def evalOnNeverIdentity(ec: ExecutionContext) =
F.evalOn(F.never[Unit], ec) <-> F.never[Unit]
def syncStepIdentity[A](fa: F[A], limit: Int) =
F.syncStep(fa, limit).flatMap {
case Left(fa) => fa
case Right(a) => F.pure(a)
} <-> fa
}
object AsyncLaws {
def apply[F[_]](implicit F0: Async[F]): AsyncLaws[F] =
new AsyncLaws[F] { val F = F0 }
}