common.scm 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. ;;; common.scm -- Common test helpers.
  2. ;;; Copyright © 2021 Mathieu Othacehe <othacehe@gnu.org>
  3. ;;;
  4. ;;; This file is part of Cuirass.
  5. ;;;
  6. ;;; Cuirass is free software: you can redistribute it and/or modify
  7. ;;; it under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation, either version 3 of the License, or
  9. ;;; (at your option) any later version.
  10. ;;;
  11. ;;; Cuirass is distributed in the hope that it will be useful,
  12. ;;; but 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 Cuirass. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (tests common)
  19. #:use-module (cuirass database)
  20. #:use-module (cuirass parameters)
  21. #:use-module (cuirass utils)
  22. #:use-module (ice-9 popen)
  23. #:use-module (ice-9 rdelim)
  24. #:export (%db
  25. retry
  26. test-init-db!))
  27. (define %db
  28. (make-parameter #f))
  29. (define (pg-tmp)
  30. "Start a temporary PostgreSQL instance using ephemeralpg."
  31. ;; Destroy the database right after disconnection.
  32. (let* ((pipe (open-input-pipe "pg_tmp -w 1"))
  33. (uri (read-string pipe)))
  34. (close-pipe pipe)
  35. uri))
  36. (define* (retry f #:key times delay)
  37. (let loop ((attempt 1))
  38. (let ((result (f)))
  39. (cond
  40. (result result)
  41. (else
  42. (if (>= attempt times)
  43. #f
  44. (begin
  45. (sleep delay)
  46. (loop (+ 1 attempt)))))))))
  47. (define (test-init-db!)
  48. "Initialize the test database."
  49. (%create-database? #t)
  50. (%package-database (pg-tmp))
  51. (%db (db-open))
  52. (%db-channel (make-worker-thread-channel
  53. (lambda ()
  54. (list (%db))))))