-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2019-06-23-000-hedgehog-on-a-rest-api--part-2.html
333 lines (294 loc) · 16.4 KB
/
2019-06-23-000-hedgehog-on-a-rest-api--part-2.html
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="alternate"
type="application/rss+xml"
href="https://magnus.therning.org/feed.xml"
title="RSS feed for https://magnus.therning.org/">
<title>Hedgehog on a REST API, part 2</title>
<meta name="author" content="Magnus Therning"><meta name="referrer" content="no-referrer"><link href= "static/style.css" rel="stylesheet" type="text/css" /><link href= "static/htmlize.css" rel="stylesheet" type="text/css" /><link href= "static/extra_style.css" rel="stylesheet" type="text/css" /></head>
<body>
<div id="preamble" class="status"><div class="nav-bar"><a class="nav-link" href="./index.html">Top</a><a class="nav-link" href="./archive.html">Archive</a><a class="nav-link align-right" href="./feed.xml"><img src="static/rss-feed-icon.png" style="height: 24px;" /></a></div></div>
<div id="content">
<div class="post-date">23 Jun 2019</div><h1 class="post-title"><a href="https://magnus.therning.org/2019-06-23-000-hedgehog-on-a-rest-api--part-2.html">Hedgehog on a REST API, part 2</a></h1>
<p>
This is a short follow-up to <a href="2019-05-30-000-hedgehog-on-a-rest-api.html">Hedgehog on a REST API</a> where I actually run the
tests in that post.
</p>
<div id="outline-container-org27855a2" class="outline-2">
<h2 id="org27855a2">Fixing an issue with the model</h2>
<div class="outline-text-2" id="text-org27855a2">
<p>
The first issue I run into is
</p>
<pre class="example" id="orgdd312b9">
━━━ Main ━━━
✗ sequential failed after 18 tests and 1 shrink.
┏━━ tst/test-01.hs ━━━
89 ┃ getUser :: (MonadGen n, MonadIO m) => Command n m State
90 ┃ getUser = Command gen exec [ Require r
91 ┃ , Ensure e
92 ┃ ]
93 ┃ where
94 ┃ gen (State m) = case M.keys m of
95 ┃ [] -> Nothing
96 ┃ ks -> Just $ GetUser <$> Gen.element ks
97 ┃
98 ┃ exec (GetUser i) = liftIO $ do
99 ┃ mgr <- newManager defaultManagerSettings
100 ┃ getReq <- parseRequest $ "GET http://localhost:3000/users/" ++ show i
101 ┃ getResp <- httpLbs getReq mgr
102 ┃ let us = decode $ responseBody getResp :: Maybe [User]
103 ┃ return (status200 == responseStatus getResp, us)
104 ┃
105 ┃ r (State m) (GetUser i) = i `elem` M.keys m
106 ┃
107 ┃ e _ _ (GetUser _) (r, us) = do
108 ┃ r === True
109 ┃ assert $ isJust us
110 ┃ (length <$> us) === Just 1
┃ ^^^^^^^^^^^^^^^^^^^^^^^^^^
┃ │ Failed (- lhs =/= + rhs)
┃ │ - Just 0
┃ │ + Just 1
┏━━ tst/test-01.hs ━━━
118 ┃ prop_seq :: Property
119 ┃ prop_seq = property $ do
120 ┃ actions <- forAll $ Gen.sequential (Range.linear 1 10) initialState [addUser, deleteUser, getUser]
┃ │ Var 0 = AddUser ""
┃ │ Var 1 = GetUser 1
121 ┃ resetWS
122 ┃ executeSequential initialState actions
This failure can be reproduced by running:
> recheck (Size 17) (Seed 2158538972777046104 (-1442908127347265675)) sequential
✗ 1 failed.
</pre>
<p>
It's easy to verify this using <a href="https://httpie.org/">httpie</a>:
</p>
<div class="org-src-container">
<pre class="src src-shell">$ http -p b POST :3000/users userId:=<span class="org-highlight-numbers-number">0</span> <span class="org-string">"userName="</span>
<span class="org-rainbow-delimiters-depth-1">{</span>
<span class="org-string">"userId"</span>: <span class="org-highlight-numbers-number">0,</span>
<span class="org-string">"userName"</span>: <span class="org-string">""</span>
<span class="org-rainbow-delimiters-depth-1">}</span>
$ http -p b GET :3000/users/1
<span class="org-rainbow-delimiters-depth-1">[]</span>
</pre>
</div>
<p>
It's clear that my assumption that <i>User ID</i> starts at 1 is wrong. Luckily
fixing that isn't too difficult. Instead of defining the update function for
<code>addUser</code> as
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span class="org-haskell-definition">u</span> <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-constructor">State</span> m<span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-constructor">AddUser</span> n<span class="org-rainbow-delimiters-depth-1">)</span> _o <span class="org-haskell-operator">=</span> <span class="org-haskell-constructor">State</span> <span class="org-haskell-operator">$</span> M.insert k n m
<span class="org-haskell-keyword">where</span>
k <span class="org-haskell-operator">=</span> succ <span class="org-haskell-operator">$</span> foldl max <span class="org-highlight-numbers-number">0</span> <span class="org-rainbow-delimiters-depth-1">(</span>M.keys m<span class="org-rainbow-delimiters-depth-1">)</span>
</pre>
</div>
<p>
I define it as
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span class="org-haskell-definition">u</span> <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-constructor">State</span> m<span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-constructor">AddUser</span> n<span class="org-rainbow-delimiters-depth-1">)</span> _o <span class="org-haskell-operator">=</span> <span class="org-haskell-constructor">State</span> <span class="org-haskell-operator">$</span> M.insert k n m
<span class="org-haskell-keyword">where</span>
k <span class="org-haskell-operator">=</span> <span class="org-haskell-keyword">case</span> M.keys m <span class="org-haskell-keyword">of</span>
<span class="org-haskell-constructor"><span class="org-rainbow-delimiters-depth-1">[]</span></span> <span class="org-haskell-operator">-></span> <span class="org-highlight-numbers-number">0</span>
ks <span class="org-haskell-operator">-></span> succ <span class="org-haskell-operator">$</span> foldl max <span class="org-highlight-numbers-number">0</span> ks
</pre>
</div>
<p>
The complete code at this point can be found <a href="https://gist.github.com/8cbbfdb130a173bb51c317df0c00aa15">here</a>.
</p>
</div>
</div>
<div id="outline-container-org9c34f53" class="outline-2">
<h2 id="org9c34f53">Fixing another issue with the model</h2>
<div class="outline-text-2" id="text-org9c34f53">
<p>
With that fix in place another issue with the model shows up
</p>
<pre class="example" id="orga71bb89">
━━━ Main ━━━
✗ sequential failed after 74 tests and 2 shrinks.
┏━━ tst/test-01.hs ━━━
91 ┃ getUser :: (MonadGen n, MonadIO m) => Command n m State
92 ┃ getUser = Command gen exec [ Require r
93 ┃ , Ensure e
94 ┃ ]
95 ┃ where
96 ┃ gen (State m) = case M.keys m of
97 ┃ [] -> Nothing
98 ┃ ks -> Just $ GetUser <$> Gen.element ks
99 ┃
100 ┃ exec (GetUser i) = liftIO $ do
101 ┃ mgr <- newManager defaultManagerSettings
102 ┃ getReq <- parseRequest $ "GET http://localhost:3000/users/" ++ show i
103 ┃ getResp <- httpLbs getReq mgr
104 ┃ let us = decode $ responseBody getResp :: Maybe [User]
105 ┃ return (status200 == responseStatus getResp, us)
106 ┃
107 ┃ r (State m) (GetUser i) = i `elem` M.keys m
108 ┃
109 ┃ e _ _ (GetUser _) (r, us) = do
110 ┃ r === True
111 ┃ assert $ isJust us
112 ┃ (length <$> us) === Just 1
┃ ^^^^^^^^^^^^^^^^^^^^^^^^^^
┃ │ Failed (- lhs =/= + rhs)
┃ │ - Just 0
┃ │ + Just 1
┏━━ tst/test-01.hs ━━━
120 ┃ prop_seq :: Property
121 ┃ prop_seq = property $ do
122 ┃ actions <- forAll $ Gen.sequential (Range.linear 1 10) initialState [addUser, deleteUser, getUser]
┃ │ Var 0 = AddUser ""
┃ │ Var 1 = DeleteUser 0
┃ │ Var 2 = AddUser ""
┃ │ Var 3 = GetUser 0
123 ┃ resetWS
124 ┃ executeSequential initialState actions
This failure can be reproduced by running:
> recheck (Size 73) (Seed 3813043122711576923 (-444438259649958339)) sequential
✗ 1 failed.
</pre>
<p>
Again, verifying this using <a href="https://httpie.org/">httpie</a> shows what the issue is
</p>
<div class="org-src-container">
<pre class="src src-shell">$ http -p b POST :3000/users userId:=<span class="org-highlight-numbers-number">0</span> <span class="org-string">"userName="</span>
<span class="org-rainbow-delimiters-depth-1">{</span>
<span class="org-string">"userId"</span>: <span class="org-highlight-numbers-number">0,</span>
<span class="org-string">"userName"</span>: <span class="org-string">""</span>
<span class="org-rainbow-delimiters-depth-1">}</span>
$ http -p b DELETE :3000/users/0
$ http -p b POST :3000/users userId:=<span class="org-highlight-numbers-number">0</span> <span class="org-string">"userName="</span>
<span class="org-rainbow-delimiters-depth-1">{</span>
<span class="org-string">"userId"</span>: <span class="org-highlight-numbers-number">1,</span>
<span class="org-string">"userName"</span>: <span class="org-string">""</span>
<span class="org-rainbow-delimiters-depth-1">}</span>
$ http -p b GET :3000/users/0
<span class="org-rainbow-delimiters-depth-1">[]</span>
</pre>
</div>
<p>
In other words, the model assumes that the 0 <i>User ID</i> get's re-used.
</p>
<p>
To fix this I need a bigger change. The central bit is that the state is changed
to keep track of the index more explicitly. That is, it changes from
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span class="org-haskell-keyword">newtype</span> <span class="org-haskell-type">State</span> <span class="org-rainbow-delimiters-depth-1">(</span>v <span class="org-haskell-operator">::</span> <span class="org-haskell-operator">*</span> <span class="org-haskell-operator">-></span> <span class="org-haskell-operator">*</span><span class="org-rainbow-delimiters-depth-1">)</span><span class="org-haskell-operator">=</span> <span class="org-haskell-constructor">State</span> <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-constructor">M.Map</span> <span class="org-haskell-constructor">Int</span> <span class="org-haskell-constructor">Text</span><span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-haskell-keyword">deriving</span> <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-constructor">Eq</span>, <span class="org-haskell-constructor">Show</span><span class="org-rainbow-delimiters-depth-1">)</span>
</pre>
</div>
<p>
to
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span class="org-haskell-keyword">data</span> <span class="org-haskell-type">State</span> <span class="org-rainbow-delimiters-depth-1">(</span>v <span class="org-haskell-operator">::</span> <span class="org-haskell-operator">*</span> <span class="org-haskell-operator">-></span> <span class="org-haskell-operator">*</span><span class="org-rainbow-delimiters-depth-1">)</span><span class="org-haskell-operator">=</span> <span class="org-haskell-constructor">State</span> <span class="org-haskell-constructor">Int</span> <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-constructor">M.Map</span> <span class="org-haskell-constructor">Int</span> <span class="org-haskell-constructor">Text</span><span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-haskell-keyword">deriving</span> <span class="org-rainbow-delimiters-depth-1">(</span><span class="org-haskell-constructor">Eq</span>, <span class="org-haskell-constructor">Show</span><span class="org-rainbow-delimiters-depth-1">)</span>
</pre>
</div>
<p>
That change does, quite obviously, require a bunch of other changes in the other
functions dealing with the state. The complete file can be viewed <a href="https://gist.github.com/e3c3f4a3c35ab8a529045f90f9f4ba71">here</a>.
</p>
</div>
</div>
<div id="outline-container-org5f32fc1" class="outline-2">
<h2 id="org5f32fc1">All is well, or is it?</h2>
<div class="outline-text-2" id="text-org5f32fc1">
<p>
After this the tests pass, so all is good in the world, right?
</p>
<p>
In the test I defined the <a href="2019-05-30-000-hedgehog-on-a-rest-api.html#MissingReference">property</a> over rather short sequences of commands. What
happens if I increase the (maximum) length of the sequences a bit? Instead using
<code>Range.linear 1 10</code> I'll use <code>Range.linear 1 1000</code>. Well, besides taking
slightly longer to run I get another sequence of commands that triggers an
issue:
</p>
<pre class="example" id="org112d5bd">
━━━ Main ━━━
✗ sequential failed after 13 tests and 29 shrinks.
┏━━ tst/test-01.hs ━━━
87 ┃ getUser :: (MonadGen n, MonadIO m) => Command n m State
88 ┃ getUser = Command gen exec [ Require r
89 ┃ , Ensure e
90 ┃ ]
91 ┃ where
92 ┃ gen (State _ m) = case M.keys m of
93 ┃ [] -> Nothing
94 ┃ ks -> Just $ GetUser <$> Gen.element ks
95 ┃
96 ┃ exec (GetUser i) = liftIO $ do
97 ┃ mgr <- newManager defaultManagerSettings
98 ┃ getReq <- parseRequest $ "GET http://localhost:3000/users/" ++ show i
99 ┃ getResp <- httpLbs getReq mgr
100 ┃ let us = decode $ responseBody getResp :: Maybe [User]
101 ┃ return (status200 == responseStatus getResp, us)
102 ┃
103 ┃ r (State _ m) (GetUser i) = i `elem` M.keys m
104 ┃
105 ┃ e _ _ (GetUser _) (r, us) = do
106 ┃ r === True
107 ┃ assert $ isJust us
108 ┃ (length <$> us) === Just 1
┃ ^^^^^^^^^^^^^^^^^^^^^^^^^^
┃ │ Failed (- lhs =/= + rhs)
┃ │ - Just 0
┃ │ + Just 1
┏━━ tst/test-01.hs ━━━
116 ┃ prop_seq :: Property
117 ┃ prop_seq = property $ do
118 ┃ actions <- forAll $ Gen.sequential (Range.linear 1 1000) initialState [addUser, deleteUser, getUser]
┃ │ Var 0 = AddUser ""
┃ │ Var 2 = AddUser ""
┃ │ Var 5 = AddUser ""
┃ │ Var 7 = AddUser ""
┃ │ Var 9 = AddUser ""
┃ │ Var 11 = AddUser ""
┃ │ Var 20 = AddUser ""
┃ │ Var 28 = AddUser ""
┃ │ Var 30 = AddUser ""
┃ │ Var 32 = AddUser ""
┃ │ Var 33 = AddUser ""
┃ │ Var 34 = AddUser ""
┃ │ Var 37 = AddUser ""
┃ │ Var 38 = AddUser ""
┃ │ Var 41 = AddUser ""
┃ │ Var 45 = AddUser ""
┃ │ Var 47 = GetUser 15
119 ┃ resetWS
120 ┃ executeSequential initialState actions
This failure can be reproduced by running:
> recheck (Size 12) (Seed 2976784816810995551 (-47094630645854485)) sequential
✗ 1 failed.
</pre>
<p>
That is, after inserting 16 users, we don't see any user when trying to get that
16th user (<i>User ID</i> 15). That's a proper bug in the server.
</p>
<p>
As a matter of fact, this is the bug I put into the server and was hoping to
find. In particular, I wanted <i>hedgehog</i> to find the minimal sequence leading to
this bug.<sup><a id="fnr.1" class="footref" href="#fn.1">1</a></sup> Which it clearly has!
</p>
</div>
</div>
<div id="footnotes">
<h2 class="footnotes">Footnotes: </h2>
<div id="text-footnotes">
<div class="footdef"><sup><a id="fn.1" class="footnum" href="#fnr.1">1</a></sup> <div class="footpara"><p class="footpara">
If you recall from the <a href="2019-05-30-000-hedgehog-on-a-rest-api.html">previous post</a>, I was interested in the integrated
shrinking offered by <i>hedgehog</i>.
</p></div></div>
</div>
</div><div class="taglist"><a href="https://magnus.therning.org/tags.html">Tags</a>: <a href="https://magnus.therning.org/tag-haskell.html">haskell</a> <a href="https://magnus.therning.org/tag-hedgehog.html">hedgehog</a> <a href="https://magnus.therning.org/tag-testing.html">testing</a> </div></div>
<div id="postamble" class="status"><!-- org-static-blog-page-postamble --></div>
</body>
</html>