Permalink
Browse files

allow for optional sticky closures to be made; and added appropriate …

…unit tests too
  • Loading branch information...
1 parent 9559b2d commit 2a332f6de9b79d49d62de16e9e3033cf72638f7a @vegashacker committed May 14, 2009
Showing with 48 additions and 14 deletions.
  1. +33 −13 closures.scm
  2. +15 −1 unit-test.ss
View
@@ -15,28 +15,48 @@
num-closures-in-memory
)
-(define-syntax body-as-closure-key
+(define-syntax body-as-closure-key-aux
(syntax-rules ()
- ((_ (req-identifier) body ...)
- (let ((key (make-closure-key)))
- (body-as-closure-key (req-identifier key) body ...)))
- ((_ (req-identifier key-expr) body ...)
- (let ((key-identifier key-expr))
- (add-closure! #:key key-identifier
- (lambda (req-identifier)
- ;; first cleanup after itself
- (hash-remove! CLOSURES key-identifier)
+ ((_ req-iden key-iden is-sticky-expr body ...)
+ (let ((is-sticky is-sticky-expr))
+ (add-closure! #:key key-iden
+ (lambda (req-iden)
+ ;; first cleanup after itself (if not sticky)
+ (unless is-sticky (hash-remove! CLOSURES key-iden))
;; then run the actual closure...
body ...))))))
+(define-syntax body-as-closure-key
+ (syntax-rules ()
+ ((_ (req-iden #:sticky) body ...)
+ (let ((key-iden (make-closure-key)))
+ (body-as-closure-key-aux req-iden key-iden #t body ...)))
+ ((_ (req-iden key-expr #:sticky) body ...)
+ (let ((key-iden key-expr))
+ (body-as-closure-key-aux req-iden key-iden #t body ...)))
+ ((_ (req-iden) body ...)
+ (let ((key-iden (make-closure-key)))
+ (body-as-closure-key-aux req-iden key-iden #f body ...)))
+ ((_ (req-iden key-expr) body ...)
+ (let ((key-iden key-expr))
+ (let ((key-iden key-expr))
+ (body-as-closure-key-aux req-iden key-iden #f body ...))))))
+
;;
;; body-as-url
;;
+;; Forms:
;; (body-as-url (req) body ...)
-;; or
;; (body-as-url (req fn-key) body ...)
-;; In the latter form, fn-key is the key that will be used to map to body.
-;; This provides a way for the developer to reuse fns in certain situations.
+;; (body-as-url (req #:sticky) body ...)
+;; (body-as-url (req fn-key #:sticky) body ...)
+;;
+;; If fn-key is given, it will be the key used to map to the body.
+;; (This provides a way for the developer to reuse fns in certain situations.)
+;;
+;; If #:sticky appears at the end of the first argument, then the closure will not
+;; be removed form memory after it's invoked (on a server restart, however, all closures
+;; --regardless of stickiness--are cleared).
;;
(define-syntax body-as-url
(syntax-rules ()
View
@@ -13,7 +13,8 @@
(test-equal? "numeric equality" 1 1 )
(test-equal? "md5 hashing" (md5-string "hello") "5d41402abc4b2a76b9719d911017c592")
(test-equal? "num closures in memory at start" (num-closures-in-memory) 0)
-
+
+ ;; closure testing
(test-equal? "after a closure made"
(begin (set! clos-key (body-as-closure-key (req) "hi"))
(num-closures-in-memory))
@@ -38,6 +39,19 @@
(test-equal? "make sure manual closures clean up too"
(num-closures-in-memory)
0)
+
+ ;; test sticky closures
+ (test-equal? "make a sticky closure"
+ (begin (set! clos-key (body-as-closure-key (req clos-key #:sticky)
+ "sticky!"))
+ (num-closures-in-memory))
+ 1)
+ (test-equal? "call the sticky closure"
+ (call-closure clos-key 'dummy-req)
+ "sticky!")
+ (test-equal? "make sure the sticky closure sticks"
+ (num-closures-in-memory)
+ 1)
)))

0 comments on commit 2a332f6

Please sign in to comment.