cache.scm 3.5 KB

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