Annotation of parser3/tests/cache/run_tests.sh, revision 1.1

1.1     ! moko        1: #!/bin/sh
        !             2: # ^cache under parallel requests (ticket #1153): the main suite is single process.
        !             3: # The requests are page.html called as CGI from here, the site root; the cache file and the counter of runs are here too.
        !             4: 
        !             5: PARSER=`pwd`/../../src/targets/cgi/parser3
        !             6: DIR=`pwd`
        !             7: ROUNDS=3
        !             8: 
        !             9: request() { # the query string of page.html, prints the response body
        !            10:        env -i GATEWAY_INTERFACE=CGI/1.1 QUERY_STRING="$1" REQUEST_METHOD=GET SCRIPT_NAME=/page.html PATH_INFO=/page.html DOCUMENT_ROOT="$DIR" PATH_TRANSLATED="$DIR/page.html" $PARSER | tr -d '\r' | sed '1,/^$/d'
        !            11: }
        !            12: 
        !            13: parallel() { # count, query string: the requests start at once, prints how many got which answer
        !            14:        i=1
        !            15:        # not through request(): the pipes delay the start, and the requests must read the cache at the same time
        !            16:        while [ $i -le $1 ]; do
        !            17:                env -i GATEWAY_INTERFACE=CGI/1.1 QUERY_STRING="$2" REQUEST_METHOD=GET SCRIPT_NAME=/page.html PATH_INFO=/page.html DOCUMENT_ROOT="$DIR" PATH_TRANSLATED="$DIR/page.html" $PARSER > out.$i &
        !            18:                i=$((i+1))
        !            19:        done
        !            20:        wait
        !            21:        cat out.* | tr -d '\r' | grep -v '^Content-' | grep -v '^$' | sort | uniq -c | sed 's/^ *//'
        !            22:        rm -f out.*
        !            23: }
        !            24: 
        !            25: rounds() { # the state to start with [a file], count, query string: the same requests several times, one after another
        !            26:        for round in `seq 1 $ROUNDS`; do
        !            27:                rm -f cache.bin; [ -n "$1" ] && cp $1 cache.bin
        !            28:                parallel $2 "$3"
        !            29:        done
        !            30: }
        !            31: 
        !            32: runs() { # how many times the code has run since the last call
        !            33:        echo "the code ran: `cat runs.txt 2>/dev/null | wc -l | tr -d ' '`"
        !            34:        rm -f runs.txt
        !            35: }
        !            36: 
        !            37: holder() { # another process renews the cache under the lock, as the previous versions do: till release, or 3 seconds
        !            38:        flock -x cache.bin sleep 3 &
        !            39:        holder_pid=$!
        !            40:        sleep 0.3
        !            41: }
        !            42: 
        !            43: release() {
        !            44:        kill $holder_pid 2>/dev/null
        !            45:        wait $holder_pid 2>/dev/null
        !            46: }
        !            47: 
        !            48: timed() { # the query string: the response, and if the request had to wait for the lock
        !            49:        start=`date +%s`
        !            50:        request "$1"
        !            51:        [ $((`date +%s`-start)) -lt 2 ] && echo "did not wait" || echo "waited"
        !            52: }
        !            53: 
        !            54: expired() { # the text: the cache with it which has just expired [no more than a second ago], kept in expired.bin
        !            55:        rm -f cache.bin
        !            56:        request "expires=1&sleep=0&text=$1" >/dev/null
        !            57:        sleep 1.1
        !            58:        mv cache.bin expired.bin
        !            59:        rm -f runs.txt
        !            60: }
        !            61: 
        !            62: {
        !            63: echo "=== no cache yet, 20 requests at once, $ROUNDS times: the code runs once each time, the others wait for it"
        !            64: rm -f runs.txt
        !            65: rounds '' 20 'expires=60&sleep=0.5&text=first'
        !            66: runs
        !            67: 
        !            68: echo "=== the cache has expired, 20 requests at once, $ROUNDS times: one renews it, the others get the old copy meanwhile"
        !            69: expired old
        !            70: rounds expired.bin 20 'expires=60&sleep=1&text=new'
        !            71: runs
        !            72: 
        !            73: echo "=== the same with \$.extend(0): the others wait for the new copy"
        !            74: rounds expired.bin 10 'expires=60&extend=0&sleep=1&text=new'
        !            75: runs
        !            76: 
        !            77: echo "=== the cache has expired more than \$.extend seconds ago [the page is requested seldom]: the others wait for the new copy"
        !            78: sleep 2
        !            79: rounds expired.bin 10 'expires=60&extend=1&sleep=1&text=new'
        !            80: runs
        !            81: 
        !            82: echo "=== the code fails when the cache is renewed: the old copy is given for \$.extend seconds, then the cache is renewed"
        !            83: expired old
        !            84: cp expired.bin cache.bin
        !            85: request 'expires=60&extend=2&sleep=0&fail=1&text=new' | grep -o 'generation failed' | head -1
        !            86: request 'expires=60&extend=2&sleep=0&text=new'
        !            87: sleep 2.5
        !            88: request 'expires=60&extend=2&sleep=0&text=new'
        !            89: runs
        !            90: 
        !            91: echo "=== the code runs longer than \$.extend: the next request renews the cache too, the one who ends later writes the copy"
        !            92: expired old
        !            93: cp expired.bin cache.bin
        !            94: request 'expires=60&extend=2&sleep=4&text=long' > long.txt &
        !            95: long_pid=$!
        !            96: sleep 0.3
        !            97: request 'expires=60&extend=2&sleep=0&text=new'
        !            98: sleep 2.5
        !            99: request 'expires=60&extend=2&sleep=0&text=renewed'
        !           100: wait $long_pid
        !           101: cat long.txt; rm -f long.txt
        !           102: request 'expires=60&extend=2&sleep=0&text=other'
        !           103: runs
        !           104: 
        !           105: echo "=== the copy has expired, the other process renews it under the lock: the request waits for it, then renews the cache itself [the holder did not]"
        !           106: expired old
        !           107: cp expired.bin cache.bin
        !           108: holder
        !           109: timed 'expires=60&sleep=0&text=new'
        !           110: release
        !           111: runs
        !           112: 
        !           113: echo "=== the file is cut: not given"
        !           114: rm -f cache.bin
        !           115: request 'expires=60&sleep=0&text=original' >/dev/null
        !           116: head -c 40 cache.bin > cut.bin; mv cut.bin cache.bin
        !           117: rm -f runs.txt
        !           118: request 'expires=60&sleep=0&text=renewed'
        !           119: runs
        !           120: 
        !           121: echo "=== the new copy is written in place of the longer one: the file is cut to it"
        !           122: expired `head -c 10000 /dev/zero | tr '\0' 'x'`
        !           123: cp expired.bin cache.bin
        !           124: request 'expires=60&sleep=0&text=short'
        !           125: request 'expires=60&sleep=0&text=other'
        !           126: [ `wc -c < cache.bin` -lt 1000 ] && echo "the file is cut" || echo "the file is not cut"
        !           127: runs
        !           128: rm -f cache.bin expired.bin runs.txt
        !           129: } >tests.log 2>&1
        !           130: 
        !           131: diff -u ok.log tests.log

E-mail: