import Control.Concurrent.ThrottledLoop import Control.Concurrent import Test.Hspec import Data.Maybe (fromJust) doNothing :: IO () doNothing = return () main :: IO () main = hspec $ do describe "Throttled Loop" $ do it "works as intended on the happy path" $ do threadId <- loop doNothing 1 1 threadDelay $ 10 * 1000 * 1000 killThread threadId it "can work with a count of 0" $ do threadId <- loop doNothing 0 1 threadDelay $ 10 * 1000 * 1000 killThread threadId it "can work with a count and time of 0 and 0" $ do threadId <- loop doNothing 0 0 threadDelay $ 1 * 1000 * 1000 killThread threadId it "can work with a time of 0" $ do threadId <- loop doNothing 1 0 threadDelay $ 1 * 1000 * 1000 killThread threadId it "executes the appropriate number of times on 1 round" $ do ctr <- newMVar 0 let incrCtr = modifyMVar_ ctr $ \x -> return (x+1) threadId <- loop incrCtr 1 10 threadDelay $ 1 * 1000 * 1000 killThread threadId val <- fromJust <$> tryTakeMVar ctr val `shouldBe` (10::Int) it "executes the appropriate number of times on 2 rounds" $ do ctr <- newMVar 0 let incrCtr = modifyMVar_ ctr $ \x -> return (x+1) threadId <- loop incrCtr 1 10 threadDelay $ 2 * 1000 * 1000 killThread threadId val <- fromJust <$> tryTakeMVar ctr val `shouldBe` (20::Int) it "executes the appropriate number of times on 3 rounds" $ do ctr <- newMVar 0 let incrCtr = modifyMVar_ ctr $ \x -> return (x+1) threadId <- loop incrCtr 1 10 threadDelay $ 3 * 1000 * 1000 killThread threadId val <- fromJust <$> tryTakeMVar ctr val `shouldBe` (30::Int)