Commit 3435b69d authored by Alfredo Di Napoli's avatar Alfredo Di Napoli

Rewrite testPrios test

This commit partially rewrites the `testPrios` queue test, in such a way
that it's now easier to inspect the final schedule and the occurrence of events.
Albeit not perfect, it should be less sensitive to random failures,
like the ones we have been observing on CI.
parent 3f90e8bb
...@@ -12,9 +12,10 @@ import Control.Exception ...@@ -12,9 +12,10 @@ import Control.Exception
import Control.Monad import Control.Monad
import Control.Monad.Reader import Control.Monad.Reader
import Control.Monad.Except import Control.Monad.Except
import Data.Maybe
import Data.Either import Data.Either
import Data.List import Data.List
import Data.Sequence (Seq) import Data.Sequence (Seq, (|>), fromList)
import GHC.Stack import GHC.Stack
import Prelude import Prelude
import System.IO.Unsafe import System.IO.Unsafe
...@@ -33,7 +34,20 @@ import Gargantext.API.Prelude ...@@ -33,7 +34,20 @@ import Gargantext.API.Prelude
import Gargantext.API.Admin.EnvTypes as EnvTypes import Gargantext.API.Admin.EnvTypes as EnvTypes
import Gargantext.API.Admin.Orchestrator.Types import Gargantext.API.Admin.Orchestrator.Types
data JobT = A | B deriving (Eq, Ord, Show, Enum, Bounded) data JobT = A
| B
| C
| D
deriving (Eq, Ord, Show, Enum, Bounded)
-- | This type models the schedule picked up by the orchestrator.
newtype JobSchedule = JobSchedule { _JobSchedule :: Seq JobT } deriving (Eq, Show)
addJobToSchedule :: JobT -> MVar JobSchedule -> IO ()
addJobToSchedule jobt mvar = do
modifyMVar_ mvar $ \js -> do
let js' = js { _JobSchedule = _JobSchedule js |> jobt }
pure js'
data Counts = Counts { countAs :: Int, countBs :: Int } data Counts = Counts { countAs :: Int, countBs :: Int }
deriving (Eq, Show) deriving (Eq, Show)
...@@ -41,8 +55,12 @@ data Counts = Counts { countAs :: Int, countBs :: Int } ...@@ -41,8 +55,12 @@ data Counts = Counts { countAs :: Int, countBs :: Int }
inc, dec :: JobT -> Counts -> Counts inc, dec :: JobT -> Counts -> Counts
inc A cs = cs { countAs = countAs cs + 1 } inc A cs = cs { countAs = countAs cs + 1 }
inc B cs = cs { countBs = countBs cs + 1 } inc B cs = cs { countBs = countBs cs + 1 }
inc C cs = cs
inc D cs = cs
dec A cs = cs { countAs = countAs cs - 1 } dec A cs = cs { countAs = countAs cs - 1 }
dec B cs = cs { countBs = countBs cs - 1 } dec B cs = cs { countBs = countBs cs - 1 }
dec C cs = cs
dec D cs = cs
jobDuration, initialDelay :: Int jobDuration, initialDelay :: Int
jobDuration = 100000 jobDuration = 100000
...@@ -75,29 +93,29 @@ testPrios :: IO () ...@@ -75,29 +93,29 @@ testPrios :: IO ()
testPrios = do testPrios = do
k <- genSecret k <- genSecret
let settings = defaultJobSettings 2 k let settings = defaultJobSettings 2 k
prios = [(B, 10), (C, 1), (D, 5)]
runningDelta job = fromMaybe 0 (lookup job prios) * 1000
st :: JobsState JobT [String] () <- newJobsState settings $ st :: JobsState JobT [String] () <- newJobsState settings $
applyPrios [(B, 10)] defaultPrios -- B has higher priority applyPrios prios defaultPrios -- B has the highest priority
runningJs <- newTVarIO (Counts 0 0) pickedSchedule <- newMVar (JobSchedule mempty)
let j jobt _jHandle _inp _l = do let j jobt _jHandle _inp _l = do
atomically $ modifyTVar runningJs (inc jobt) -- simulate the running time of a job, then add to the schedule.
threadDelay jobDuration -- The running time is proportional to the priority of the job,
atomically $ modifyTVar runningJs (dec jobt) -- to account for the fact that we are pushing jobs sequentially,
-- so we have to our account for the submission time.
threadDelay $ jobDuration - runningDelta jobt
addJobToSchedule jobt pickedSchedule
jobs = [ (A, j A) jobs = [ (A, j A)
, (A, j A) , (C, j C)
, (B, j B)
, (B, j B) , (B, j B)
, (D, j D)
] ]
_jids <- forM jobs $ \(t, f) -> do forM_ jobs $ \(t, f) -> void $ pushJob t () f settings st
pushJob t () f settings st -- wait for the jobs to finish, waiting for more than the total duration,
threadDelay (2*initialDelay) -- so that we are sure that all jobs have finished, then check the schedule.
r1 <- readTVarIO runningJs threadDelay (5 * jobDuration)
r1 `shouldBe` (Counts 0 2) finalSchedule <- readMVar pickedSchedule
threadDelay jobDuration finalSchedule `shouldBe` JobSchedule (fromList [B, D, C, A])
r2 <- readTVarIO runningJs
r2 `shouldBe` (Counts 2 0)
threadDelay jobDuration
r3 <- readTVarIO runningJs
r3 `shouldBe` (Counts 0 0)
testExceptions :: IO () testExceptions :: IO ()
testExceptions = do testExceptions = do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment