Commit | Line | Data |
---|---|---|
2ea2aac6 | 1 | ;;; GNU Guix --- Functional package management for GNU |
9acacb71 | 2 | ;;; Copyright © 2017, 2020 Ludovic Courtès <ludo@gnu.org> |
104b4e25 | 3 | ;;; Copyright © 2022 Simon Tournier <zimon.toutoune@gmail.com> |
2ea2aac6 LC |
4 | ;;; |
5 | ;;; This file is part of GNU Guix. | |
6 | ;;; | |
7 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
8 | ;;; under the terms of the GNU General Public License as published by | |
9 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
10 | ;;; your option) any later version. | |
11 | ;;; | |
12 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
13 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | ;;; GNU General Public License for more details. | |
16 | ;;; | |
17 | ;;; You should have received a copy of the GNU General Public License | |
18 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
19 | ||
20 | (define-module (test-cache) | |
21 | #:use-module (guix cache) | |
22 | #:use-module (srfi srfi-1) | |
23 | #:use-module (srfi srfi-19) | |
24 | #:use-module (srfi srfi-64) | |
25 | #:use-module ((guix utils) #:select (call-with-temporary-directory)) | |
26 | #:use-module (ice-9 match)) | |
27 | ||
28 | (test-begin "cache") | |
29 | ||
30 | (test-equal "remove-expired-cache-entries" | |
31 | '("o" "l" "d") | |
32 | (let* ((removed '()) | |
33 | (now (time-second (current-time time-monotonic))) | |
34 | (ttl 100) | |
35 | (stamp (match-lambda | |
36 | ((or "n" "e" "w") (+ now 100)) | |
37 | ((or "o" "l" "d") (- now 100)))) | |
38 | (delete (lambda (entry) | |
39 | (set! removed (cons entry removed))))) | |
40 | (remove-expired-cache-entries (reverse '("n" "e" "w" | |
41 | "o" "l" "d")) | |
42 | #:entry-expiration stamp | |
43 | #:delete-entry delete) | |
44 | removed)) | |
45 | ||
46 | (define-syntax-rule (test-cache-cleanup cache exp ...) | |
47 | (call-with-temporary-directory | |
48 | (lambda (cache) | |
49 | (let* ((deleted '()) | |
50 | (delete! (lambda (entry) | |
51 | (set! deleted (cons entry deleted))))) | |
52 | exp ... | |
53 | (maybe-remove-expired-cache-entries cache | |
54 | (const '("a" "b" "c")) | |
55 | #:entry-expiration (const 0) | |
56 | #:delete-entry delete!) | |
57 | (reverse deleted))))) | |
58 | ||
59 | (test-equal "maybe-remove-expired-cache-entries, first cleanup" | |
60 | '("a" "b" "c") | |
61 | (test-cache-cleanup cache)) | |
62 | ||
63 | (test-equal "maybe-remove-expired-cache-entries, no cleanup needed" | |
64 | '() | |
65 | (test-cache-cleanup cache | |
66 | (call-with-output-file (string-append cache "/last-expiry-cleanup") | |
67 | (lambda (port) | |
68 | (display (+ (time-second (current-time time-monotonic)) 100) | |
69 | port))))) | |
70 | ||
71 | (test-equal "maybe-remove-expired-cache-entries, cleanup needed" | |
72 | '("a" "b" "c") | |
73 | (test-cache-cleanup cache | |
74 | (call-with-output-file (string-append cache "/last-expiry-cleanup") | |
75 | (lambda (port) | |
76 | (display 0 port))))) | |
77 | ||
104b4e25 | 78 | (test-equal "maybe-remove-expired-cache-entries, empty cache" |
79 | '("a" "b" "c") | |
80 | (test-cache-cleanup cache | |
81 | (call-with-output-file (string-append cache "/last-expiry-cleanup") | |
82 | (lambda (port) | |
83 | (display "" port))))) | |
84 | ||
85 | (test-equal "maybe-remove-expired-cache-entries, corrupted cache" | |
86 | '("a" "b" "c") | |
87 | (test-cache-cleanup cache | |
88 | (call-with-output-file (string-append cache "/last-expiry-cleanup") | |
89 | (lambda (port) | |
90 | (display "1\"34657890" port))))) | |
91 | ||
2ea2aac6 LC |
92 | (test-end "cache") |
93 | ||
94 | ;;; Local Variables: | |
95 | ;;; eval: (put 'test-cache-cleanup 'scheme-indent-function 1) | |
96 | ;;; End: |