conditions.scm 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ;; Fibers: cooperative, event-driven user-space threads.
  2. ;;;; Copyright (C) 2016 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This library is free software; you can redistribute it and/or
  5. ;;;; modify it under the terms of the GNU Lesser General Public
  6. ;;;; License as published by the Free Software Foundation; either
  7. ;;;; version 3 of the License, or (at your option) any later version.
  8. ;;;;
  9. ;;;; This library is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;;; Lesser General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU Lesser General Public
  15. ;;;; License along with this library; if not, write to the Free Software
  16. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;;;
  18. (define-module (tests conditions)
  19. #:use-module (fibers)
  20. #:use-module (fibers conditions)
  21. #:use-module (fibers operations)
  22. #:use-module (fibers timers))
  23. (define failed? #f)
  24. (define-syntax-rule (assert-equal expected actual)
  25. (let ((x expected))
  26. (format #t "assert ~s equal to ~s: " 'actual x)
  27. (force-output)
  28. (let ((y actual))
  29. (cond
  30. ((equal? x y) (format #t "ok\n"))
  31. (else
  32. (format #t "no (got ~s)\n" y)
  33. (set! failed? #t))))))
  34. (define-syntax-rule (assert-run-fibers-terminates exp)
  35. (begin
  36. (format #t "assert run-fibers on ~s terminates: " 'exp)
  37. (force-output)
  38. (let ((start (get-internal-real-time)))
  39. (call-with-values (lambda () (run-fibers (lambda () exp)))
  40. (lambda vals
  41. (format #t "ok (~a s)\n" (/ (- (get-internal-real-time) start)
  42. 1.0 internal-time-units-per-second))
  43. (apply values vals))))))
  44. (define-syntax-rule (assert-run-fibers-returns (expected ...) exp)
  45. (begin
  46. (call-with-values (lambda () (assert-run-fibers-terminates exp))
  47. (lambda run-fiber-return-vals
  48. (assert-equal '(expected ...) run-fiber-return-vals)))))
  49. (define* (with-timeout op #:key (seconds 0.05) (wrap values))
  50. (choice-operation op
  51. (wrap-operation (sleep-operation seconds) wrap)))
  52. (define (wait/timeout cv)
  53. (perform-operation
  54. (with-timeout
  55. (wrap-operation (wait-operation cv)
  56. (lambda () #t))
  57. #:wrap (lambda () #f))))
  58. (define cv (make-condition))
  59. (assert-equal #t (condition? cv))
  60. (assert-run-fibers-returns (#f) (wait/timeout cv))
  61. (assert-run-fibers-returns (#f) (wait/timeout cv))
  62. (assert-equal #t (signal-condition! cv))
  63. (assert-equal #f (signal-condition! cv))
  64. (assert-run-fibers-returns (#t) (wait/timeout cv))
  65. (assert-run-fibers-returns (#t) (wait/timeout cv))
  66. (assert-run-fibers-returns (#t)
  67. (let ((cv (make-condition)))
  68. (spawn-fiber (lambda () (signal-condition! cv)))
  69. (wait cv)
  70. #t))
  71. (exit (if failed? 1 0))