<commit_before>
module Test.Resources(main) where

import Development.Shake
import Test.Type
import Control.Monad
import Data.IORef


main extra = do
    ref <- newIORef 0
    flip (shaken test) extra $ \args obj -> do
        want args

        let cap = 2
        r1_cap <- newResource "test" cap
        r2_cap <- newResource "test" cap
        unless (r1_cap < r2_cap || r2_cap < r1_cap) $ error "Resources should have a good ordering"

        phony "cap" $
            need $ map obj ["file1.txt","file2.txt","file3.txt","file4.txt"]
        obj "*.txt" %> \out ->
            withResource r1_cap 1 $ do
                old <- liftIO $ atomicModifyIORef ref $ \i -> (i+1,i)
                when (old >= cap) $ error "Too many resources in use at one time"
                liftIO $ sleep 0.1
                liftIO $ atomicModifyIORef ref $ \i -> (i-1,i)
                writeFile' out ""


test build obj = do
    build ["clean"]
    build ["-j2","cap"]
    build ["clean"]
    build ["-j4","cap"]
    build ["clean"]
    build ["-j10","cap"]
<commit_msg>#291, rename more of the test<commit_after>
module Test.Resources(main) where

import Development.Shake
import Test.Type
import Control.Monad
import Data.IORef


main extra = do
    inside <- newIORef 0
    flip (shaken test) extra $ \args obj -> do
        want args

        let cap = 2
        r1_cap <- newResource "test" cap
        r2_cap <- newResource "test" cap
        unless (r1_cap < r2_cap || r2_cap < r1_cap) $ error "Resources should have a good ordering"

        phony "cap" $
            need $ map obj ["file1.txt","file2.txt","file3.txt","file4.txt"]
        obj "*.txt" %> \out ->
            withResource r1_cap 1 $ do
                old <- liftIO $ atomicModifyIORef inside $ \i -> (i+1,i)
                when (old >= cap) $ error "Too many resources in use at one time"
                liftIO $ sleep 0.1
                liftIO $ atomicModifyIORef inside $ \i -> (i-1,i)
                writeFile' out ""


test build obj = do
    build ["clean"]
    build ["-j2","cap"]
    build ["clean"]
    build ["-j4","cap"]
    build ["clean"]
    build ["-j10","cap"]

{'fraction_non_alphanumeric': 0.12855146716348392, 'fraction_numerical': 0.021891010712622262, 'mean_word_length': 1.9505494505494505, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '474359', 'n_tokens_mistral': 727, 'n_tokens_neox': 675, 'n_words': 219}