2021-03-09 18:06:57 +01:00
|
|
|
;;; common.scm -- Common test helpers.
|
|
|
|
;;; Copyright © 2021 Mathieu Othacehe <othacehe@gnu.org>
|
|
|
|
;;;
|
|
|
|
;;; This file is part of Cuirass.
|
|
|
|
;;;
|
|
|
|
;;; Cuirass is free software: you can redistribute it and/or modify
|
|
|
|
;;; it under the terms of the GNU General Public License as published by
|
|
|
|
;;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;;; (at your option) any later version.
|
|
|
|
;;;
|
|
|
|
;;; Cuirass is distributed in the hope that it will be useful,
|
|
|
|
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with Cuirass. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
(define-module (tests common)
|
|
|
|
#:use-module (cuirass database)
|
|
|
|
#:use-module (cuirass parameters)
|
|
|
|
#:use-module (cuirass utils)
|
2021-03-10 07:44:58 +01:00
|
|
|
#:use-module (ice-9 popen)
|
|
|
|
#:use-module (ice-9 rdelim)
|
2021-03-09 18:06:57 +01:00
|
|
|
#:export (%db
|
2021-03-22 16:25:53 +01:00
|
|
|
retry
|
2021-03-09 18:06:57 +01:00
|
|
|
test-init-db!))
|
|
|
|
|
|
|
|
(define %db
|
|
|
|
(make-parameter #f))
|
|
|
|
|
2021-03-10 07:44:58 +01:00
|
|
|
(define (pg-tmp)
|
|
|
|
"Start a temporary PostgreSQL instance using ephemeralpg."
|
|
|
|
;; Destroy the database right after disconnection.
|
|
|
|
(let* ((pipe (open-input-pipe "pg_tmp -w 1"))
|
|
|
|
(uri (read-string pipe)))
|
|
|
|
(close-pipe pipe)
|
|
|
|
uri))
|
|
|
|
|
2021-03-22 16:25:53 +01:00
|
|
|
(define* (retry f #:key times delay)
|
|
|
|
(let loop ((attempt 1))
|
|
|
|
(let ((result (f)))
|
|
|
|
(cond
|
|
|
|
(result result)
|
|
|
|
(else
|
|
|
|
(if (>= attempt times)
|
|
|
|
#f
|
|
|
|
(begin
|
|
|
|
(sleep delay)
|
|
|
|
(loop (+ 1 attempt)))))))))
|
|
|
|
|
2021-03-09 18:06:57 +01:00
|
|
|
(define (test-init-db!)
|
|
|
|
"Initialize the test database."
|
|
|
|
(%create-database? #t)
|
2021-03-10 07:44:58 +01:00
|
|
|
(%package-database (pg-tmp))
|
2021-03-09 18:06:57 +01:00
|
|
|
(%db (db-open))
|
|
|
|
(%db-channel (make-worker-thread-channel
|
|
|
|
(lambda ()
|
|
|
|
(list (%db))))))
|