test-finalization.scm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. ;;; Copyright (C) 2024 David Thompson <dave@spritely.institute>
  2. ;;;
  3. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  4. ;;; you may not use this file except in compliance with the License.
  5. ;;; You may obtain a copy of the License at
  6. ;;;
  7. ;;; http://www.apache.org/licenses/LICENSE-2.0
  8. ;;;
  9. ;;; Unless required by applicable law or agreed to in writing, software
  10. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  11. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. ;;; See the License for the specific language governing permissions and
  13. ;;; limitations under the License.
  14. ;;; Commentary:
  15. ;;;
  16. ;;; Finalization tests.
  17. ;;;
  18. ;;; Code:
  19. (use-modules (hoot finalization)
  20. (srfi srfi-64)
  21. (test utils))
  22. (test-begin "test-finalization")
  23. ;; Test if our finalization registry emulation is good enough.
  24. (test-equal "basic registration"
  25. 'yay
  26. (let* ((result #f)
  27. (r (make-finalization-registry (lambda (x) (set! result x)))))
  28. (finalization-registry-register! r (list 'foo) 'yay)
  29. (gc)
  30. (poll-finalization-registry! r)
  31. result))
  32. (test-equal "basic unregistration"
  33. #f
  34. (let* ((result #f)
  35. (r (make-finalization-registry (lambda (x) (set! result x)))))
  36. (finalization-registry-register! r (list 'foo) 'no 'token)
  37. (finalization-registry-unregister! r 'token)
  38. (gc)
  39. (poll-finalization-registry! r)
  40. result))
  41. (test-equal "registering same value multiple times with same held value"
  42. '(yay)
  43. (let* ((result '())
  44. (r (make-finalization-registry (lambda (x) (set! result (cons x result))))))
  45. (let ((val (list 'foo)))
  46. (finalization-registry-register! r val 'yay)
  47. (finalization-registry-register! r val 'yay)
  48. (finalization-registry-register! r val 'yay))
  49. (gc)
  50. (poll-finalization-registry! r)
  51. result))
  52. (test-equal "registering same value multiple times with different held values"
  53. 6
  54. (let* ((result 0)
  55. (r (make-finalization-registry (lambda (x) (set! result (+ result x))))))
  56. (let ((val (list 'foo)))
  57. (finalization-registry-register! r val 1)
  58. (finalization-registry-register! r val 2)
  59. (finalization-registry-register! r val 3))
  60. (gc)
  61. (poll-finalization-registry! r)
  62. result))
  63. (test-equal "registering same value multiple times with different held values and unregister tokens"
  64. 4
  65. (let* ((result 0)
  66. (r (make-finalization-registry (lambda (x) (set! result (+ result x))))))
  67. (let ((val (list 'foo)))
  68. (finalization-registry-register! r val 1 'foo)
  69. (finalization-registry-register! r val 2 'bar)
  70. (finalization-registry-register! r val 3 'baz))
  71. (finalization-registry-unregister! r 'bar)
  72. (gc)
  73. (poll-finalization-registry! r)
  74. result))
  75. (test-equal "registering different values with the same unregister token"
  76. 3
  77. (let* ((result 0)
  78. (r (make-finalization-registry (lambda (x) (set! result (+ result x))))))
  79. (let ((val (list 'foo)))
  80. (finalization-registry-register! r val 1 'foo)
  81. (finalization-registry-register! r val 2 'foo)
  82. (finalization-registry-register! r val 3 'bar))
  83. (finalization-registry-unregister! r 'foo)
  84. (gc)
  85. (poll-finalization-registry! r)
  86. result))
  87. (test-assert "unregistering with unused token returns #f"
  88. (not (let ((r (make-finalization-registry (lambda (x) x))))
  89. (finalization-registry-unregister! r 'nope))))
  90. (test-assert "unregistering with used token returns #t"
  91. (let ((r (make-finalization-registry (lambda (x) x))))
  92. (finalization-registry-register! r (list 'foo) 'hey 'cancel)
  93. (finalization-registry-unregister! r 'cancel)))
  94. ;; These tests just verify that the wasm bindings work. We cannot
  95. ;; manually invoke GC on the web to test that the finalization
  96. ;; callback is being called properly.
  97. (with-additional-imports ((hoot finalization))
  98. (test-call "#t"
  99. (lambda ()
  100. (finalization-registry?
  101. (make-finalization-registry (lambda (x) x)))))
  102. (test-call "#t"
  103. (lambda ()
  104. (let ((r (make-finalization-registry (lambda (x) x))))
  105. (finalization-registry-register! r (list 'foo) 'cool)
  106. #t)))
  107. (test-call "(foo)\n#t"
  108. (lambda ()
  109. (let ((r (make-finalization-registry (lambda (x) x)))
  110. (val (list 'foo)))
  111. (finalization-registry-register! r val 'hey 'cancel)
  112. ;; Return val to ensure it's live for the duration of
  113. ;; the test.
  114. (values val (finalization-registry-unregister! r 'cancel))))))
  115. (test-end* "test-finalization")