cache.scm 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2020 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. (test-begin "cache")
  26. (test-equal "remove-expired-cache-entries"
  27. '("o" "l" "d")
  28. (let* ((removed '())
  29. (now (time-second (current-time time-monotonic)))
  30. (ttl 100)
  31. (stamp (match-lambda
  32. ((or "n" "e" "w") (+ now 100))
  33. ((or "o" "l" "d") (- now 100))))
  34. (delete (lambda (entry)
  35. (set! removed (cons entry removed)))))
  36. (remove-expired-cache-entries (reverse '("n" "e" "w"
  37. "o" "l" "d"))
  38. #:entry-expiration stamp
  39. #:delete-entry delete)
  40. removed))
  41. (define-syntax-rule (test-cache-cleanup cache exp ...)
  42. (call-with-temporary-directory
  43. (lambda (cache)
  44. (let* ((deleted '())
  45. (delete! (lambda (entry)
  46. (set! deleted (cons entry deleted)))))
  47. exp ...
  48. (maybe-remove-expired-cache-entries cache
  49. (const '("a" "b" "c"))
  50. #:entry-expiration (const 0)
  51. #:delete-entry delete!)
  52. (reverse deleted)))))
  53. (test-equal "maybe-remove-expired-cache-entries, first cleanup"
  54. '("a" "b" "c")
  55. (test-cache-cleanup cache))
  56. (test-equal "maybe-remove-expired-cache-entries, no cleanup needed"
  57. '()
  58. (test-cache-cleanup cache
  59. (call-with-output-file (string-append cache "/last-expiry-cleanup")
  60. (lambda (port)
  61. (display (+ (time-second (current-time time-monotonic)) 100)
  62. port)))))
  63. (test-equal "maybe-remove-expired-cache-entries, cleanup needed"
  64. '("a" "b" "c")
  65. (test-cache-cleanup cache
  66. (call-with-output-file (string-append cache "/last-expiry-cleanup")
  67. (lambda (port)
  68. (display 0 port)))))
  69. (test-end "cache")
  70. ;;; Local Variables:
  71. ;;; eval: (put 'test-cache-cleanup 'scheme-indent-function 1)
  72. ;;; End: