File: test.hs

package info (click to toggle)
haskell-filelock 0.1.1.7-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 92 kB
  • sloc: haskell: 151; makefile: 4
file content (91 lines) | stat: -rw-r--r-- 2,512 bytes parent folder | download | duplicates (3)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
{-# LANGUAGE ViewPatterns #-}

import Control.Monad
import Control.Concurrent
import Control.Concurrent.Async
import Data.Maybe
import System.Environment
import System.Exit
import System.Process
import System.IO

import System.FileLock

main :: IO ()
main = do
  hSetBuffering stdout LineBuffering
  args <- getArgs
  case args of
    ["shared", read -> duration]
      -> holdLock "shared" Shared duration
    ["exclusive", read -> duration]
      -> holdLock "exclusive" Exclusive duration
    ["try"]
      -> tryTakingLock
    ["tryshared", read -> duration]
      -> tryHoldLock "shared" Shared duration
    ["tryexclusive", read -> duration]
      -> tryHoldLock "exclusive" Exclusive duration
    _ -> do
      withFile "lock.log" WriteMode $ \h ->
        void $ mapConcurrently id
          [ callSelf h ["shared", "5"]
          , callSelf h ["shared", "2"]
          , sleep 1 >> callSelf h ["exclusive", "3"]
          , sleep 3 >> callSelf h ["try"]
          , sleep 4 >> callSelf h ["shared", "1"]
          , sleep 6 >> callSelf h ["shared", "1"]
          , sleep 10 >> callSelf h ["try"]
          ]
      sleep 11
      log <- readFile "lock.log"
      expected <- readFile "tests/lock.log.expected"
      when (log /= expected) $ do
        putStrLn "log mismatch!"
        putStrLn "log:"
        putStrLn log
        putStrLn "expected:"
        putStrLn expected
        exitFailure

callSelf :: Handle -> [String] -> IO ()
callSelf out args = do
  self <- getExecutablePath
  (_hin, _hout, _herr, ph) <- createProcess_ "callSelf"
    (proc self args) { std_out = UseHandle out }
  ExitSuccess <- waitForProcess ph
  return ()

sleep :: Int -> IO ()
sleep = threadDelay . (*1000000)

holdLock :: String -> SharedExclusive -> Int -> IO ()
holdLock ty sex duration = do
  withFileLock lockfile sex $ \_ -> do
    putStrLn $ "took " ++ desc
    sleep duration
    putStrLn $ "releasing " ++ desc
  where
    desc = ty ++ " lock"

tryTakingLock :: IO ()
tryTakingLock = do
  ml <- tryLockFile lockfile Exclusive
  case ml of
    Nothing -> putStrLn "lock not available"
    Just l -> do
      putStrLn "lock was available"
      unlockFile l

tryHoldLock :: String -> SharedExclusive -> Int -> IO ()
tryHoldLock ty sex duration = do
  res <- withTryFileLock lockfile sex $ \_ -> do
    putStrLn $ "took " ++ desc
    sleep duration
    putStrLn $ "released " ++ desc
  when (isNothing res) $ putStrLn "lock not available"
  where
    desc = ty ++ " lock"

lockfile :: String
lockfile = "lock"