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
|
{-# LANGUAGE BangPatterns #-}
module ThreadSpec where
import Control.Concurrent (threadDelay)
import Data.IORef (readIORef, writeIORef, atomicModifyIORef)
import Network.Wai.Handler.Warp.Thread
import Test.Hspec
main :: IO ()
main = hspec spec
spec :: Spec
spec = describe "forkIOwithBreakableForever" $ do
it "can be breakable" $ do
ref' <- forkIOwithBreakableForever True $ \ref -> do
threadDelay 1000
!_ <- atomicModifyIORef ref (\x -> (False, x))
return ()
threadDelay 100000
readIORef ref' `shouldReturn` False
_ <- breakForever ref'
threadDelay 100000
writeIORef ref' True
threadDelay 100000
readIORef ref' `shouldReturn` True
|