workers.scm 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; 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 GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (test-workers)
  19. #:use-module (guix workers)
  20. #:use-module (ice-9 threads)
  21. #:use-module (srfi srfi-64))
  22. (test-begin "workers")
  23. (test-equal "enqueue"
  24. 4242
  25. (let* ((pool (make-pool))
  26. (result 0)
  27. (1+! (let ((lock (make-mutex)))
  28. (lambda ()
  29. (with-mutex lock
  30. (set! result (+ result 1)))))))
  31. (let loop ((i 4242))
  32. (unless (zero? i)
  33. (pool-enqueue! pool 1+!)
  34. (loop (- i 1))))
  35. (let poll ()
  36. (unless (pool-idle? pool)
  37. (pk 'busy result)
  38. (sleep 1)
  39. (poll)))
  40. result))
  41. ;; Same as above, but throw exceptions within the workers and make sure they
  42. ;; remain alive.
  43. (test-equal "exceptions"
  44. 4242
  45. (let* ((pool (make-pool 10))
  46. (result 0)
  47. (1+! (let ((lock (make-mutex)))
  48. (lambda ()
  49. (with-mutex lock
  50. (set! result (+ result 1)))))))
  51. (let loop ((i 10))
  52. (unless (zero? i)
  53. (pool-enqueue! pool (lambda ()
  54. (throw 'whatever)))
  55. (loop (- i 1))))
  56. (let loop ((i 4242))
  57. (unless (zero? i)
  58. (pool-enqueue! pool 1+!)
  59. (loop (- i 1))))
  60. (let poll ()
  61. (unless (pool-idle? pool)
  62. (pk 'busy result)
  63. (sleep 1)
  64. (poll)))
  65. result))
  66. (test-end)