diff --git a/config.lisp b/config.lisp index c63b690..08c8294 100644 --- a/config.lisp +++ b/config.lisp @@ -6,7 +6,8 @@ (:use :common-lisp) (:export #:base #:root #:*root* #:env-data #:env-keys #:env-prefix #:env-path - #:actions #:add #:add-action #:children #:env-slots #:name #:setup #:parent)) + #:actions #:add #:add-action #:children #:env-slots + #:name #:setup #:parent #:shutdown)) (in-package :scopes/config) @@ -60,6 +61,7 @@ (name :reader name :initarg :name) (parent :reader parent :initarg :parent) (setup :reader setup :initarg :setup :initform #'(lambda (cfg))) + (shutdown :reader shutdown :initarg :shutdown :initform #'(lambda (ctx))) (actions :accessor actions :initarg :actions :initform nil))) (defmethod initialize-instance :after ((cfg base) &key parent &allow-other-keys) diff --git a/core/core.lisp b/core/core.lisp index 065fca1..69d9888 100644 --- a/core/core.lisp +++ b/core/core.lisp @@ -3,9 +3,10 @@ (defpackage :scopes/core (:use :common-lisp) (:local-nicknames (:config :scopes/config) - (:message :scopes/core/message)) + (:message :scopes/core/message) + (:alx :alexandria)) (:export #:*root* #:default-setup #:find-service #:setup-services - #:context #:add-action #:config #:name #:send + #:context #:add-action #:config #:name #:send #:shutdown #:printer)) (in-package :scopes/core) @@ -45,7 +46,7 @@ ((config :reader config :initarg :config) (name :reader name :initarg :name) (actions :accessor actions :initform nil) - (services :initform (make-hash-table)))) + (services :reader services :initform (make-hash-table)))) (defun default-setup (cfg &optional (cls 'context)) (make-instance cls :config cfg :name (config:name cfg))) @@ -60,6 +61,10 @@ (dolist (c (config:children cfg)) (add-service *root* c))) +(defun shutdown () + (dolist (ctx (alx:hash-table-values (services *root*))) + (funcall (config:shutdown (config ctx)) ctx))) + (defun add-action (ctx pat hdlr) (let* ((acts (actions ctx)) (act (find-action pat acts))) diff --git a/test/test-core.lisp b/test/test-core.lisp index 5ac53b0..d8bdb7c 100644 --- a/test/test-core.lisp +++ b/test/test-core.lisp @@ -53,11 +53,14 @@ (t:*test-suite* (make-instance 'test-suite :name "core"))) (log4cl:log-config :sane :daily (t:test-path "scopes-test.log" "log")) (load (t:test-path "config-core" "etc")) - (core:setup-services *config*) - (setf (receiver t:*test-suite*) (core:find-service :test-receiver)) - (test-send) - (check-expected) - (t:show-result))) + (unwind-protect + (progn + (core:setup-services *config*) + (setf (receiver t:*test-suite*) (core:find-service :test-receiver)) + (test-send)) + (core:shutdown) + (check-expected) + (t:show-result)))) (deftest test-send () (let ((rcvr (receiver t:*test-suite*))