@@ -6,6 +6,7 @@ module RunSpec (main, spec, withApp, connectTo) where
66
77import Control.Concurrent (forkIO, killThread, threadDelay)
88import Control.Concurrent.MVar (newEmptyMVar, takeMVar, putMVar)
9+import Control.Concurrent.STM
910import qualified Control.Exception as E
1011import Control.Exception.Lifted (bracket, try, IOException, onException)
1112import Control.Monad (forM_, replicateM_, unless)
@@ -264,9 +265,11 @@ spec = do
264265 ]
265266 it "lots of chunks" $ do
266267 ifront <- I.newIORef id
268+ countVar <- newTVarIO (0 :: Int)
267269 let app req f = do
268270 bss <- consumeBody $ requestBody req
269271 I.atomicModifyIORef ifront $ \front -> (front . (S.concat bss:), ())
272+ atomically $ modifyTVar countVar (+ 1)
270273 f $ responseLBS status200 [] ""
271274 withApp defaultSettings app $ \port -> do
272275 handle <- connectTo "127.0.0.1" port
@@ -276,14 +279,21 @@ spec = do
276279 ["0\r\n\r\n"]
277280 mapM_ (\bs -> hPutStr handle bs >> hFlush handle) input
278281 hClose handle
279− threadDelay 100000 -- FIXME why does this delay need to be so high?
282+ atomically $ do
283+ count <- readTVar countVar
284+ check $ count == 2
280285 front <- I.readIORef ifront
281286 front [] `shouldBe` replicate 2 (S.concat $ replicate 50 "12345")
287+-- For some reason, the following test on Windows causes the socket
288+-- to be killed prematurely. Worth investigating in the future if possible.
289+#if !WINDOWS
282290 it "in chunks" $ do
283291 ifront <- I.newIORef id
292+ countVar <- newTVarIO (0 :: Int)
284293 let app req f = do
285294 bss <- consumeBody $ requestBody req
286295 liftIO $ I.atomicModifyIORef ifront $ \front -> (front . (S.concat bss:), ())
296+ atomically $ modifyTVar countVar (+ 1)
287297 f $ responseLBS status200 [] ""
288298 withApp defaultSettings app $ \port -> do
289299 handle <- connectTo "127.0.0.1" port
@@ -295,12 +305,15 @@ spec = do
295305 ]
296306 mapM_ (\bs -> hPutStr handle bs >> hFlush handle) $ map S.singleton $ S.unpack input
297307 hClose handle
298− threadDelay 1000
308+ atomically $ do
309+ count <- readTVar countVar
310+ check $ count == 2
299311 front <- I.readIORef ifront
300312 front [] `shouldBe`
301313 [ "Hello World\nBye"
302314 , "Hello World"
303315 ]
316+#endif
304317 it "timeout in request body" $ do
305318 ifront <- I.newIORef id
306319 let app req f = do