cache.scm 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. (define-module (test-cache)
  19. #:use-module (guix cache)
  20. #:use-module (srfi srfi-1)
  21. #:use-module (srfi srfi-19)
  22. #:use-module (srfi srfi-64)
  23. #:use-module ((guix utils) #:select (call-with-temporary-directory))
  24. #:use-module (ice-9 match))
  25. (cond-expand
  26. (guile-2.2
  27. ;; Guile 2.2.2 has a bug whereby 'time-monotonic' objects have seconds and
  28. ;; nanoseconds swapped (fixed in Guile commit 886ac3e). Work around it.
  29. (define time-monotonic time-tai))
  30. (else #t))
  31. (test-begin "cache")
  32. (test-equal "remove-expired-cache-entries"
  33. '("o" "l" "d")
  34. (let* ((removed '())
  35. (now (time-second (current-time time-monotonic)))
  36. (ttl 100)
  37. (stamp (match-lambda
  38. ((or "n" "e" "w") (+ now 100))
  39. ((or "o" "l" "d") (- now 100))))
  40. (delete (lambda (entry)
  41. (set! removed (cons entry removed)))))
  42. (remove-expired-cache-entries (reverse '("n" "e" "w"
  43. "o" "l" "d"))
  44. #:entry-expiration stamp
  45. #:delete-entry delete)
  46. removed))
  47. (define-syntax-rule (test-cache-cleanup cache exp ...)
  48. (call-with-temporary-directory
  49. (lambda (cache)
  50. (let* ((deleted '())
  51. (delete! (lambda (entry)
  52. (set! deleted (cons entry deleted)))))
  53. exp ...
  54. (maybe-remove-expired-cache-entries cache
  55. (const '("a" "b" "c"))
  56. #:entry-expiration (const 0)
  57. #:delete-entry delete!)
  58. (reverse deleted)))))
  59. (test-equal "maybe-remove-expired-cache-entries, first cleanup"
  60. '("a" "b" "c")
  61. (test-cache-cleanup cache))
  62. (test-equal "maybe-remove-expired-cache-entries, no cleanup needed"
  63. '()
  64. (test-cache-cleanup cache
  65. (call-with-output-file (string-append cache "/last-expiry-cleanup")
  66. (lambda (port)
  67. (display (+ (time-second (current-time time-monotonic)) 100)
  68. port)))))
  69. (test-equal "maybe-remove-expired-cache-entries, cleanup needed"
  70. '("a" "b" "c")
  71. (test-cache-cleanup cache
  72. (call-with-output-file (string-append cache "/last-expiry-cleanup")
  73. (lambda (port)
  74. (display 0 port)))))
  75. (test-end "cache")
  76. ;;; Local Variables:
  77. ;;; eval: (put 'test-cache-cleanup 'scheme-indent-function 1)
  78. ;;; End: