tests: Rewrite 'fcntl-lock' test.

* tests/utils.scm (temp-file): New variable.
  ("fcntl-flock"): Rewrite to actually test whether the child process
  waits for the lock to be released.  The previous test was wrong
  because (1) it expected F_SETLK semantics, not F_SETLKW, and (2) it
  got EBADF because of a mismatch between the open mode and the lock
  style.
This commit is contained in:
Ludovic Courtès 2014-03-06 18:38:19 +01:00
parent 56c72822a8
commit 827d556311

View file

@ -27,6 +27,9 @@ (define-module (test-utils)
#:use-module (rnrs io ports) #:use-module (rnrs io ports)
#:use-module (ice-9 match)) #:use-module (ice-9 match))
(define temp-file
(string-append "t-utils-" (number->string (getpid))))
(test-begin "utils") (test-begin "utils")
(test-assert "bytevector->base16-string->bytevector" (test-assert "bytevector->base16-string->bytevector"
@ -139,33 +142,43 @@ (define-module (test-utils)
(append pids1 pids2))) (append pids1 pids2)))
(equal? (get-bytevector-all decompressed) data))))) (equal? (get-bytevector-all decompressed) data)))))
(false-if-exception (delete-file temp-file))
(test-equal "fcntl-flock" (test-equal "fcntl-flock"
0 ; the child's exit status 42 ; the child's exit status
(let ((file (open-input-file (search-path %load-path "guix.scm")))) (let ((file (open-file temp-file "w0")))
(fcntl-flock file 'read-lock) ;; Acquire an exclusive lock.
(fcntl-flock file 'write-lock)
(match (primitive-fork) (match (primitive-fork)
(0 (0
(dynamic-wind (dynamic-wind
(const #t) (const #t)
(lambda () (lambda ()
;; Taking a read lock should be OK. ;; Reopen FILE read-only so we can have a read lock.
(fcntl-flock file 'read-lock) (let ((file (open-file temp-file "r")))
(fcntl-flock file 'unlock) ;; Wait until we can acquire the lock.
(fcntl-flock file 'read-lock)
(catch 'flock-error (primitive-exit (read file)))
(lambda ()
;; Taking an exclusive lock should raise an exception.
(fcntl-flock file 'write-lock))
(lambda args
(primitive-exit 0)))
(primitive-exit 1)) (primitive-exit 1))
(lambda () (lambda ()
(primitive-exit 2)))) (primitive-exit 2))))
(pid (pid
;; Write garbage and wait.
(display "hello, world!" file)
(force-output file)
(sleep 1)
;; Write the real answer.
(seek file 0 SEEK_SET)
(truncate-file file 0)
(write 42 file)
(force-output file)
;; Unlock, which should let the child continue.
(fcntl-flock file 'unlock)
(match (waitpid pid) (match (waitpid pid)
((_ . status) ((_ . status)
(let ((result (status:exit-val status))) (let ((result (status:exit-val status)))
(fcntl-flock file 'unlock)
(close-port file) (close-port file)
result))))))) result)))))))
@ -178,5 +191,7 @@ (define-module (test-utils)
(test-end) (test-end)
(false-if-exception (delete-file temp-file))
(exit (= (test-runner-fail-count (test-runner-current)) 0)) (exit (= (test-runner-fail-count (test-runner-current)) 0))