From 7f0191098e5e24aecc877da4b1b5c27252728c04 Mon Sep 17 00:00:00 2001 From: NikolajDanger Date: Wed, 16 Oct 2024 16:54:24 +0200 Subject: [PATCH] :mailbox_with_mail: nightmare nightmare nightmare nightmare nightmare nightmare nightmare nightmare nightmare nightmare nightmare nightmare nightmare nightmare nightmare nightmare nightmare nightmare nightmare nightmare nightmare nightmare nightmare nightmare --- a6/src/SPC.hs | 16 ++++------------ a6/src/SPC_Tests.hs | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/a6/src/SPC.hs b/a6/src/SPC.hs index 988dbf5..bbfd428 100644 --- a/a6/src/SPC.hs +++ b/a6/src/SPC.hs @@ -95,7 +95,7 @@ type WorkerName = String -- processes spawned by the workes. data WorkerMsg = -- | New job time - MsgStartJob (IO ()) JobId + MsgStartJob SPC (IO ()) JobId -- Messages sent to SPC. data SPCMsg @@ -173,12 +173,6 @@ io m = SPCM $ \state -> do runSPCM :: SPCState -> SPCM a -> IO a runSPCM state (SPCM f) = fst <$> f state -schedule :: SPCM () -schedule = undefined - -jobDone :: JobId -> JobDoneReason -> SPCM () -jobDone = undefined - workerIsIdle :: WorkerName -> Worker -> SPCM () workerIsIdle = undefined @@ -188,13 +182,9 @@ workerIsGone = undefined checkTimeouts :: SPCM () checkTimeouts = pure () -- change in Task 4 -workerExists :: WorkerName -> SPCM Bool -workerExists = undefined - handleMsg :: Chan SPCMsg -> SPCM () handleMsg c = do checkTimeouts - schedule msg <- io $ receive c case msg of MsgJobAdd job rsvp -> do @@ -307,7 +297,9 @@ workerLoop name c = do msg <- receive c case msg of -- stuff happening here - MsgStartJob action jobid -> -- do stuff + MsgStartJob (SPC sc) action jobid -> do + action + sendTo sc $ MsgJobDone jobid -- | Shut down a running worker. No effect if the worker is already -- terminated. diff --git a/a6/src/SPC_Tests.hs b/a6/src/SPC_Tests.hs index 113d738..6969456 100644 --- a/a6/src/SPC_Tests.hs +++ b/a6/src/SPC_Tests.hs @@ -4,12 +4,43 @@ import Control.Concurrent (threadDelay) import Control.Monad (forM, forM_, replicateM) import Data.IORef import SPC +import GenServer import Test.Tasty (TestTree, localOption, mkTimeout, testGroup) import Test.Tasty.HUnit (assertFailure, testCase, (@?=)) +import Data.Either (isRight) tests :: TestTree tests = localOption (mkTimeout 3000000) $ testGroup "SPC (core)" - [] + [ + testCase "worker-add" $ do + spc <- startSPC + w <- workerAdd spc "Nikolaj" + isRight w @?= True, + testCase "worker-add-2" $ do + spc <- startSPC + _ <- workerAdd spc "Nikolaj" + w <- workerAdd spc "Alba" + isRight w @?= True, + testCase "worker-add-3" $ do + spc <- startSPC + _ <- workerAdd spc "Nikolaj" + _ <- workerAdd spc "Alba" + w <- workerAdd spc "Sebastian" + isRight w @?= True, + testCase "worker-add-2-fail" $ do + spc <- startSPC + _ <- workerAdd spc "Nikolaj" + w <- workerAdd spc "Nikolaj" + isRight w @?= False, + testCase "worker-job" $ do + spc <- startSPC + w <- workerAdd spc "Nikolaj" + ref <- newIORef False + case spc of (SPC c) -> sendTo c (writeIORef ref True) 1 + isRight w @?= True + v <- readIORef ref + v @?= True + ]