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