File: JobQueueSpec.hs

package info (click to toggle)
haskell-hspec-core 2.11.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 656 kB
  • sloc: haskell: 8,945; makefile: 5
file content (44 lines) | stat: -rw-r--r-- 1,323 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
module Test.Hspec.Core.Runner.JobQueueSpec (spec) where

import           Prelude ()
import           Helper

import           Control.Concurrent

import           Test.Hspec.Core.Runner.JobQueue

spec :: Spec
spec = do
  describe "enqueueJob" $ do
    let
      waitFor job = job (\ _ -> pass) >>= either throwIO return

    context "with Sequential" $ do
      it "runs actions sequentially" $ do
        withJobQueue 10 $ \ queue -> do
          ref <- newIORef []
          jobA <- enqueueJob queue Sequential $ \ _ -> modifyIORef ref (23 :)
          jobB <- enqueueJob queue Sequential $ \ _ -> modifyIORef ref (42 :)
          waitFor jobB
          readIORef ref `shouldReturn` [42 :: Int]
          waitFor jobA
          readIORef ref `shouldReturn` [23, 42]

    context "with Concurrent" $ do
      it "runs actions concurrently" $ do
        withJobQueue 10 $ \ queue -> do
          barrierA <- newEmptyMVar
          barrierB <- newEmptyMVar

          jobA <- enqueueJob queue Concurrent $ \ _ -> do
            putMVar barrierB ()
            takeMVar barrierA

          jobB <- enqueueJob queue Concurrent $ \ _ -> do
            putMVar barrierA ()
            takeMVar barrierB

          timeout (0.1 :: Seconds) $ do
            waitFor jobA
            waitFor jobB
          `shouldReturn` Just ()