File size: 9,544 Bytes
3dcad1f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
;;;; guardians.test --- test suite for Guile Guardians -*- scheme -*-
;;;; Jim Blandy <[email protected]> --- July 1999
;;;;
;;;; Copyright (C) 1999, 2001, 2006, 2014 Free Software Foundation, Inc.
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
;;; These tests make some questionable assumptions.
;;;
;;; - They assume that a GC will find all dead objects, so they
;;; will become flaky if we have a generational GC.
;;;
;;; - More generally, when a weakly referenced object doesn't disappear as
;;; expected, it's hard to tell whether that's because of a guardian bug of
;;; because a reference to it is being held somewhere, e.g., one some part
;;; of the stack that hasn't been overwritten. Thus, most tests cannot
;;; fail, they can just throw `unresolved'. We try hard to clear
;;; references that may have been left on the stacks (see "clear refs left
;;; on the stack" lines).
;;;
;;; - They assume that objects won't be saved by the guardian until
;;; they explicitly invoke GC --- in other words, they assume that GC
;;; won't happen too often.
(define-module (test-guardians)
:use-module (test-suite lib)
:use-module (ice-9 documentation)
:use-module (ice-9 weak-vector))
;;;
;;; miscellaneous
;;;
(define (documented? object)
(not (not (object-documentation object))))
(gc)
;;; Who guards the guardian?
;;; Note: We use strings rather than symbols because symbols are usually
;;; ``interned'', i.e., kept in a weakly-keyed hash table, thus making them
;;; inappropriate for the tests below. Furthermore, we use `string-copy' in
;;; order to make sure that no string is kept around in the interpreter
;;; unwillingly (e.g., in the source-property weak hash table).
(gc)
(define g2 (make-guardian))
(g2 (list (string-copy "g2-garbage")))
(define g3 (make-guardian))
(g3 (list (string-copy "g3-garbage")))
(g3 g2)
(pass-if "g2-garbage not collected yet" (equal? (g2) #f))
(pass-if "g3-garbage not collected yet" (equal? (g3) #f))
(set! g2 #f)
(gc)
(let ((seen-g3-garbage #f)
(seen-g2 #f)
(seen-something-else #f))
(let loop ()
(let ((saved (g3)))
(if saved
(begin
(cond
((equal? saved (list (string-copy "g3-garbage")))
(set! seen-g3-garbage #t))
((procedure? saved) (set! seen-g2 saved))
(else (pk 'junk saved) (set! seen-something-else #t)))
(loop)))))
(pass-if "g3-garbage saved" (or seen-g3-garbage (throw 'unresolved)))
(pass-if "g2-saved" (or (procedure? seen-g2) (throw 'unresolved)))
(pass-if "nothing else saved" (not seen-something-else))
;; FIXME: The following test fails because the guardian for `g2-garbage'
;; disappared from the weak-car guardian list of `g2-garbage' right before
;; `g2-garbage' was finalized (in `finalize_guarded ()'). Sample session
;; (compiled with `-DDEBUG_GUARDIANS'):
;;
;; guile> (define g (make-guardian))
;; guile> (let ((g2 (make-guardian)))
;; (format #t "g2 = ~x~%" (object-address g2))
;; (g2 (string-copy "foo"))
;; (g g2))
;; g2 = 81fde18
;; guile> (gc)
;; finalizing guarded 0x827f6a0 (1 guardians)
;; guardian for 0x827f6a0 vanished
;; end of finalize (0x827f6a0)
;; finalizing guarded 0x81fde18 (1 guardians)
;; end of finalize (0x81fde18)
(pass-if "g2-garbage saved" (or (and (procedure? seen-g2)
(equal? (seen-g2)
(list (string-copy
"g2-garbage"))))
(throw 'unresolved))))
(with-test-prefix "standard guardian functionality"
(with-test-prefix "make-guardian"
(pass-if "documented?"
(documented? make-guardian))
(pass-if "returns procedure"
(procedure? (make-guardian)))
(pass-if "returns new procedure each time"
(not (equal? (make-guardian) (make-guardian)))))
(with-test-prefix "empty guardian"
(pass-if "returns #f"
(eq? ((make-guardian)) #f))
(pass-if "returns always #f"
(let ((g (make-guardian)))
(and (eq? (g) #f)
(begin (gc) (eq? (g) #f))
(begin (gc) (eq? (g) #f))))))
(with-test-prefix "guarding independent objects"
(pass-if "guarding immediate"
(let ((g (make-guardian)))
(g #f)
(and (eq? (g) #f)
(begin (gc) (eq? (g) #f))
(begin (gc) (eq? (g) #f)))))
(pass-if "guarding non-immediate"
(let ((g (make-guardian)))
(gc)
(g (cons #f #f))
(cons 'clear 'stack) ;; clear refs left on the stack
(if (not (eq? (g) #f))
(throw 'unresolved)
(begin
(gc)
(if (not (equal? (g) (cons #f #f)))
(throw 'unresolved)
(eq? (g) #f))))))
(pass-if "guarding two non-immediates"
(let ((g (make-guardian)))
(gc)
(g (cons #f #f))
(g (cons #t #t))
(cons 'clear 'stack) ;; clear refs left on the stack
(if (not (eq? (g) #f))
(throw 'unresolved)
(begin
(gc)
(let ((l (list (g) (g))))
(if (not (or (equal? l (list (cons #f #f) (cons #t #t)))
(equal? l (list (cons #t #t) (cons #f #f)))))
(throw 'unresolved)
(eq? (g) #f)))))))
(pass-if "re-guarding non-immediates"
(let ((g (make-guardian)))
(gc)
(g (cons #f #f))
(cons 'clear 'stack) ;; clear refs left on the stack
(if (not (eq? (g) #f))
(throw 'unresolved)
(begin
(gc)
(let ((p (g)))
(if (not (equal? p (cons #f #f)))
(throw 'unresolved)
(begin
(g p)
(set! p #f)
(gc)
(if (not (equal? (g) (cons #f #f)))
(throw 'unresolved)
(eq? (g) #f)))))))))
(pass-if "guarding living non-immediate"
(let ((g (make-guardian))
(p (cons #f #f)))
(g p)
(if (not (eq? (g) #f))
(throw 'fail)
(begin
(gc)
(not (eq? (g) p)))))))
(with-test-prefix "guarding weakly referenced objects"
(pass-if "guarded weak vector element gets returned from guardian"
(let ((g (make-guardian))
(v (weak-vector #f)))
(gc)
(let ((p (cons #f #f)))
(g p)
(weak-vector-set! v 0 p)
(set! p #f)) ;; clear refs left on the stack
(if (not (eq? (g) #f))
(throw 'unresolved)
(begin
(gc)
(if (not (equal? (g) (cons #f #f)))
(throw 'unresolved)
(eq? (g) #f))))))
(pass-if "guarded element of weak vector gets eventually removed from weak vector"
(let ((g (make-guardian))
(v (weak-vector #f)))
(gc)
(let ((p (cons #f #f)))
(g p)
(weak-vector-set! v 0 p)
(set! p #f)) ;; clear refs left on the stack
(begin
(gc)
(if (not (equal? (g) (cons #f #f)))
(throw 'unresolved)
(begin
(gc)
(or (not (weak-vector-ref v 0))
(throw 'unresolved))))))))
(with-test-prefix "guarding weak containers"
(pass-if "element of guarded weak vector gets collected"
(let ((g (make-guardian))
(v (weak-vector #f)))
;; Note: We don't pass `(cons #f #f)' as an argument to `weak-vector'
;; otherwise references to it are likely to be left on the stack.
(weak-vector-set! v 0 (cons #f #f))
(g v)
(gc)
(if (equal? (weak-vector-ref v 0) (cons #f #f))
(throw 'unresolved)
#t))))
(with-test-prefix "guarding guardians"
#t)
(with-test-prefix "guarding dependent objects"
;; We don't make any guarantees about the order objects are
;; returned from guardians and therefore we skip the following
;; test.
(if #f
(pass-if "guarding vector and element"
(let ((g (make-guardian)))
(gc)
(let ((p (cons #f #f)))
(g p)
(g (vector p)))
(if (not (eq? (g) #f))
(throw 'unresolved)
(begin
(gc)
(if (not (equal? (g) (vector (cons #f #f))))
(throw 'unresolved)
(if (not (eq? (g) #f))
(throw 'unresolved)
(begin
(gc)
(if (not (equal? (g) (cons #f #f)))
(throw 'unresolved)
(eq? (g) #f)))))))))))
(with-test-prefix "guarding objects more than once"
(pass-if "guarding twice in one guardian"
(let ((g (make-guardian)))
(gc)
(let ((p (cons #f #f)))
(g p)
(g p)
(set! p #f)) ;; clear refs left on the stack
(if (not (eq? (g) #f))
(throw 'unresolved)
(begin
(gc)
(or (and (and=> (g) (lambda (o) (equal? o (cons #f #f))))
(and=> (g) (lambda (o) (equal? o (cons #f #f)))))
(throw 'unresolved))))))
(pass-if "guarding twice in two guardians"
(let ((g (make-guardian))
(h (make-guardian)))
(gc)
(let ((p (cons #f #f)))
(g p)
(h p)
(set! p #f)) ;; clear refs left on the stack
(if (not (eq? (g) #f))
(throw 'unresolved)
(begin
(gc)
(or (and (and=> (g) (lambda (o) (equal? o (cons #f #f))))
(and=> (h) (lambda (o) (equal? o (cons #f #f)))))
(throw 'unresolved)))))))
(with-test-prefix "guarding cyclic dependencies"
#t)
)
|