From 28df5baf6cfd0a250e02f7aa3a1a4e1ffc8d101d Mon Sep 17 00:00:00 2001 From: "Julian K. Arni" Date: Fri, 13 May 2016 14:56:09 +0200 Subject: [PATCH] Start more extensive tests --- servant-ekg.cabal | 17 +++++++----- stack.yaml | 16 +++--------- test/{test.hs => Servant/EkgSpec.hs} | 39 +++++++++++++++++++++++----- test/Spec.hs | 1 + 4 files changed, 47 insertions(+), 26 deletions(-) rename test/{test.hs => Servant/EkgSpec.hs} (66%) create mode 100644 test/Spec.hs diff --git a/servant-ekg.cabal b/servant-ekg.cabal index 32b7cab..95a7c64 100644 --- a/servant-ekg.cabal +++ b/servant-ekg.cabal @@ -1,25 +1,25 @@ name: servant-ekg -version: 0.1.0.0 +version: 0.2.0.0 synopsis: Helpers for using ekg with servant description: Helpers for using ekg with servant license: BSD3 license-file: LICENSE author: Anchor Engineering -maintainer: Anchor Engineering +maintainer: Servant Contributors category: System build-type: Simple cabal-version: >=1.10 source-repository HEAD type: git - location: https://github.com/anchor/servant-ekg.git + location: https://github.com/servant/servant-ekg.git library exposed-modules: Servant.Ekg hs-source-dirs: lib build-depends: base >=4.7 && <4.9 , ekg-core - , servant == 0.7.* + , servant > 0.5 && < 0.8 , http-types , text , time @@ -32,13 +32,18 @@ test-suite spec ghc-options: -Wall default-language: Haskell2010 hs-source-dirs: test - main-is: test.hs + main-is: Spec.hs build-depends: base == 4.* , aeson , ekg , ekg-core , servant-ekg , servant-server + , servant-client + , http-client , text , wai - , warp + , warp >= 3.2.4 && < 3.3 + , hspec == 2.* + , unordered-containers + , transformers diff --git a/stack.yaml b/stack.yaml index 89e083b..7b07350 100644 --- a/stack.yaml +++ b/stack.yaml @@ -1,16 +1,3 @@ -# This file was automatically generated by 'stack init' -# -# Some commonly used options have been documented as comments in this file. -# For advanced use and comprehensive documentation of the format, please see: -# http://docs.haskellstack.org/en/stable/yaml_configuration/ - -# A warning or info to be displayed to the user on config load. -user-message: ! 'Warning (added by new or init): Specified resolver could not satisfy - all dependencies. Some external packages have been added as dependencies. - - You can suppress this message by removing it from stack.yaml - -' # Resolver to choose a 'specific' stackage snapshot or a compiler version. # A snapshot resolver dictates the compiler version and the set of packages @@ -50,6 +37,9 @@ packages: extra-deps: - servant-0.7.1 - servant-server-0.7.1 +- servant-client-0.7.1 +- warp-3.2.6 +- http2-1.6.0 # Override default flag values for local packages and extra-deps flags: {} diff --git a/test/test.hs b/test/Servant/EkgSpec.hs similarity index 66% rename from test/test.hs rename to test/Servant/EkgSpec.hs index 0a0ab3a..a78ea6e 100644 --- a/test/test.hs +++ b/test/Servant/EkgSpec.hs @@ -6,19 +6,46 @@ {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} +module Servant.EkgSpec (spec) where + import Control.Concurrent +import Control.Monad.Trans.Except import Data.Aeson import Data.Monoid import Data.Proxy +import qualified Data.HashMap.Strict as H import Data.Text import GHC.Generics +import Network.HTTP.Client (defaultManagerSettings, newManager) import Network.Wai import Network.Wai.Handler.Warp import Servant +import Servant.Client import System.Metrics +import qualified System.Metrics.Counter as Counter +import Test.Hspec import Servant.Ekg + +-- * Spec + +spec :: Spec +spec = describe "servant-ekg" $ do + + let getEp :<|> postEp :<|> deleteEp = client testApi + + it "collects GET data" $ do + withApp $ \port mvar -> do + mgr <- newManager defaultManagerSettings + result <- runExceptT $ getEp "name" Nothing mgr (BaseUrl Http "localhost" port "") + m <- readMVar mvar + print $ H.keys m + case H.lookup "hello.:name.GET" m of + Nothing -> fail "Expected some value" + Just v -> Counter.read (metersC2XX v) `shouldReturn` 1 + + -- * Example -- | A greet message data type @@ -65,10 +92,8 @@ server = helloH :<|> postGreetH :<|> deleteGreetH test :: Application test = serve testApi server --- Put this all to work! -main :: IO () -main = do - ekg <- newStore - ms <- newMVar mempty - _ <- forkIO $ run 8001 $ monitorEndpoints testApi ekg ms test - return () +withApp :: (Port -> MVar (H.HashMap Text Meters) -> IO a) -> IO a +withApp a = do + ekg <- newStore + ms <- newMVar mempty + withApplication (return $ monitorEndpoints testApi ekg ms test) $ \p -> a p ms diff --git a/test/Spec.hs b/test/Spec.hs new file mode 100644 index 0000000..a824f8c --- /dev/null +++ b/test/Spec.hs @@ -0,0 +1 @@ +{-# OPTIONS_GHC -F -pgmF hspec-discover #-}