#!/bin/sh
# Exercises the cache. The first three tests set cache-size to nothing, one
# slot and the full table in turn, then count what the requests left behind.
test_description='Validate cache'
. ./setup.sh
# The counting tests below visit the same pages, the index plus six pages in
# each of two repositories, so the counts they expect stay tied to this one
# list of thirteen requests.
request_all_pages() {
cgit_ok "$1" "url=" >/dev/null &&
for repo in foo bar
do
for page in "" /refs /tree /log /diff /patch
do
cgit_ok "$1" "url=$repo$page" >/dev/null || return 1
done
done
}
test_expect_success 'a cache-size of 0 leaves no slot behind' '
rm -f cache/* &&
sed "s/^cache-size=.*/cache-size=0/" cgitrc >size0rc &&
request_all_pages size0rc &&
test_dir_is_empty cache
'
# With one slot every request lands on the same file, so the key it holds is
# all that keeps one page from being served for another.
test_expect_success 'one slot serves each request its own page' '
rm -f cache/* &&
sed "s/^cache-size=.*/cache-size=1/" cgitrc >size1rc &&
cgit_ok size1rc "url=foo/log/" >/dev/null &&
cgit_rc size1rc "url=bar/refs/" >refs.out 2>refs.err &&
test_grep "^Status: 200" refs.out &&
test_grep "data-page=.refs. data-repo=.bar." refs.out &&
test_grep "
log.out 2>log.err &&
test_grep "data-page=.log. data-repo=.foo." log.out &&
test_grep "output &&
test_line_count = 1 output
'
# The listing is a page like any other and takes a slot of its own, so a
# fresh render of it would list fourteen slots. Thirteen on the second
# request shows the first was replayed, which a dynamic ttl that never
# expires keeps true however slow the run.
test_expect_success 'a full table gives each request a slot of its own' '
rm -f cache/* &&
{
echo "cache-dynamic-ttl=-1" &&
sed "s/^cache-size=.*/cache-size=1021/" cgitrc
} >size1021rc &&
request_all_pages size1021rc &&
ls cache >output &&
test_line_count = 13 output &&
cgit_ok size1021rc "url=foo/ls_cache" >output.full &&
strip_headers output &&
test_line_count = 13 output &&
ls cache >output &&
test_line_count = 14 output &&
cgit_ok size1021rc "url=foo/ls_cache" >output.second &&
test_cmp output.full output.second
'
# Page output is buffered, so a slot only holds the whole page if the buffer is
# emptied before the size of what was generated is taken. The blob set up here
# is larger than one buffer, so a missing flush would leave the cached copy cut
# short.
test_expect_success 'set up a repo with a page larger than the output buffer' '
mkrepo repos/bigpage 1 &&
(
cd repos/bigpage &&
awk "BEGIN{for(i=0;i<20000;i++) print \"line \" i \" of the big file\"}" >big.txt &&
git add big.txt &&
git commit -m "add big.txt"
) &&
{
echo "virtual-root=/" &&
echo "cache-root=$PWD/cache2" &&
echo "cache-size=64" &&
echo "enable-tree-linenumbers=1" &&
echo "repo.url=bigpage" &&
echo "repo.path=$PWD/repos/bigpage/.git"
} >bigrc &&
rm -rf cache2 && mkdir cache2
'
test_expect_success 'the first request fills the slot and the second replays it' '
cgit_rc bigrc "url=bigpage/tree/big.txt" >big.first &&
test $(wc -c big.second &&
strip_headers big.first.body &&
strip_headers big.second.body &&
test_cmp big.first.body big.second.body
'
test_expect_success 'the cached body matches one generated with the cache off' '
sed -e "s/^cache-size=64$/cache-size=0/" bigrc >bignocache &&
cgit_rc bignocache "url=bigpage/tree/big.txt" >big.nocache &&
strip_headers big.nocache.body &&
test_cmp big.nocache.body big.second.body
'
test_expect_success 'the page ends where it should, so nothing was dropped' '
tail -c 200 big.second.body | grep "