-
Notifications
You must be signed in to change notification settings - Fork 509
/
GenTemporalLaws.scala
44 lines (35 loc) · 1.45 KB
/
GenTemporalLaws.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
/*
* 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.GenTemporal
import cats.syntax.all._
import scala.concurrent.duration.FiniteDuration
trait GenTemporalLaws[F[_], E] extends GenSpawnLaws[F, E] with ClockLaws[F] {
implicit val F: GenTemporal[F, E]
def monotonicSleepSumIdentity(delta: FiniteDuration) =
F.sleep(delta) >> F.monotonic <-> F.monotonic.map(delta + _)
def sleepRaceMinimum(d1: FiniteDuration, d2: FiniteDuration) =
F.race(F.sleep(d1), F.sleep(d2)) >> F.monotonic <-> F.monotonic.map(d1.min(d2) + _)
def startSleepMaximum(d1: FiniteDuration, d2: FiniteDuration) =
F.start(F.sleep(d1)).flatMap(f => F.sleep(d2) >> f.join) >> F.monotonic <-> F
.monotonic
.map(d1.max(d2) + _)
}
object GenTemporalLaws {
def apply[F[_], E](implicit F0: GenTemporal[F, E]): GenTemporalLaws[F, E] =
new GenTemporalLaws[F, E] { val F = F0 }
}