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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
test_description='Validate cache'
. ./setup.sh
request_all_pages() {
cgit_url "" &&
for repo in foo bar
do
for page in "" /refs /tree /log /diff /patch
do
cgit_url "$repo$page" || return 1
done
done
}
set_cache_size() {
sed -e "s/^cache-size=.*$/cache-size=$1/" cgitrc >cgitrc.tmp &&
mv -f cgitrc.tmp cgitrc
}
test_expect_success 'verify cache-size=0' '
rm -f cache/* &&
set_cache_size 0 &&
request_all_pages &&
test_dir_is_empty cache
'
test_expect_success 'verify cache-size=1' '
rm -f cache/* &&
set_cache_size 1 &&
request_all_pages &&
ls cache >output &&
test_line_count = 1 output
'
test_expect_success 'verify cache-size=1021' '
rm -f cache/* &&
set_cache_size 1021 &&
request_all_pages &&
ls cache >output &&
test_line_count = 13 output &&
cgit_url "foo/ls_cache" >output.full &&
strip_headers <output.full >output &&
test_line_count = 13 output &&
cgit_url "foo/ls_cache" >output.second &&
test_cmp output.full output.second
'
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_CONFIG="$PWD/bigrc" QUERY_STRING="url=bigpage/tree/big.txt" cgit >big.first &&
test $(wc -c <big.first) -gt 65536 &&
CGIT_CONFIG="$PWD/bigrc" QUERY_STRING="url=bigpage/tree/big.txt" cgit >big.second &&
strip_headers <big.first >big.first.body &&
strip_headers <big.second >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_CONFIG="$PWD/bignocache" QUERY_STRING="url=bigpage/tree/big.txt" cgit >big.nocache &&
strip_headers <big.nocache >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 "</html>"
'
test_expect_success 'an over-long cache key leaves no slot behind' '
rm -rf cache3 && mkdir cache3 &&
sed -e "s|^cache-root=.*|cache-root=$PWD/cache3|" bigrc >bigkeyrc &&
long=$(awk "BEGIN{s=\"\";for(i=0;i<6000;i++)s=s \"k\"; print s}") &&
CGIT_CONFIG="$PWD/bigkeyrc" QUERY_STRING="url=bigpage/&q=$long" cgit >key.out 2>/dev/null &&
grep "</html>" key.out &&
test_dir_is_empty cache3
'
test_expect_success 'an ordinary key still fills a slot' '
rm -rf cache3 && mkdir cache3 &&
CGIT_CONFIG="$PWD/bigkeyrc" QUERY_STRING="url=bigpage/" cgit >/dev/null &&
ls cache3 >key.slots &&
test_line_count = 1 key.slots
'
test_expect_success 'set up a repo missing a parent object' '
mkrepo repos/broken 2 &&
parent=$(git -C repos/broken rev-parse HEAD^) &&
tip=$(git -C repos/broken rev-parse HEAD) &&
rm "repos/broken/.git/objects/$(echo "$parent" | cut -c1-2)/$(echo "$parent" | cut -c3-)" &&
{
echo "virtual-root=/" &&
echo "cache-root=$PWD/cache4" &&
echo "cache-size=64" &&
echo "repo.url=broken" &&
echo "repo.path=$PWD/repos/broken/.git"
} >brokenrc &&
rm -rf cache4 && mkdir cache4
'
test_expect_success 'an error after output began ends the page with it and caches nothing' '
CGIT_CONFIG="$PWD/brokenrc" QUERY_STRING="url=broken/commit/&id=$tip" cgit >broken.out &&
grep "Not a commit" broken.out &&
test $(grep -c "^Status:" broken.out) = 1 &&
test_dir_is_empty cache4
'
test_expect_success 'an error page leaves no slot behind' '
rm -rf cache3 && mkdir cache3 &&
CGIT_CONFIG="$PWD/bigkeyrc" \
QUERY_STRING="url=bigpage/commit/&id=0123456789abcdef0123456789abcdef01234567" cgit >error.out &&
grep "Not a commit" error.out &&
test_dir_is_empty cache3
'
test_expect_success 'a clone file leaves no slot behind' '
rm -rf cache3 && mkdir cache3 &&
CGIT_CONFIG="$PWD/bigkeyrc" QUERY_STRING="url=bigpage/info/refs" cgit >clone.out &&
grep "refs/heads/master" clone.out &&
CGIT_CONFIG="$PWD/bigkeyrc" QUERY_STRING="url=bigpage/objects/info/packs" cgit >packs.out &&
grep "^Status: 200" packs.out &&
test_dir_is_empty cache3
'
test_expect_success 'a response over cache-max-slot-size is served but not kept' '
rm -rf cache3 && mkdir cache3 &&
{
echo "cache-max-slot-size=32" &&
cat bigkeyrc
} >slotsizerc &&
CGIT_CONFIG="$PWD/slotsizerc" QUERY_STRING="url=bigpage/tree/big.txt" cgit >big.unkept &&
tail -c 200 big.unkept | grep "</html>" &&
test_dir_is_empty cache3 &&
CGIT_CONFIG="$PWD/slotsizerc" QUERY_STRING="url=bigpage/" cgit >/dev/null &&
ls cache3 >kept.slots &&
test_line_count = 1 kept.slots
'
test_expect_success 'an expired slot is dropped when its refill is too large' '
rm -rf cache3 && mkdir cache3 &&
CGIT_CONFIG="$PWD/bigkeyrc" QUERY_STRING="url=bigpage/tree/big.txt" cgit >/dev/null &&
ls cache3 >filled.slots &&
test_line_count = 1 filled.slots &&
touch -t 200001010000 cache3/* &&
CGIT_CONFIG="$PWD/slotsizerc" QUERY_STRING="url=bigpage/tree/big.txt" cgit >big.refill &&
tail -c 200 big.refill | grep "</html>" &&
test_dir_is_empty cache3
'
test_expect_success 'pages following a branch take the dynamic ttl' '
rm -rf cache3 && mkdir cache3 &&
{
echo "cache-dynamic-ttl=0" &&
cat bigkeyrc
} >dynamicrc &&
CGIT_CONFIG="$PWD/dynamicrc" QUERY_STRING="url=bigpage/log/" cgit >/dev/null &&
CGIT_CONFIG="$PWD/dynamicrc" QUERY_STRING="url=bigpage/log/&h=master" cgit >/dev/null &&
test_dir_is_empty cache3 &&
CGIT_CONFIG="$PWD/dynamicrc" QUERY_STRING="url=bigpage/" cgit >/dev/null &&
CGIT_CONFIG="$PWD/dynamicrc" QUERY_STRING="url=bigpage/summary/" cgit >/dev/null &&
ls cache3 >summary.slots &&
test_line_count = 2 summary.slots
'
test_expect_success 'a failed final write still delivers the page' '
rm -rf cache3 && mkdir cache3 &&
(
trap "" XFSZ &&
ulimit -f 2 &&
CGIT_CONFIG="$PWD/bigkeyrc" QUERY_STRING="url=bigpage/" cgit 2>flush.err
) | cat >flush.out &&
grep "^Status: 200" flush.out &&
grep "</html>" flush.out &&
! grep "called many times" flush.err
'
overlap_requests() {
n=0
rm -f filter-running
CGIT_CONFIG="$PWD/lockrc" QUERY_STRING="url=bigpage/about/" cgit >first.out 2>first.err &
while ! test -f filter-running && test $n -lt 10
do
sleep 1
n=$((n + 1))
done
CGIT_CONFIG="$PWD/lockrc" QUERY_STRING="url=bigpage/log/" cgit >/dev/null 2>second.err
wait
}
test_expect_success 'a slot stays locked while an exec filter runs' '
rm -rf cache3 && mkdir cache3 &&
write_script slow-filter.sh <<-EOF &&
touch "$PWD/filter-running"
sleep 3
cat
EOF
{
echo "cache-size=1" &&
sed "/^cache-size=/d" bigkeyrc &&
echo "repo.readme=:file-1" &&
echo "repo.about-filter=exec:$PWD/slow-filter.sh"
} >lockrc &&
overlap_requests &&
test $(grep -c "^Status:" first.out) = 1 &&
grep "id=.about." first.out &&
! grep "Unable to publish" first.err &&
! grep "Unable to lock" second.err
'
test_expect_success PERL 'a filter holds the lock file only as its stdout' '
rm -rf cache3 && mkdir cache3 &&
write_script fd-filter "$PERL_PATH" <<-\EOF &&
use POSIX qw(fstat);
1 while <STDIN>;
my @lock = stat $ENV{LOCK_FILE} or die "$!\n";
my @fds = grep {
my @fd = fstat($_);
@fd && $fd[0] == $lock[0] && $fd[1] == $lock[1]
} 0 .. 255;
print "lock file on fd @fds.\n";
EOF
{
echo "cache-size=1" &&
sed "/^cache-size=/d" bigkeyrc &&
echo "repo.readme=:file-1" &&
echo "repo.about-filter=exec:$PWD/fd-filter"
} >fdrc &&
LOCK_FILE="$PWD/cache3/00000000.lock" CGIT_CONFIG="$PWD/fdrc" \
QUERY_STRING="url=bigpage/about/" cgit >fd.out &&
grep -F "lock file on fd 1." fd.out
'
test_expect_success SANITY 'a refresh that cannot lock serves the expired slot and says why' '
rm -rf cache3 && mkdir cache3 &&
CGIT_CONFIG="$PWD/bigkeyrc" QUERY_STRING="url=bigpage/" cgit >/dev/null &&
slot=$(ls cache3) &&
echo "the expired copy" >>"cache3/$slot" &&
touch -t 200001010000 "cache3/$slot" &&
test_when_finished "chmod u+w cache3" &&
chmod a-w cache3 &&
CGIT_CONFIG="$PWD/bigkeyrc" QUERY_STRING="url=bigpage/" cgit >stale.out 2>stale.err &&
grep "the expired copy" stale.out &&
grep "Unable to open .*\.lock: Permission denied" stale.err
'
test_expect_success 'a slot lock that cannot take its key is let go' '
rm -rf cache3 && mkdir cache3 &&
(
trap "" XFSZ &&
ulimit -f 0 &&
CGIT_CONFIG="$PWD/bigkeyrc" QUERY_STRING="url=bigpage/" cgit 2>&1
) | cat >nokey.out &&
grep "</html>" nokey.out &&
grep "Unable to write .*\.lock" nokey.out &&
test_dir_is_empty cache3
'
test_expect_success 'set up a cached scan of two repositories' '
mkrepo scanned/one 1 &&
mkrepo scanned/two 1 &&
{
echo "virtual-root=/" &&
echo "cache-root=$PWD/cache5" &&
echo "cache-size=64" &&
echo "cache-index-ttl=0" &&
echo "scan-path=$PWD/scanned"
} >scanrc &&
rm -rf cache5 && mkdir cache5 &&
CGIT_CONFIG="$PWD/scanrc" QUERY_STRING="url=" cgit >/dev/null &&
scanned_rc=$(ls cache5) &&
test_path_is_file "cache5/$scanned_rc"
'
test_expect_success PERL 'a scan finding its lock held runs uncached' '
write_script hold-lock "$PERL_PATH" <<-\EOF &&
use Fcntl qw(:flock);
open(my $lock, ">>", shift) or die "$!\n";
flock($lock, LOCK_EX | LOCK_NB) or die "$!\n";
exit(system(@ARGV) ? 1 : 0);
EOF
rm "cache5/$scanned_rc" &&
CGIT_CONFIG="$PWD/scanrc" QUERY_STRING="url=" \
./hold-lock "cache5/$scanned_rc.lock" cgit >held.out 2>held.err &&
grep ">one</a>" held.out &&
grep ">two</a>" held.out &&
test_path_is_missing "cache5/$scanned_rc" &&
test_must_be_empty held.err
'
test_expect_success 'a stale lock file reaches neither the scan nor a slot' '
{
printf "%01024d\n" 0 0 0 0 &&
echo "repo.url=ghost" &&
echo "repo.path=$PWD/scanned/one/.git"
} >stale.lock &&
rm -f "cache5/$scanned_rc" &&
cp stale.lock "cache5/$scanned_rc.lock" &&
CGIT_CONFIG="$PWD/scanrc" QUERY_STRING="url=" cgit >scan.out &&
grep ">one</a>" scan.out &&
! grep ">ghost</a>" scan.out &&
rm -rf cache3 && mkdir cache3 &&
CGIT_CONFIG="$PWD/bigkeyrc" QUERY_STRING="url=bigpage/" cgit >page.out &&
slot=$(ls cache3) &&
rm "cache3/$slot" &&
{ cat page.out page.out && echo "STALE-TAIL"; } >"cache3/$slot.lock" &&
CGIT_CONFIG="$PWD/bigkeyrc" QUERY_STRING="url=bigpage/" cgit >/dev/null &&
CGIT_CONFIG="$PWD/bigkeyrc" QUERY_STRING="url=bigpage/" cgit >replay.out &&
grep "</html>" replay.out &&
! grep "STALE-TAIL" replay.out
'
test_done
|