@@ -2,17 +2,16 @@
2{-# LANGUAGE FlexibleContexts #-}
2{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE GADTs #-}
3{-# LANGUAGE GADTs #-}
4{-# LANGUAGE LambdaCase #-}
4{-# LANGUAGE LambdaCase #-}
5−{-# LANGUAGE PolyKinds #-}
6{-# LANGUAGE RankNTypes #-}
5{-# LANGUAGE RankNTypes #-}
7{-# LANGUAGE RecordWildCards #-}
6{-# LANGUAGE RecordWildCards #-}
8{-# LANGUAGE ScopedTypeVariables #-}
7{-# LANGUAGE ScopedTypeVariables #-}
9{-# LANGUAGE TupleSections #-}
8{-# LANGUAGE TupleSections #-}
10−
9+{-# LANGUAGE TypeApplications #-}
11{-# LANGUAGE TypeFamilies #-}
10{-# LANGUAGE TypeFamilies #-}
12{-# LANGUAGE UndecidableInstances #-}
11{-# LANGUAGE UndecidableInstances #-}
14module Roboservant.Direct
13module Roboservant.Direct
16 Config (..),
15 Config (..),
17 -- TODO come up with something smarter than exporting all this, we should
16 -- TODO come up with something smarter than exporting all this, we should
18 -- have some nice error-display functions
17 -- have some nice error-display functions
@@ -25,30 +24,25 @@ module Roboservant.Direct
27import Control.Exception.Lifted
26import Control.Exception.Lifted
28− ( Exception,
29+ Exception,
30− SomeAsyncException,
30+ SomeAsyncException,
35− throw,
34+ Handler(Handler) )
37import Control.Monad.State.Strict
35import Control.Monad.State.Strict
40− StateT (runStateT),
36+ ( MonadIO(..), StateT(runStateT), modify', MonadState(get) )
43import Control.Monad.Trans.Control (MonadBaseControl)
37import Control.Monad.Trans.Control (MonadBaseControl)
44import qualified Data.Dependent.Map as DM
38import qualified Data.Dependent.Map as DM
45−import Data.Dynamic (Dynamic (..))
39+import Data.Dynamic ( Dynamic(..) )
46import qualified Data.IntSet as IntSet
40import qualified Data.IntSet as IntSet
47import Data.List.NonEmpty (NonEmpty (..))
41import Data.List.NonEmpty (NonEmpty (..))
48import qualified Data.List.NonEmpty as NEL
42import qualified Data.List.NonEmpty as NEL
49import Data.Maybe (mapMaybe)
43import Data.Maybe (mapMaybe)
50import qualified Data.Set as Set
44import qualified Data.Set as Set
51−import Data.Time.Clock (UTCTime, addUTCTime, getCurrentTime)
45+import Data.Time.Clock ( addUTCTime, getCurrentTime, UTCTime )
52import qualified Data.Vinyl as V
46import qualified Data.Vinyl as V
53import qualified Data.Vinyl.Curry as V
47import qualified Data.Vinyl.Curry as V
54import qualified Data.Vinyl.Functor as V
48import qualified Data.Vinyl.Functor as V
@@ -56,17 +50,16 @@ import GHC.Generics ((:*:) (..))
56import Roboservant.Types
50import Roboservant.Types
57 ( ApiOffset (..),
51 ( ApiOffset (..),
58 Argument (..),
52 Argument (..),
59− InteractionError(..),
53+ FlattenServer (..),
60 Provenance (..),
54 Provenance (..),
62 ReifiedEndpoint (..),
55 ReifiedEndpoint (..),
63 Stash (..),
56 Stash (..),
64 StashValue (..),
57 StashValue (..),
67−import Roboservant.Types.Config
61+import Servant (Endpoints, Proxy (Proxy), Server, ServerError (..))
69−import System.Random (Random (randomR), StdGen, mkStdGen)
62+import System.Random ( mkStdGen, Random(randomR), StdGen )
70import qualified Type.Reflection as R
63import qualified Type.Reflection as R
72data RoboservantException
65data RoboservantException
@@ -93,6 +86,15 @@ data FuzzOp
94 deriving (Show, Eq)
87 deriving (Show, Eq)
91+ { seed :: [(Dynamic, Int)],
92+ maxRuntime :: Double, -- seconds to test for
95+ coverageThreshold :: Double
96data FuzzState
98data FuzzState
97 = FuzzState
99 = FuzzState
98 { path :: [FuzzOp],
100 { path :: [FuzzOp],
@@ -105,7 +107,7 @@ data EndpointOption
105 = forall as.
107 = forall as.
106 (V.RecordToList as, V.RMap as) =>
108 (V.RecordToList as, V.RMap as) =>
107 EndpointOption
109 EndpointOption
108− { eoCall :: V.Curried as (IO (Either InteractionError (NonEmpty (Dynamic, Int)))),
110+ { eoCall :: V.Curried as (IO (Either ServerError (NonEmpty (Dynamic, Int)))),
109 eoArgs :: V.Rec (TypedF StashValue) as
111 eoArgs :: V.Rec (TypedF StashValue) as
@@ -121,18 +123,14 @@ data Report
122 deriving (Show)
124 deriving (Show)
126−-- fuzzClient :: Client api -> Config -> IO (Maybe Report)
127−-- fuzzClient = undefined
132− ReifiedApi ->
127+ forall api.
128+ (FlattenServer api, ToReifiedApi (Endpoints api)) =>
133 Config ->
130 Config ->
134 IO (Maybe Report)
132 IO (Maybe Report)
135−fuzz' reifiedApi Config {..} = handle (pure . Just . formatException) $ do
133+fuzz server Config {..} checker = handle (pure . Just . formatException) $ do
136 let path = []
134 let path = []
137 stash = addToStash seed mempty
135 stash = addToStash seed mempty
138 currentRng = mkStdGen rngSeed
136 currentRng = mkStdGen rngSeed
@@ -141,7 +139,7 @@ fuzz' reifiedApi Config {..} = handle (pure . Just . formatException) $ do
141 runStateT
139 runStateT
142 (untilDone (maxReps, deadline) go <* (evaluateCoverage =<< get))
140 (untilDone (maxReps, deadline) go <* (evaluateCoverage =<< get))
143 FuzzState {..}
141 FuzzState {..}
144− logInfo $ show stopreason
142+ print stopreason
145 pure Nothing
143 pure Nothing
147 -- something less terrible later
145 -- something less terrible later
@@ -150,15 +148,13 @@ fuzz' reifiedApi Config {..} = handle (pure . Just . formatException) $ do
151 (unlines [show failureType, show exception])
149 (unlines [show failureType, show exception])
153− displayDiagnostics FuzzState {..} = liftIO $
151+ displayDiagnostics FuzzState {..} = liftIO $ do
155− ["api endpoints covered"]
152+ putStrLn "api endpoints covered"
156− <> (map show . Set.toList . Set.fromList $ map apiOffset path)
153+ mapM_ print (Set.toList $ Set.fromList $ map apiOffset path)
157− <> ["", "types in stash"]
155+ putStrLn "types in stash"
158− <> DM.foldrWithKey (\_ v r -> (show . NEL.length . getStashValue $ v) : r) [] (getStash stash)
156+ DM.forWithKey_ (getStash stash) $ \_k v ->
159− -- <> (map (show . NEL.length . getStashValue ) $ DM.assocs (getStash stash))
161− -- (show . NEL.length $ getStashValue v))
157+ print (NEL.length $ getStashValue v)
163 evaluateCoverage f@FuzzState {..}
159 evaluateCoverage f@FuzzState {..}
164 | coverage > coverageThreshold = pure ()
160 | coverage > coverageThreshold = pure ()
@@ -178,7 +174,7 @@ fuzz' reifiedApi Config {..} = handle (pure . Just . formatException) $ do
179 _ <- action
175 _ <- action
180 untilDone (n -1, deadline) action
176 untilDone (n -1, deadline) action
181−
177+ reifiedApi = toReifiedApi (flattenServer @api server) (Proxy @(Endpoints api))
182 routeCount = length reifiedApi
178 routeCount = length reifiedApi
183 elementOrFail ::
179 elementOrFail ::
184 (MonadState FuzzState m, MonadIO m) =>
180 (MonadState FuzzState m, MonadIO m) =>
@@ -195,7 +191,7 @@ fuzz' reifiedApi Config {..} = handle (pure . Just . formatException) $ do
195 ( forall as.
191 ( forall as.
196 (V.RecordToList as, V.RMap as) =>
192 (V.RecordToList as, V.RMap as) =>
197 FuzzOp ->
193 FuzzOp ->
198− V.Curried as (IO (Either InteractionError (NonEmpty (Dynamic, Int)))) ->
194+ V.Curried as (IO (Either ServerError (NonEmpty (Dynamic, Int)))) ->
199 V.Rec (TypedF V.Identity) as ->
195 V.Rec (TypedF V.Identity) as ->
@@ -236,24 +232,27 @@ fuzz' reifiedApi Config {..} = handle (pure . Just . formatException) $ do
236 execute ::
232 execute ::
237 (MonadState FuzzState m, MonadIO m, V.RecordToList as, V.RMap as) =>
233 (MonadState FuzzState m, MonadIO m, V.RecordToList as, V.RMap as) =>
238 FuzzOp ->
234 FuzzOp ->
239− V.Curried as (IO (Either InteractionError (NonEmpty (Dynamic, Int)))) ->
235+ V.Curried as (IO (Either ServerError (NonEmpty (Dynamic, Int)))) ->
240 V.Rec (TypedF V.Identity) as ->
236 V.Rec (TypedF V.Identity) as ->
242 execute fuzzop func args = do
238 execute fuzzop func args = do
243− (liftIO . logInfo . show . (fuzzop,) . stash) =<< get
239+ (liftIO . print . (fuzzop,) . stash) =<< get
244 liftIO (V.runcurry' func argVals) >>= \case
240 liftIO (V.runcurry' func argVals) >>= \case
245− Left (e::InteractionError) ->
241+ -- parameterise this
246− if fatalError e
242+ Left (serverError :: ServerError) ->
247− then throw e
243+ case errHTTPCode serverError of
248− else pure ()
244+ 500 -> throw serverError
246+ liftIO $ print ("ignoring non-500 error", serverError)
249− Right (dyn :: NEL.NonEmpty (Dynamic, Int)) ->
247+ Right (dyn :: NEL.NonEmpty (Dynamic, Int)) -> do
248+ -- liftIO $ print ("storing", fmap dynTypeRep dyn)
251− ( \fs@FuzzState {..} ->
250+ ( \fs@FuzzState {..} ->
252− fs {stash = addToStash (NEL.toList dyn) stash}
251+ fs {stash = addToStash (NEL.toList dyn) stash}
255 argVals = V.rmap (\(_ :*: V.Identity x) -> V.Identity x) args
254 argVals = V.rmap (\(_ :*: V.Identity x) -> V.Identity x) args
256− -- argTypes = recordToList' (\(tr :*: _) -> R.SomeTypeRep tr) args
255+ -- argTypes = recordToList' (\(tr :*: _) -> R.SomeTypeRep tr) args
258 (MonadState FuzzState m, MonadIO m, MonadBaseControl IO m) =>
257 (MonadState FuzzState m, MonadIO m, MonadBaseControl IO m) =>
@@ -262,12 +261,13 @@ fuzz' reifiedApi Config {..} = handle (pure . Just . formatException) $ do
262 (execute op func args)
261 (execute op func args)
263 [ Handler (\(e :: SomeAsyncException) -> throw e),
262 [ Handler (\(e :: SomeAsyncException) -> throw e),
265− ( \(e :: SomeException) ->
264+ ( \(e :: SomeException) -> do
265+ displayDiagnostics =<< get
266 throw . RoboservantException ServerCrashed (Just e) =<< get
266 throw . RoboservantException ServerCrashed (Just e) =<< get
270− (liftIO healthCheck)
270+ (liftIO checker)
271 (\(e :: SomeException) -> throw . RoboservantException CheckerFailed (Just e) =<< get)
271 (\(e :: SomeException) -> throw . RoboservantException CheckerFailed (Just e) =<< get)
273addToStash ::
273addToStash ::