chore(tests): fix tests

This commit is contained in:
Gregor Kleen 2019-07-24 11:12:47 +02:00
parent d1abe530b6
commit da59a2f9da
3 changed files with 24 additions and 27 deletions

View File

@ -478,7 +478,7 @@ getApplicationRepl = do
return (getPort wsettings, foundation, app1) return (getPort wsettings, foundation, app1)
shutdownApp :: MonadIO m => UniWorX -> m () shutdownApp :: (MonadIO m, MonadBaseControl IO m) => UniWorX -> m ()
shutdownApp app = do shutdownApp app = do
stopJobCtl app stopJobCtl app
liftIO $ do liftIO $ do

View File

@ -113,16 +113,14 @@ manageJobPool, manageCrontab :: forall m.
=> UniWorX -> m () => UniWorX -> m ()
manageCrontab foundation@UniWorX{..} = do manageCrontab foundation@UniWorX{..} = do
context <- atomically . fmap jobContext $ readTMVar appJobState context <- atomically . fmap jobContext $ readTMVar appJobState
liftIO . unsafeHandler foundation . void $ do let awaitTermination = atomically $ do
shouldTerminate <- readTMVar appJobState >>= fmap not . isEmptyTMVar . jobShutdown
guard shouldTerminate
liftIO . race_ awaitTermination . unsafeHandler foundation . void $ do
atomically . assertM_ (not . Map.null . jobWorkers) $ readTMVar appJobState
runReaderT ?? foundation $ runReaderT ?? foundation $
writeJobCtlBlock JobCtlDetermineCrontab writeJobCtlBlock JobCtlDetermineCrontab
evalRWST execCrontab' context HashMap.empty evalRWST (forever execCrontab) context HashMap.empty
where
execCrontab' = do
shouldTerminate <- atomically $ readTMVar appJobState >>= fmap not . isEmptyTMVar . jobShutdown
if
| shouldTerminate -> return ()
| otherwise -> execCrontab *> execCrontab'
manageJobPool foundation@UniWorX{..} manageJobPool foundation@UniWorX{..}
@ -158,6 +156,7 @@ manageJobPool foundation@UniWorX{..}
runWorker = unsafeHandler foundation . flip runReaderT (jobContext oldState) $ do runWorker = unsafeHandler foundation . flip runReaderT (jobContext oldState) $ do
$logInfoS logIdent "Started" $logInfoS logIdent "Started"
runConduit $ streamChan .| handleJobs' workerId runConduit $ streamChan .| handleJobs' workerId
$logInfoS logIdent "Stopped"
worker <- allocateLinkedAsync runWorker worker <- allocateLinkedAsync runWorker
tell . Endo $ \cSt -> cSt tell . Endo $ \cSt -> cSt
@ -196,18 +195,18 @@ manageJobPool foundation@UniWorX{..}
$logInfoS "JobPoolManager" "Shutting down" $logInfoS "JobPoolManager" "Shutting down"
terminate () terminate ()
stopJobCtl :: MonadIO m => UniWorX -> m () stopJobCtl :: (MonadIO m, MonadBaseControl IO m) => UniWorX -> m ()
-- ^ Stop all worker threads currently running -- ^ Stop all worker threads currently running
stopJobCtl UniWorX{appJobState} = do stopJobCtl UniWorX{appJobState} = do
atomically $ do didStop <- atomically $ do
JobState{..} <- readTMVar appJobState jState <- tryReadTMVar appJobState
putTMVar jobShutdown () for jState $ \jSt'@JobState{jobShutdown} -> jSt' <$ tryPutTMVar jobShutdown ()
atomically $ do whenIsJust didStop $ \jSt' -> void . fork . atomically $ do
JobState{..} <- takeTMVar appJobState workers <- maybe [] (Map.keys . jobWorkers) <$> tryTakeTMVar appJobState
mapM_ (void . waitCatchSTM) $ mapM_ (void . waitCatchSTM) $
[ jobPoolManager [ jobPoolManager jSt'
, jobCron , jobCron jSt'
] ++ Map.keys jobWorkers ] ++ workers
execCrontab :: RWST JobContext () (HashMap JobCtl (Max UTCTime)) (HandlerT UniWorX IO) () execCrontab :: RWST JobContext () (HashMap JobCtl (Max UTCTime)) (HandlerT UniWorX IO) ()
-- ^ Keeping a `HashMap` of the latest execution times of `JobCtl`s we have -- ^ Keeping a `HashMap` of the latest execution times of `JobCtl`s we have

View File

@ -3,7 +3,7 @@ module TestImport
, module X , module X
) where ) where
import Application (makeFoundation, makeLogWare) import Application (makeFoundation, makeLogWare, shutdownApp)
import ClassyPrelude as X hiding (delete, deleteBy, Handler, Index, (<.>), (<|), index, uncons, unsnoc, cons, snoc) import ClassyPrelude as X hiding (delete, deleteBy, Handler, Index, (<.>), (<|), index, uncons, unsnoc, cons, snoc)
import Database.Persist as X hiding (get) import Database.Persist as X hiding (get)
import Database.Persist.Sql as X (SqlPersistM) import Database.Persist.Sql as X (SqlPersistM)
@ -31,7 +31,7 @@ import Test.QuickCheck.Classes.Binary as X
import Data.Proxy as X import Data.Proxy as X
import Data.UUID as X (UUID) import Data.UUID as X (UUID)
import System.IO as X (hPrint, hPutStrLn, stderr) import System.IO as X (hPrint, hPutStrLn, stderr)
import Jobs (handleJobs, stopJobCtl) import Jobs (handleJobs)
import Numeric.Natural as X import Numeric.Natural as X
import Control.Lens as X hiding ((<.), elements) import Control.Lens as X hiding ((<.), elements)
@ -42,7 +42,6 @@ import Database (truncateDb)
import Database as X (fillDb) import Database as X (fillDb)
import Control.Monad.Trans.Resource (runResourceT, MonadResourceBase) import Control.Monad.Trans.Resource (runResourceT, MonadResourceBase)
import Data.Pool (destroyAllResources)
import Settings import Settings
@ -51,6 +50,8 @@ import qualified Data.CaseInsensitive as CI
import Data.Typeable import Data.Typeable
import Handler.Utils (runAppLoggingT)
runDB :: SqlPersistM a -> YesodExample UniWorX a runDB :: SqlPersistM a -> YesodExample UniWorX a
runDB query = do runDB query = do
@ -74,13 +75,10 @@ withApp = around $ \act -> runResourceT $ do
[] []
useEnv useEnv
foundation <- makeFoundation settings foundation <- makeFoundation settings
let wipeDB foundation
stopDBAccess = do runAppLoggingT foundation $ handleJobs foundation
stopJobCtl foundation
liftIO . destroyAllResources $ appConnPool foundation
bracket_ stopDBAccess (handleJobs foundation) $ wipeDB foundation
logWare <- makeLogWare foundation logWare <- makeLogWare foundation
lift $ act (foundation, logWare) lift $ act (foundation, logWare) `finally` shutdownApp foundation
-- This function will truncate all of the tables in your database. -- This function will truncate all of the tables in your database.
-- 'withApp' calls it before each test, creating a clean environment for each -- 'withApp' calls it before each test, creating a clean environment for each