diff options
| author | Ludovic Courtès <ludo@gnu.org> | 2014-03-07 16:46:09 +0100 |
|---|---|---|
| committer | Ludovic Courtès <ludo@gnu.org> | 2014-03-08 00:18:22 +0100 |
| commit | c7445833eb43ec621fb5a56f6bfbbf0a02a675c2 (patch) | |
| tree | 3107311f5d32a144f6c3373f6b5b0eb70041f6d5 /tests/utils.scm | |
| parent | e7f34eb0dc5a5302726857a77de3cf5f6635c1b7 (diff) | |
utils: Add a non-blocking option for 'fcntl-flock'.
* guix/utils.scm (F_SETLK): New variable.
(fcntl-flock): Add 'wait?' keyword parameter; honor it.
* tests/utils.scm ("fcntl-flock non-blocking"): New test.
Diffstat (limited to 'tests/utils.scm')
| -rw-r--r-- | tests/utils.scm | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/tests/utils.scm b/tests/utils.scm index 5be7baf016d..adac5d43815 100644 --- a/tests/utils.scm +++ b/tests/utils.scm | |||
| @@ -143,7 +143,7 @@ | |||
| 143 | (equal? (get-bytevector-all decompressed) data))))) | 143 | (equal? (get-bytevector-all decompressed) data))))) |
| 144 | 144 | ||
| 145 | (false-if-exception (delete-file temp-file)) | 145 | (false-if-exception (delete-file temp-file)) |
| 146 | (test-equal "fcntl-flock" | 146 | (test-equal "fcntl-flock wait" |
| 147 | 42 ; the child's exit status | 147 | 42 ; the child's exit status |
| 148 | (let ((file (open-file temp-file "w0"))) | 148 | (let ((file (open-file temp-file "w0"))) |
| 149 | ;; Acquire an exclusive lock. | 149 | ;; Acquire an exclusive lock. |
| @@ -182,6 +182,48 @@ | |||
| 182 | (close-port file) | 182 | (close-port file) |
| 183 | result))))))) | 183 | result))))))) |
| 184 | 184 | ||
| 185 | (test-equal "fcntl-flock non-blocking" | ||
| 186 | EAGAIN ; the child's exit status | ||
| 187 | (match (pipe) | ||
| 188 | ((input . output) | ||
| 189 | (match (primitive-fork) | ||
| 190 | (0 | ||
| 191 | (dynamic-wind | ||
| 192 | (const #t) | ||
| 193 | (lambda () | ||
| 194 | (close-port output) | ||
| 195 | |||
| 196 | ;; Wait for the green light. | ||
| 197 | (read-char input) | ||
| 198 | |||
| 199 | ;; Open FILE read-only so we can have a read lock. | ||
| 200 | (let ((file (open-file temp-file "w"))) | ||
| 201 | (catch 'flock-error | ||
| 202 | (lambda () | ||
| 203 | ;; This attempt should throw EAGAIN. | ||
| 204 | (fcntl-flock file 'write-lock #:wait? #f)) | ||
| 205 | (lambda (key errno) | ||
| 206 | (primitive-exit errno)))) | ||
| 207 | (primitive-exit -1)) | ||
| 208 | (lambda () | ||
| 209 | (primitive-exit -2)))) | ||
| 210 | (pid | ||
| 211 | (close-port input) | ||
| 212 | (let ((file (open-file temp-file "w"))) | ||
| 213 | ;; Acquire an exclusive lock. | ||
| 214 | (fcntl-flock file 'write-lock) | ||
| 215 | |||
| 216 | ;; Tell the child to continue. | ||
| 217 | (write 'green-light output) | ||
| 218 | (force-output output) | ||
| 219 | |||
| 220 | (match (waitpid pid) | ||
| 221 | ((_ . status) | ||
| 222 | (let ((result (status:exit-val status))) | ||
| 223 | (fcntl-flock file 'unlock) | ||
| 224 | (close-port file) | ||
| 225 | result))))))))) | ||
| 226 | |||
| 185 | ;; This is actually in (guix store). | 227 | ;; This is actually in (guix store). |
| 186 | (test-equal "store-path-package-name" | 228 | (test-equal "store-path-package-name" |
| 187 | "bash-4.2-p24" | 229 | "bash-4.2-p24" |
