packages feed

hspec-slow (empty) → 0.1.0.0

raw patch · 5 files changed

+155/−0 lines, 5 filesdep +basedep +hspecdep +hspec-slowsetup-changed

Dependencies added: base, hspec, hspec-slow, mtl, stm, time, transformers

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Author name here (c) 2016++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Author name here nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ hspec-slow.cabal view
@@ -0,0 +1,41 @@+name:                hspec-slow+version:             0.1.0.0+synopsis:            Find slow test cases+description:         Records and prints out slow Hspec tests+homepage:            https://github.com/bobjflong/hspec-slow#readme+license:             BSD3+license-file:        LICENSE+author:              Bob Long+maintainer:          robertjflong@gmail.com+copyright:           2016 Bob Long+category:            Testing+build-type:          Simple+-- extra-source-files:+cabal-version:       >=1.10++library+  hs-source-dirs:      src+  exposed-modules:     Test.Hspec.Slow+  build-depends:       base >= 4.7 && < 5+                       , stm+                       , time+                       , transformers+                       , mtl+                       , hspec+  default-language:    Haskell2010++test-suite hspec-slow-test+  type:                exitcode-stdio-1.0+  hs-source-dirs:      test+  main-is:             Spec.hs+  build-depends:       base+                     , hspec-slow+                     , hspec+                     , stm+                     , mtl+  ghc-options:         -threaded -rtsopts -with-rtsopts=-N+  default-language:    Haskell2010++source-repository head+  type:     git+  location: https://github.com/bobjflong/hspec-slow
+ src/Test/Hspec/Slow.hs view
@@ -0,0 +1,62 @@+{-# LANGUAGE BangPatterns #-}+{-# LANGUAGE RankNTypes   #-}++module Test.Hspec.Slow (+    configure,+    timedHspec,+    timedHspecParallel+  ) where++import           Control.Concurrent.STM.TVar+import           Control.Monad.IO.Class+import           Control.Monad.Reader+import           Control.Monad.STM+import           Data.Time.Clock+import           Test.Hspec++type SlowResults = [(String, NominalDiffTime)]+type SlowResultTracker = TVar SlowResults++data SlowConfiguration = SlowConfiguration {+  duration :: Int,+  tracker  :: SlowResultTracker+}++configure :: Int -> IO SlowConfiguration+configure x =+  newTVarIO [] >>= \t ->+    return (SlowConfiguration x t)++stopwatch :: MonadIO m => m a -> m (a, NominalDiffTime)+stopwatch x = do+  start <- liftIO getCurrentTime+  !a <- x+  end <- liftIO getCurrentTime+  return (a, end `diffUTCTime` start)++trackedAction :: MonadIO m => String -> m a -> ReaderT SlowConfiguration m a+trackedAction s m = do+  conf <- ask+  (result, d) <- lift (stopwatch m)+  if d > (realToFrac . duration $ conf)+    then do+      liftIO $ atomically $ modifyTVar (tracker conf) (++ [(s, d)])+      return result+    else return result++type Timer = forall m a. (MonadIO m, Example (m a)) => String -> m a -> SpecWith (Arg (m a))++timed :: SlowConfiguration -> Timer+timed c s a = it s $ runReaderT (trackedAction s a) c++slowReport :: (MonadIO m) => SlowConfiguration -> m ()+slowReport s = do+  slows <- liftIO $ readTVarIO (tracker s)+  liftIO $ putStrLn "Slow examples:"+  liftIO $ mapM_ (\(t, v) -> putStrLn $ show v ++ ": " ++ t) slows++timedHspec :: SlowConfiguration -> (Timer -> SpecWith ()) -> IO ()+timedHspec t x = hspec $ (afterAll_ . slowReport) t $ x (timed t)++timedHspecParallel :: SlowConfiguration -> (Timer -> SpecWith ()) -> IO ()+timedHspecParallel t x = hspec $ (afterAll_ . slowReport) t $ parallel $ x (timed t)
+ test/Spec.hs view
@@ -0,0 +1,20 @@+import           Control.Concurrent+import           Control.Concurrent.STM.TVar+import           Control.Monad.Reader+import           Test.Hspec+import           Test.Hspec.Slow++main :: IO ()+main = do+  conf <- configure 1+  timedHspecParallel conf $ \it ->+    describe "Main" $ do+      it "should foo" $ do+        threadDelay 3000000+        1 `shouldBe` 1+      it "should bar" $ do+        threadDelay 1000+        1 `shouldBe` 1+      it "should baz" $ do+        threadDelay 4000000+        1 `shouldBe` 1