lawspec 0.17.5 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +168 -0
- package/README.md +3 -3
- package/bin/lawspec.mjs +1 -1
- package/build.json +149 -87
- package/core.wasm +0 -0
- package/doctor.mjs +11 -1
- package/examples/specs/actors.lawspec +100 -0
- package/examples/specs/concurrent_collections.lawspec +48 -0
- package/examples/specs/consistency.lawspec +21 -0
- package/examples/specs/distribution.lawspec +110 -0
- package/examples/specs/domain_modeling.lawspec +20 -3
- package/examples/specs/durations.lawspec +63 -0
- package/examples/specs/generation.lawspec +142 -0
- package/examples/specs/handles.lawspec +20 -0
- package/examples/specs/keywords.lawspec +33 -0
- package/examples/specs/limits.lawspec +43 -0
- package/examples/specs/models.lawspec +121 -0
- package/examples/specs/railway.lawspec +78 -0
- package/examples/specs/resilience.lawspec +245 -0
- package/examples/specs/sessions.lawspec +37 -0
- package/examples/specs/workflows.lawspec +145 -0
- package/package.json +1 -1
- package/templates.mjs +4 -4
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
unit example.models
|
|
2
|
+
|
|
3
|
+
-- A stateful model pairs a system's commands with reference definitions over
|
|
4
|
+
-- a simpler model state. LawSpec generates runs of commands, runs them
|
|
5
|
+
-- against the adapters, and checks each result and state against the model.
|
|
6
|
+
|
|
7
|
+
-- A linear model: the stack's size is in its type, so a run never pops an
|
|
8
|
+
-- empty stack.
|
|
9
|
+
type Stack (n :: Natural) is
|
|
10
|
+
| Empty where n = 0
|
|
11
|
+
| Push top :: Int8 rest :: Stack m where n = m + 1
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
empty :: Unit -> Stack 0
|
|
15
|
+
push :: (x :: Int8) -> Stack n / Stack (n + 1) -> Unit
|
|
16
|
+
pop :: Stack (n + 1) / Stack n -> Int8
|
|
17
|
+
peek :: Stack (n + 1) / Stack (n + 1) -> Int8
|
|
18
|
+
|
|
19
|
+
definition modelPush (x :: Int8) (xs :: List Int8) :: List Int8 is Cons x xs end
|
|
20
|
+
|
|
21
|
+
definition modelPop (xs :: List Int8) :: Pair Int8 (List Int8) is
|
|
22
|
+
match xs with
|
|
23
|
+
| Nil -> Pair 0 Nil
|
|
24
|
+
| Cons x rest -> Pair x rest
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
definition modelPeek (xs :: List Int8) :: Pair Int8 (List Int8) is
|
|
29
|
+
match xs with
|
|
30
|
+
| Nil -> Pair 0 Nil
|
|
31
|
+
| Cons x rest -> Pair x xs
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
definition toList (s :: Stack n) :: List Int8 is
|
|
36
|
+
match s with
|
|
37
|
+
| Empty -> Nil
|
|
38
|
+
| Push top rest -> Cons top (toList rest)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
model stack :: Stack n by List Int8 is
|
|
43
|
+
start empty by Nil
|
|
44
|
+
push by modelPush
|
|
45
|
+
pop by modelPop
|
|
46
|
+
peek by modelPeek
|
|
47
|
+
abstract toList
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
-- A shared model: commands take one counter, whose type never changes, and
|
|
51
|
+
-- may run at the same time, so each must be atomic. LawSpec also runs them
|
|
52
|
+
-- in parallel and checks that every history is linearizable.
|
|
53
|
+
type Counter is Counter id :: Int32 end
|
|
54
|
+
|
|
55
|
+
newCounter :: Unit -> Counter
|
|
56
|
+
async increment :: Counter -> Int64
|
|
57
|
+
async decrement :: Counter -> Int64
|
|
58
|
+
async read :: Counter -> Int64
|
|
59
|
+
|
|
60
|
+
definition modelIncrement (n :: Int64 where n >= 0 && n <= 1000000) :: Pair Int64 Int64 is Pair (n + 1) (n + 1) end
|
|
61
|
+
definition modelDecrement (n :: Int64 where n >= 0 && n <= 1000000) :: Pair Int64 Int64 is Pair (n - 1) (n - 1) end
|
|
62
|
+
definition modelRead (n :: Int64) :: Pair Int64 Int64 is Pair n n end
|
|
63
|
+
definition positive (n :: Int64) :: Bool is n > 0 end
|
|
64
|
+
|
|
65
|
+
model counter :: shared Counter by Int64 is
|
|
66
|
+
start newCounter ~ 0
|
|
67
|
+
increment ~ modelIncrement
|
|
68
|
+
decrement ~ modelDecrement when positive
|
|
69
|
+
read ~ modelRead
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
-- A protocol lists what one end of a channel sends and receives; the other
|
|
73
|
+
-- end does the reverse. Scenarios run a shared model's commands from
|
|
74
|
+
-- processes that run at the same time and talk over channels; LawSpec checks
|
|
75
|
+
-- that they cannot deadlock or race, then runs them on many schedules, and
|
|
76
|
+
-- each run must agree with the model.
|
|
77
|
+
protocol Report is
|
|
78
|
+
send Int64
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
protocol Request is
|
|
82
|
+
send Report
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
scenario `a count is passed on` in counter is
|
|
86
|
+
channel report :: Report
|
|
87
|
+
par
|
|
88
|
+
n <- increment
|
|
89
|
+
send report n
|
|
90
|
+
with
|
|
91
|
+
receive report m
|
|
92
|
+
expect m = 1
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
-- The asker hands its end of the answer channel to a worker, which replies
|
|
97
|
+
-- to the listener directly.
|
|
98
|
+
scenario `a reply is delegated` in counter is
|
|
99
|
+
channel ask :: Request
|
|
100
|
+
channel answer :: Report
|
|
101
|
+
par
|
|
102
|
+
send ask answer
|
|
103
|
+
with
|
|
104
|
+
receive ask reply
|
|
105
|
+
n <- increment
|
|
106
|
+
send reply n
|
|
107
|
+
with
|
|
108
|
+
receive answer m
|
|
109
|
+
expect m = 1
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
scenario `two counts at once` in counter is
|
|
114
|
+
par
|
|
115
|
+
a <- increment
|
|
116
|
+
with
|
|
117
|
+
b <- increment
|
|
118
|
+
end
|
|
119
|
+
total <- read
|
|
120
|
+
expect total = 2
|
|
121
|
+
end
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
unit example.railway
|
|
2
|
+
|
|
3
|
+
-- Railway combinators on Either. Each symbol has a prelude name, and both are
|
|
4
|
+
-- rewritten into matches, so they work in laws and checked definitions alike.
|
|
5
|
+
|
|
6
|
+
type Problem is | TooSmall | TooLarge | Odd end
|
|
7
|
+
|
|
8
|
+
definition positive (x :: BigInt) :: Either Problem BigInt is
|
|
9
|
+
prelude.select (x > 0) (Right x) (Left TooSmall)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
definition small (x :: BigInt) :: Either Problem BigInt is
|
|
13
|
+
prelude.select (x < 100) (Right x) (Left TooLarge)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
definition even (x :: BigInt) :: Bool is prelude.rem x 2 == 0 end
|
|
17
|
+
|
|
18
|
+
definition twice (x :: BigInt) :: BigInt is x + x end
|
|
19
|
+
|
|
20
|
+
definition retry (p :: Problem) :: Either Problem BigInt is Right 1 end
|
|
21
|
+
|
|
22
|
+
-- A checked pipeline written with symbols: positive, then small, then doubled.
|
|
23
|
+
definition checked (x :: BigInt) :: Either Problem BigInt is
|
|
24
|
+
twice <$> (positive x >>= small)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
-- The same pipeline in English.
|
|
28
|
+
definition checkedInWords (x :: BigInt) :: Either Problem BigInt is
|
|
29
|
+
prelude.map twice (prelude.bind (positive x) small)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
law `symbols and English agree` is
|
|
33
|
+
definition is `for all` (x :: BigInt) . checked x = checkedInWords x end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
law `binding associates` is
|
|
37
|
+
definition is
|
|
38
|
+
`for all` (x :: BigInt) . ((positive x >>= small) >>= positive) = (positive x >>= (small >=> positive))
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
law `a failure short-circuits` is
|
|
43
|
+
definition is `for all` (x :: BigInt) . x <= 0 implies checked x = Left TooSmall end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
law `recovery replaces a failure` is
|
|
47
|
+
definition is `for all` (x :: BigInt) . (positive x <|> retry) = prelude.select (x > 0) (Right x) (Right 1) end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
law `ensure keeps even values` is
|
|
51
|
+
definition is
|
|
52
|
+
`for all` (x :: BigInt) .
|
|
53
|
+
prelude.ensure even Odd (positive x) = prelude.select (x > 0) (prelude.select (even x) (Right x) (Left Odd)) (Left TooSmall)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
law `both pairs successes` is
|
|
58
|
+
definition is
|
|
59
|
+
`for all` (x :: BigInt) . prelude.isRight (positive x <*> small x) = (x > 0 && x < 100)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
-- The prelude's railway laws, instantiated for these definitions.
|
|
64
|
+
law `positive is a left identity case` is
|
|
65
|
+
definition is `bind has a left identity` positive end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
law `positive, small and positive associate` is
|
|
69
|
+
definition is `bind is associative` positive small positive end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
law `mapping twice fuses` is
|
|
73
|
+
definition is `map fuses` positive twice twice end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
law `retry keeps successes` is
|
|
77
|
+
definition is `recovery keeps successes` positive retry end
|
|
78
|
+
end
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
unit example.resilience
|
|
2
|
+
|
|
3
|
+
import lawspec.resilience as r
|
|
4
|
+
import example.limits as limits
|
|
5
|
+
|
|
6
|
+
-- Conformance of each target's workflow runtime with a reference model.
|
|
7
|
+
-- The adapters call the runtime; the checked definitions are the reference.
|
|
8
|
+
-- Delays are whole microseconds.
|
|
9
|
+
|
|
10
|
+
-- The delay before attempt k (2 or more) of each retry strategy.
|
|
11
|
+
definition exponentialDelay (base :: Integer where base >= 0 && base <= 1000000)
|
|
12
|
+
(factor :: Integer where factor >= 1 && factor <= 4)
|
|
13
|
+
(attempt :: Integer where attempt >= 2 && attempt <= 10) :: Integer is
|
|
14
|
+
base * prelude.pow factor (attempt - 2)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
definition linearDelay (base :: Integer where base >= 0 && base <= 1000000)
|
|
18
|
+
(step :: Integer where step >= 0 && step <= 1000000)
|
|
19
|
+
(attempt :: Integer where attempt >= 2 && attempt <= 10) :: Integer is
|
|
20
|
+
base + step * (attempt - 2)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
runtimeExponentialDelay :: Integer -> Integer -> Integer -> Integer
|
|
24
|
+
runtimeLinearDelay :: Integer -> Integer -> Integer -> Integer
|
|
25
|
+
runtimeFibonacciDelay :: Integer -> Integer -> Integer
|
|
26
|
+
|
|
27
|
+
-- The first outputs of SplitMix64 for a seed, and full jitter.
|
|
28
|
+
splitMix :: UInt64 -> Int32 -> List UInt64
|
|
29
|
+
fullJitter :: UInt64 -> Integer -> Integer
|
|
30
|
+
|
|
31
|
+
-- A stage that always fails, retried exponentially from 100ms, doubling, for
|
|
32
|
+
-- the given number of attempts: the waits the runtime makes.
|
|
33
|
+
retriedWaits :: Int32 -> List Integer
|
|
34
|
+
|
|
35
|
+
-- The same, with a when predicate that rejects the error: no waits.
|
|
36
|
+
rejectedWaits :: Int32 -> List Integer
|
|
37
|
+
|
|
38
|
+
-- Unrefined inputs are mapped into range, so every target generates them
|
|
39
|
+
-- directly: a base up to 65535us, a factor of 1 to 4, and attempts 2 to 10.
|
|
40
|
+
law `exponential delays match the reference` is
|
|
41
|
+
definition is
|
|
42
|
+
`for all` (base :: UInt16) (f :: UInt8) (a :: UInt8) .
|
|
43
|
+
runtimeExponentialDelay (prelude.Integer base) (1 + prelude.rem (prelude.Integer f) 4) (2 + prelude.rem (prelude.Integer a) 9) =
|
|
44
|
+
exponentialDelay (prelude.Integer base) (1 + prelude.rem (prelude.Integer f) 4) (2 + prelude.rem (prelude.Integer a) 9)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
law `the fifth exponential attempt` is
|
|
49
|
+
definition is
|
|
50
|
+
`for all` (base :: Integer where base == 100000) . runtimeExponentialDelay base 2 5 = 800000
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
law `linear delays match the reference` is
|
|
55
|
+
definition is
|
|
56
|
+
`for all` (base :: UInt16) (step :: UInt16) (a :: UInt8) .
|
|
57
|
+
runtimeLinearDelay (prelude.Integer base) (prelude.Integer step) (2 + prelude.rem (prelude.Integer a) 9) =
|
|
58
|
+
linearDelay (prelude.Integer base) (prelude.Integer step) (2 + prelude.rem (prelude.Integer a) 9)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
law `fibonacci delays add the two before` is
|
|
63
|
+
definition is
|
|
64
|
+
`for all` (base :: UInt16) (a :: UInt8) .
|
|
65
|
+
runtimeFibonacciDelay (prelude.Integer base) (4 + prelude.rem (prelude.Integer a) 27) =
|
|
66
|
+
runtimeFibonacciDelay (prelude.Integer base) (3 + prelude.rem (prelude.Integer a) 27) +
|
|
67
|
+
runtimeFibonacciDelay (prelude.Integer base) (2 + prelude.rem (prelude.Integer a) 27)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
law `fibonacci delays start at the base` is
|
|
72
|
+
definition is
|
|
73
|
+
`for all` (base :: UInt16) .
|
|
74
|
+
runtimeFibonacciDelay (prelude.Integer base) 2 = prelude.Integer base and
|
|
75
|
+
runtimeFibonacciDelay (prelude.Integer base) 3 = prelude.Integer base
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
law `SplitMix64 is the same everywhere` is
|
|
80
|
+
definition is
|
|
81
|
+
`for all` (count :: Int8 where count >= 0 && count <= 3) .
|
|
82
|
+
prelude.length (splitMix 0 (prelude.Int32 count)) = prelude.Integer count
|
|
83
|
+
end
|
|
84
|
+
example `seed zero` is
|
|
85
|
+
count = 3
|
|
86
|
+
expect splitMix 0 (prelude.Int32 count) = [16294208416658607535, 7960286522194355700, 487617019471545679]
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
law `full jitter stays within the delay` is
|
|
91
|
+
definition is
|
|
92
|
+
`for all` (seed :: UInt64) (delay :: UInt32) .
|
|
93
|
+
(fullJitter seed (prelude.Integer delay) >= 0 && fullJitter seed (prelude.Integer delay) <= prelude.Integer delay) = true
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
law `a failing stage waits between its attempts` is
|
|
98
|
+
definition is
|
|
99
|
+
`for all` (attempts :: Int8 where attempts >= 1 && attempts <= 6) .
|
|
100
|
+
prelude.length (retriedWaits (prelude.Int32 attempts)) = prelude.Integer attempts - 1
|
|
101
|
+
end
|
|
102
|
+
example `four attempts` is
|
|
103
|
+
attempts = 4
|
|
104
|
+
expect retriedWaits (prelude.Int32 attempts) = [100000, 200000, 400000]
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
law `a rejected error is not retried` is
|
|
109
|
+
definition is
|
|
110
|
+
`for all` (attempts :: Int8 where attempts >= 1 && attempts <= 6) .
|
|
111
|
+
rejectedWaits (prelude.Int32 attempts) = []
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
-- Calls limited at each time (microseconds, in order) under one runtime
|
|
116
|
+
-- with its gates on: whether each call was admitted.
|
|
117
|
+
limitedAt :: List Integer -> List Bool
|
|
118
|
+
|
|
119
|
+
definition admits (gate :: r.Gate) :: Bool is
|
|
120
|
+
match gate with
|
|
121
|
+
| Admit -> true
|
|
122
|
+
| WaitFor micros -> false
|
|
123
|
+
| Reject -> false
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
-- The reference: the resilience unit's token bucket, folded over the times.
|
|
128
|
+
definition bucketRun (state :: r.TokenBucket) (times :: List Integer) :: List Bool is
|
|
129
|
+
match times with
|
|
130
|
+
| Nil -> Nil
|
|
131
|
+
| Cons t rest ->
|
|
132
|
+
match r.tokenBucketAdmit 2 1000000 state t with
|
|
133
|
+
| Step next gate -> Cons (admits gate) (bucketRun next rest)
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
-- Times a millisecond apart and more, as a list of gaps.
|
|
139
|
+
definition timesFrom (start :: Integer) (gaps :: List UInt16) :: List Integer is
|
|
140
|
+
match gaps with
|
|
141
|
+
| Nil -> Nil
|
|
142
|
+
| Cons gap rest -> Cons start (timesFrom (start + prelude.Integer gap * 1000) rest)
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
law `the rate limit admits as the token bucket does` is
|
|
147
|
+
definition is
|
|
148
|
+
`for all` (gaps :: List UInt16) .
|
|
149
|
+
limitedAt (timesFrom 0 gaps) = bucketRun (r.tokenBucketStart 2 1000000 0) (timesFrom 0 gaps)
|
|
150
|
+
end
|
|
151
|
+
example `three calls at once` is
|
|
152
|
+
gaps = [0, 0, 0]
|
|
153
|
+
expect limitedAt (timesFrom 0 gaps) = [true, true, false]
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
-- The other state machines, which every target runs as generated code.
|
|
158
|
+
law `an open breaker rejects until its cooldown ends` is
|
|
159
|
+
definition is
|
|
160
|
+
`for all` (t :: UInt16) .
|
|
161
|
+
r.breakerAdmit (r.Open 1000) (prelude.Integer t) =
|
|
162
|
+
prelude.select (prelude.Integer t >= 1000) (r.Step r.HalfOpen r.Admit) (r.Step (r.Open 1000) r.Reject)
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
law `a breaker opens after enough failures in its window` is
|
|
167
|
+
definition is
|
|
168
|
+
`for all` (gap :: UInt8) .
|
|
169
|
+
r.breakerRecord 2 1000 500 (r.breakerRecord 2 1000 500 (r.Closed []) 10 false) (10 + prelude.Integer gap) false =
|
|
170
|
+
r.Open (510 + prelude.Integer gap)
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
law `a breaker forgets failures outside its window` is
|
|
175
|
+
definition is
|
|
176
|
+
`for all` (gap :: UInt8) .
|
|
177
|
+
r.breakerRecord 2 100 500 (r.breakerRecord 2 100 500 (r.Closed []) 0 false) (200 + prelude.Integer gap) false =
|
|
178
|
+
r.Closed [200 + prelude.Integer gap]
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
law `a successful trial closes the breaker` is
|
|
183
|
+
definition is
|
|
184
|
+
`for all` (t :: UInt16) . r.breakerRecord 2 1000 500 r.HalfOpen (prelude.Integer t) true = r.Closed []
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
law `a bulkhead admits up to its limit` is
|
|
189
|
+
definition is
|
|
190
|
+
`for all` (running :: UInt8) .
|
|
191
|
+
r.bulkheadAdmit 4 (r.Bulkhead (prelude.Integer running)) 0 =
|
|
192
|
+
prelude.select (prelude.Integer running < 4)
|
|
193
|
+
(r.Step (r.Bulkhead (prelude.Integer running + 1)) r.Admit)
|
|
194
|
+
(r.Step (r.Bulkhead (prelude.Integer running)) (r.WaitFor 1000))
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
law `a sliding window admits n calls per period` is
|
|
199
|
+
definition is
|
|
200
|
+
`for all` (t :: UInt16) .
|
|
201
|
+
r.slidingWindowAdmit 2 1000 (r.SlidingWindow [prelude.Integer t, prelude.Integer t]) (prelude.Integer t + 1) =
|
|
202
|
+
r.Step (r.SlidingWindow [prelude.Integer t, prelude.Integer t]) (r.WaitFor 999)
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
-- Books a ticket under a fresh runtime: the stages whose undos ran.
|
|
207
|
+
compensationsFor :: Int64 -> List Text
|
|
208
|
+
|
|
209
|
+
law `a failed booking releases its seat` is
|
|
210
|
+
definition is
|
|
211
|
+
`for all` (n :: Int64) .
|
|
212
|
+
compensationsFor n = prelude.select (n < 0) ["reserveSeat"] []
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
-- Quotes a ticket under a runtime with the real clock: whether it timed out.
|
|
217
|
+
async quoteTimedOut :: Int64 -> Bool
|
|
218
|
+
|
|
219
|
+
law `a slow quote times out` is
|
|
220
|
+
definition is
|
|
221
|
+
`for all` (n :: Int64) . quoteTimedOut n = (n == -1)
|
|
222
|
+
end
|
|
223
|
+
example `a slow quote` is
|
|
224
|
+
n = -1
|
|
225
|
+
expect quoteTimedOut n = true
|
|
226
|
+
end
|
|
227
|
+
example `a quick quote` is
|
|
228
|
+
n = 0
|
|
229
|
+
expect quoteTimedOut n = false
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
-- Quotes a ticket under a runtime with the real clock: whether it succeeded
|
|
234
|
+
-- quickly (within 400ms), for ticket -2 through a hedged attempt.
|
|
235
|
+
async quoteHedged :: Int64 -> Bool
|
|
236
|
+
|
|
237
|
+
law `a stalled quote is hedged` is
|
|
238
|
+
definition is
|
|
239
|
+
`for all` (n :: Int64) . quoteHedged n = true
|
|
240
|
+
end
|
|
241
|
+
example `a stalled quote` is
|
|
242
|
+
n = -2
|
|
243
|
+
expect quoteHedged n = true
|
|
244
|
+
end
|
|
245
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
unit example.sessions
|
|
2
|
+
|
|
3
|
+
-- Protocols become typed channel ends on every target, for implementation
|
|
4
|
+
-- code: each step is its own type, so steps out of order do not compile,
|
|
5
|
+
-- and an end used twice fails. The adapters below implement adding with a
|
|
6
|
+
-- server process they talk to through those generated types.
|
|
7
|
+
|
|
8
|
+
-- What a server does: receive two numbers, send their sum. Its client holds
|
|
9
|
+
-- the other end, which does the reverse.
|
|
10
|
+
protocol Serve is
|
|
11
|
+
receive Int32
|
|
12
|
+
receive Int32
|
|
13
|
+
send Int64
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
-- Handing a server's end to a worker (delegation).
|
|
17
|
+
protocol Hire is
|
|
18
|
+
send Serve
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
-- Adds through a server process.
|
|
22
|
+
add :: Int32 -> Int32 -> Integer
|
|
23
|
+
|
|
24
|
+
-- Adds through a worker that is handed the server's end over a Hire channel.
|
|
25
|
+
addHired :: Int32 -> Int32 -> Integer
|
|
26
|
+
|
|
27
|
+
law `a server adds` is
|
|
28
|
+
definition is
|
|
29
|
+
`for all` (a :: Int32) (b :: Int32) . add a b = prelude.Integer a + prelude.Integer b
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
law `a hired worker adds` is
|
|
34
|
+
definition is
|
|
35
|
+
`for all` (a :: Int32) (b :: Int32) . addHired a b = prelude.Integer a + prelude.Integer b
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
unit example.workflows
|
|
2
|
+
|
|
3
|
+
-- Workflows compose steps written by hand on each target. LawSpec generates
|
|
4
|
+
-- the composition itself, so a workflow behaves the same everywhere: every
|
|
5
|
+
-- stage below is a keyword, and each has a symbol too (>>=, <$>, <!>, <|>, ??).
|
|
6
|
+
|
|
7
|
+
type Signup is Signup name :: Text age :: Int32 end
|
|
8
|
+
type Account is Account name :: Text age :: Int32 level :: Int32 end
|
|
9
|
+
type SignupError is | MissingName | TooYoung | NameTooLong | Unavailable end
|
|
10
|
+
|
|
11
|
+
-- Checked definitions: LawSpec writes these on every target.
|
|
12
|
+
definition explain (reason :: Text) :: SignupError is TooYoung end
|
|
13
|
+
|
|
14
|
+
definition promote (a :: Account) :: Account is
|
|
15
|
+
match a with
|
|
16
|
+
| Account name age level -> Account name age 2
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
definition brief (a :: Account) :: Bool is
|
|
21
|
+
match a with
|
|
22
|
+
| Account name age level -> prelude.length name <= 8
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
definition tooLong (a :: Account) :: SignupError is NameTooLong end
|
|
27
|
+
|
|
28
|
+
definition nameOf (s :: Signup) :: Text is
|
|
29
|
+
match s with
|
|
30
|
+
| Signup name age -> name
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
-- Side steps and recovery handlers, also written by hand.
|
|
35
|
+
audit :: Account -> Bool
|
|
36
|
+
waitlist :: SignupError -> Either SignupError Account
|
|
37
|
+
|
|
38
|
+
-- The workflow's error type is declared, so every step must fail with
|
|
39
|
+
-- SignupError; checkAge fails with a reason, which mapError turns into one.
|
|
40
|
+
workflow register :: Signup -> Either SignupError Account is
|
|
41
|
+
checkName :: Signup -> Either SignupError Signup
|
|
42
|
+
retry exponential 100ms 2 3 max 1s jitter full
|
|
43
|
+
then checkAge :: Signup -> Either Text Signup
|
|
44
|
+
mapError explain
|
|
45
|
+
then openAccount :: Signup -> Either SignupError Account
|
|
46
|
+
map promote
|
|
47
|
+
tap audit
|
|
48
|
+
ensure brief else tooLong
|
|
49
|
+
orElse waitlist
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
-- With the error written as _, LawSpec generates GreetError, with one
|
|
53
|
+
-- constructor per step that can fail: GreetCheckNameFailed and GreetCheckAgeFailed.
|
|
54
|
+
workflow greet :: Signup -> Either _ Text is
|
|
55
|
+
checkName :: Signup -> Either SignupError Signup
|
|
56
|
+
>>= checkAge :: Signup -> Either Text Signup
|
|
57
|
+
<$> nameOf
|
|
58
|
+
?? anonymous
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
definition anonymous (e :: GreetError) :: Text is "guest" end
|
|
62
|
+
|
|
63
|
+
law `registering an account` is
|
|
64
|
+
definition is
|
|
65
|
+
`for all` (s :: Signup) .
|
|
66
|
+
prelude.isRight (register s) = (match s with
|
|
67
|
+
| Signup name age ->
|
|
68
|
+
prelude.length name > 0 && age >= 18 && prelude.length name <= 8
|
|
69
|
+
end)
|
|
70
|
+
end
|
|
71
|
+
example `a new account is promoted` is
|
|
72
|
+
s = Signup "ada" 30
|
|
73
|
+
expect register s = Right (Account "ada" 30 2)
|
|
74
|
+
end
|
|
75
|
+
example `a name is required` is
|
|
76
|
+
s = Signup "" 30
|
|
77
|
+
expect register s = Left MissingName
|
|
78
|
+
end
|
|
79
|
+
example `seventeen is too young` is
|
|
80
|
+
s = Signup "bob" 17
|
|
81
|
+
expect register s = Left TooYoung
|
|
82
|
+
end
|
|
83
|
+
example `a long name is rejected` is
|
|
84
|
+
s = Signup "bartholomew" 30
|
|
85
|
+
expect register s = Left NameTooLong
|
|
86
|
+
end
|
|
87
|
+
example `a taken name joins the waitlist` is
|
|
88
|
+
s = Signup "taken" 30
|
|
89
|
+
expect register s = Right (Account "waitlist" 18 0)
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
law `greeting never fails` is
|
|
94
|
+
definition is
|
|
95
|
+
`for all` (s :: Signup) . prelude.isRight (greet s) = true
|
|
96
|
+
end
|
|
97
|
+
example `a named greeting` is
|
|
98
|
+
s = Signup "ada" 30
|
|
99
|
+
expect greet s = Right "ada"
|
|
100
|
+
end
|
|
101
|
+
example `an anonymous greeting` is
|
|
102
|
+
s = Signup "" 30
|
|
103
|
+
expect greet s = Right "guest"
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
-- An all group runs its steps on the same state and combines their results.
|
|
108
|
+
-- With accumulate, every failing step's error is reported, in a generated
|
|
109
|
+
-- VetFailures constructor.
|
|
110
|
+
definition firstName (a :: Signup) (b :: Signup) :: Text is nameOf a end
|
|
111
|
+
|
|
112
|
+
workflow vet :: Signup -> Either _ Text is
|
|
113
|
+
all accumulate
|
|
114
|
+
then checkName
|
|
115
|
+
then checkAge
|
|
116
|
+
end
|
|
117
|
+
combine firstName
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
law `vetting reports every problem` is
|
|
121
|
+
definition is
|
|
122
|
+
`for all` (s :: Signup) .
|
|
123
|
+
prelude.isRight (vet s) = (match s with
|
|
124
|
+
| Signup name age -> prelude.length name > 0 && age >= 18
|
|
125
|
+
end)
|
|
126
|
+
end
|
|
127
|
+
example `no problems` is
|
|
128
|
+
s = Signup "ada" 30
|
|
129
|
+
expect vet s = Right "ada"
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
law `vetting a nameless child reports both problems` is
|
|
134
|
+
definition is
|
|
135
|
+
`for all` (age :: Int8 where age >= 0 && age < 18) .
|
|
136
|
+
vet (Signup "" (prelude.Int32 age)) = Left (VetFailures [VetCheckNameFailed MissingName, VetCheckAgeFailed "too young"])
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
law `vetting a named child reports its age` is
|
|
141
|
+
definition is
|
|
142
|
+
`for all` (age :: Int8 where age >= 0 && age < 18) .
|
|
143
|
+
vet (Signup "ada" (prelude.Int32 age)) = Left (VetFailures [VetCheckAgeFailed "too young"])
|
|
144
|
+
end
|
|
145
|
+
end
|
package/package.json
CHANGED
package/templates.mjs
CHANGED
|
@@ -81,23 +81,23 @@ const scaffolds = {
|
|
|
81
81
|
"haskell": {
|
|
82
82
|
"readable": {
|
|
83
83
|
"stack.yaml": "snapshot: lts-24.58\npackages: [.]\n",
|
|
84
|
-
"package.yaml": "name: lawspec-example\nversion: 0.1.0\ndependencies: [base, text, bytestring, containers]\nlibrary:\n source-dirs: src\ntests:\n laws:\n main: Spec.hs\n source-dirs: test\n dependencies: [lawspec-example, hspec, hedgehog, hspec-hedgehog, containers, mtl]\n build-tools: [hspec-discover]\n",
|
|
84
|
+
"package.yaml": "name: lawspec-example\nversion: 0.1.0\ndependencies: [base, text, bytestring, containers, network]\nlibrary:\n source-dirs: src\ntests:\n laws:\n main: Spec.hs\n source-dirs: test\n dependencies: [lawspec-example, hspec, hedgehog, hspec-hedgehog, containers, mtl]\n build-tools: [hspec-discover]\n",
|
|
85
85
|
"test/Spec.hs": "{-# OPTIONS_GHC -F -pgmF hspec-discover #-}\n"
|
|
86
86
|
},
|
|
87
87
|
"compact": {
|
|
88
88
|
"stack.yaml": "snapshot: lts-24.58\npackages: [.]\n",
|
|
89
|
-
"package.yaml": "name: lawspec-example\nversion: 0.1.0\ndependencies: [base, text, bytestring, containers]\nlibrary:\n source-dirs: src\ntests:\n laws:\n main: Spec.hs\n source-dirs: test\n dependencies: [lawspec-example, hspec, hedgehog, hspec-hedgehog, containers, mtl]\n build-tools: [hspec-discover]\n",
|
|
89
|
+
"package.yaml": "name: lawspec-example\nversion: 0.1.0\ndependencies: [base, text, bytestring, containers, network]\nlibrary:\n source-dirs: src\ntests:\n laws:\n main: Spec.hs\n source-dirs: test\n dependencies: [lawspec-example, hspec, hedgehog, hspec-hedgehog, containers, mtl]\n build-tools: [hspec-discover]\n",
|
|
90
90
|
"test/Spec.hs": "{-# OPTIONS_GHC -F -pgmF hspec-discover #-}\n"
|
|
91
91
|
}
|
|
92
92
|
},
|
|
93
93
|
"kotlin": {
|
|
94
94
|
"readable": {
|
|
95
95
|
"settings.gradle.kts": "rootProject.name = \"lawspec-example\"\n",
|
|
96
|
-
"build.gradle.kts": "plugins {\n kotlin(\"jvm\") version \"2.3.21\"\n}\n\nrepositories {\n mavenCentral()\n}\n\nkotlin {\n jvmToolchain(25)\n}\n\ndependencies {\n testImplementation(\"io.kotest:kotest-runner-junit5:5.9.1\")\n testImplementation(\"io.kotest:kotest-assertions-core:5.9.1\")\n testImplementation(\"io.kotest:kotest-property:5.9.1\")\n}\n\ntasks.test {\n useJUnitPlatform()\n}\n"
|
|
96
|
+
"build.gradle.kts": "plugins {\n kotlin(\"jvm\") version \"2.3.21\"\n}\n\nrepositories {\n mavenCentral()\n}\n\nkotlin {\n jvmToolchain(25)\n}\n\ndependencies {\n implementation(\"org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0\")\n testImplementation(\"io.kotest:kotest-runner-junit5:5.9.1\")\n testImplementation(\"io.kotest:kotest-assertions-core:5.9.1\")\n testImplementation(\"io.kotest:kotest-property:5.9.1\")\n}\n\ntasks.test {\n useJUnitPlatform()\n}\n"
|
|
97
97
|
},
|
|
98
98
|
"compact": {
|
|
99
99
|
"settings.gradle.kts": "rootProject.name = \"lawspec-example\"\n",
|
|
100
|
-
"build.gradle.kts": "plugins { kotlin(\"jvm\") version \"2.3.21\" }\nrepositories { mavenCentral() }\nkotlin { jvmToolchain(25) }\ndependencies { testImplementation(\"io.kotest:kotest-runner-junit5:5.9.1\"); testImplementation(\"io.kotest:kotest-assertions-core:5.9.1\"); testImplementation(\"io.kotest:kotest-property:5.9.1\") }\ntasks.test { useJUnitPlatform() }\n"
|
|
100
|
+
"build.gradle.kts": "plugins { kotlin(\"jvm\") version \"2.3.21\" }\nrepositories { mavenCentral() }\nkotlin { jvmToolchain(25) }\ndependencies { implementation(\"org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0\"); testImplementation(\"io.kotest:kotest-runner-junit5:5.9.1\"); testImplementation(\"io.kotest:kotest-assertions-core:5.9.1\"); testImplementation(\"io.kotest:kotest-property:5.9.1\") }\ntasks.test { useJUnitPlatform() }\n"
|
|
101
101
|
}
|
|
102
102
|
},
|
|
103
103
|
"rust": {
|