on master help sign in
using withMySocket
commita77012ebacfaf650b7c40341d26e865add035f1f authorKazu Yamamoto <kazu@iij.ad.jp> dateWed 10 Jul 2019, 16:40 (7 years ago) parent4d102c2
1 file changed+13−25
warp/test/RunSpec.hs+13−25
warp/test/RunSpec.hs+13−25
@@ -78,6 +78,9 @@ connectTo port = do
7878 , msBuffer = ref
7979 }
8080
81+withMySocket :: (MySocket -> IO a) -> Int -> IO a
82+withMySocket body port = bracket (connectTo port) msClose body
83+
8184incr :: MonadIO m => Counter -> m ()
8285incr icount = liftIO $ I.atomicModifyIORef icount $ \ecount ->
8386 ((case ecount of
@@ -154,8 +157,7 @@ runTest :: Int -- ^ expected number of requests
154157 -> IO ()
155158runTest expected app chunks = do
156159 ref <- I.newIORef (Right 0)
157− withApp defaultSettings (app ref) $ \port -> do
158− ms <- connectTo port
160+ withApp defaultSettings (app ref) $ withMySocket $ \ms -> do
159161 forM_ chunks $ \chunk -> msWrite ms chunk
160162 _ <- timeout 100000 $ replicateM_ expected $ msRead ms 4096
161163 res <- I.readIORef ref
@@ -172,10 +174,9 @@ runTerminateTest :: InvalidRequest
172174runTerminateTest expected input = do
173175 ref <- I.newIORef Nothing
174176 let onExc _ = I.writeIORef ref . Just
175− withApp (setOnException onExc defaultSettings) dummyApp $ \port -> do
176− ms <- connectTo port
177+ withApp (setOnException onExc defaultSettings) dummyApp $ withMySocket $ \ms -> do
177178 msWrite ms input
178− msClose ms
179+ msClose ms -- explicitly
179180 threadDelay 1000
180181 res <- I.readIORef ref
181182 show res `shouldBe` show (Just expected)
@@ -246,13 +247,11 @@ spec = do
246247 let app req f = do
247248 liftIO $ I.writeIORef iheaders $ requestHeaders req
248249 f $ responseLBS status200 [] ""
249− withApp defaultSettings app $ \port -> do
250− ms <- connectTo port
250+ withApp defaultSettings app $ withMySocket $ \ms -> do
251251 let input = S.concat
252252 [ "GET / HTTP/1.1\r\nfoo: bar\r\n baz\r\n\tbin\r\n\r\n"
253253 ]
254254 msWrite ms input
255− msClose ms
256255 threadDelay 1000
257256 headers <- I.readIORef iheaders
258257 headers `shouldBe`
@@ -263,13 +262,11 @@ spec = do
263262 let app req f = do
264263 liftIO $ I.writeIORef iheaders $ requestHeaders req
265264 f $ responseLBS status200 [] ""
266− withApp defaultSettings app $ \port -> do
267− ms <- connectTo port
265+ withApp defaultSettings app $ withMySocket $ \ms -> do
268266 let input = S.concat
269267 [ "GET / HTTP/1.1\r\nfoo:bar\r\n\r\n"
270268 ]
271269 msWrite ms input
272− msClose ms
273270 threadDelay 1000
274271 headers <- I.readIORef iheaders
275272 headers `shouldBe`
@@ -285,8 +282,7 @@ spec = do
285282 liftIO $ I.atomicModifyIORef ifront $ \front -> (front . (S.concat bss:), ())
286283 atomically $ modifyTVar countVar (+ 1)
287284 f $ responseLBS status200 [] ""
288− withApp defaultSettings app $ \port -> do
289− ms <- connectTo port
285+ withApp defaultSettings app $ withMySocket $ \ms -> do
290286 let input = S.concat
291287 [ "POST / HTTP/1.1\r\nTransfer-Encoding: Chunked\r\n\r\n"
292288 , "c\r\nHello World\n\r\n3\r\nBye\r\n0\r\n\r\n"
@@ -294,7 +290,6 @@ spec = do
294290 , "b\r\nHello World\r\n0\r\n\r\n"
295291 ]
296292 msWrite ms input
297− msClose ms
298293 atomically $ do
299294 count <- readTVar countVar
300295 check $ count == 2
@@ -311,14 +306,12 @@ spec = do
311306 I.atomicModifyIORef ifront $ \front -> (front . (S.concat bss:), ())
312307 atomically $ modifyTVar countVar (+ 1)
313308 f $ responseLBS status200 [] ""
314− withApp defaultSettings app $ \port -> do
315− ms <- connectTo port
309+ withApp defaultSettings app $ withMySocket $ \ms -> do
316310 let input = concat $ replicate 2 $
317311 ["POST / HTTP/1.1\r\nTransfer-Encoding: Chunked\r\n\r\n"] ++
318312 (replicate 50 "5\r\n12345\r\n") ++
319313 ["0\r\n\r\n"]
320314 mapM_ (msWrite ms) input
321− msClose ms
322315 atomically $ do
323316 count <- readTVar countVar
324317 check $ count == 2
@@ -334,8 +327,7 @@ spec = do
334327 liftIO $ I.atomicModifyIORef ifront $ \front -> (front . (S.concat bss:), ())
335328 atomically $ modifyTVar countVar (+ 1)
336329 f $ responseLBS status200 [] ""
337− withApp defaultSettings app $ \port -> do
338− ms <- connectTo port
330+ withApp defaultSettings app $ withMySocket $ \ms -> do
339331 let input = S.concat
340332 [ "POST / HTTP/1.1\r\nTransfer-Encoding: Chunked\r\n\r\n"
341333 , "c\r\nHello World\n\r\n3\r\nBye\r\n0\r\n"
@@ -343,7 +335,6 @@ spec = do
343335 , "b\r\nHello World\r\n0\r\n\r\n"
344336 ]
345337 mapM_ (msWrite ms) $ map S.singleton $ S.unpack input
346− msClose ms
347338 atomically $ do
348339 count <- readTVar countVar
349340 check $ count == 2
@@ -364,11 +355,10 @@ spec = do
364355 E.throwIO (e :: E.SomeException)
365356 liftIO $ I.atomicModifyIORef ifront $ \front -> (front . (S.concat bss:), ())
366357 f $ responseLBS status200 [] ""
367− withApp (setTimeout 1 defaultSettings) app $ \port -> do
358+ withApp (setTimeout 1 defaultSettings) app $ withMySocket $ \ms -> do
368359 let bs1 = S.replicate 2048 88
369360 bs2 = "This is short"
370361 bs = S.append bs1 bs2
371− ms <- connectTo port
372362 msWrite ms "POST / HTTP/1.1\r\n"
373363 msWrite ms "content-length: "
374364 msWrite ms $ S8.pack $ show $ S.length bs
@@ -377,7 +367,6 @@ spec = do
377367 msWrite ms bs1
378368 threadDelay 100000
379369 msWrite ms bs2
380− msClose ms
381370 threadDelay 5000000
382371 front <- I.readIORef ifront
383372 S.concat (front []) `shouldBe` bs
@@ -393,8 +382,7 @@ spec = do
393382 loop
394383 loop
395384 doubleBS = S.concatMap $ \w -> S.pack [w, w]
396− withApp defaultSettings app $ \port -> do
397− ms <- connectTo port
385+ withApp defaultSettings app $ withMySocket $ \ms -> do
398386 msWrite ms "POST / HTTP/1.1\r\n\r\n12345"
399387 timeout 100000 (msRead ms 10) >>= (`shouldBe` Just "1122334455")
400388 msWrite ms "67890"