Fix resources tests

This commit is contained in:
Renaud Casenave-Péré 2017-06-06 23:29:06 +02:00
parent 94a4243a84
commit 20078d8901
3 changed files with 45 additions and 50 deletions

View file

@ -78,9 +78,11 @@ The syntax of defrestype is as follows:
(:load (make-load-fun extension classname (cdr fun)))))))
(defclass binary-resource (shared-resource)
((buffer :initarg :buffer :type '(array (unsigned-byte 8) (*)) :reader raw-data))
((buffer :initarg :buffer :type (array (unsigned-byte 8) (*)) :reader raw-data))
(:documentation "Resource containing binary data."))
(defmethod resource-initialize ((res binary-resource)))
(defclass stream-resource (resource)
((stream :initarg :stream :reader raw-data))
(:documentation "Streaming resource."))
@ -89,23 +91,25 @@ The syntax of defrestype is as follows:
((data :initarg :data :reader raw-data))
(:documentation "Resource defined in lisp."))
(defmethod resource-initialize ((res lisp-resource)))
(defclass resource-proxy ()
((resource :initarg :resource))
(:documentation "Proxy class of a resource."))
(defmethod raw-data ((res resource-proxy))
(with-slots (resource) res
(when (tg:weak-pointer-value resource)
(when (and resource (tg:weak-pointer-value resource))
(raw-data (tg:weak-pointer-value resource)))))
(defmethod res-path ((res resource-proxy))
(with-slots (resource) res
(when (tg:weak-pointer-value resource)
(when (and resource (tg:weak-pointer-value resource))
(res-path (tg:weak-pointer-value resource)))))
(defmethod res-loaded-p ((res resource-proxy))
(with-slots (resource) res
(when (tg:weak-pointer-value resource)
(when (and resource (tg:weak-pointer-value resource))
(res-loaded-p (tg:weak-pointer-value resource)))))
(defun initialize (&optional argv)
@ -177,7 +181,8 @@ The syntax of defrestype is as follows:
(defun unload-resource (proxy)
(tg:cancel-finalization proxy)
(with-slots (resource) proxy
(dec-ref (tg:weak-pointer-value resource))))
(dec-ref (tg:weak-pointer-value resource))
(setf resource nil)))
(defmacro with-resource ((path proxy &optional (promise (gensym "PROMISE") promise-p)) &body body)
`(multiple-value-bind (,proxy ,promise) (load-resource ,path)

View file

@ -7,5 +7,5 @@
(:nicknames :test)
(:use-reexport
:stoe/test/jobs
;; :stoe/test/resources
:stoe/test/resources
:stoe/test/entity))

View file

@ -6,89 +6,79 @@
(uiop:define-package :stoe/test/resources
(:use :cl :prove
:stoe/core/utils
:stoe/core/resources
:stoe/test/job-utils))
:stoe/core/resources))
(in-package :stoe/test/resources)
(setq *random-state* (make-random-state))
(defvar *data-dir* #P".data/")
(defvar *res-array* (make-array '(10)))
(defparameter *res-array* (make-array '(10)))
(defun get-resource-path (index &optional (ext "dat"))
(merge-pathnames (make-pathname :name (format nil "~2,'0d.~a" index ext)) *data-dir*))
(defun get-resource-path (index &optional (ext "bin"))
(merge-pathnames (make-pathname :name (format nil "~2,'0d" index) :type ext) *data-dir*))
(defun generate-files ()
(ensure-directories-exist *data-dir*)
;; Make a bunch of files full of random binary+lisp data
(loop-with-progress "Generating files" for i below 10
do (let ((data (loop for j below (* 1024 1024 (+ 0.5 (random 1.0)))
collect (random 255))))
do (unless (and (probe-file (get-resource-path i))
(probe-file (get-resource-path i "lisp")))
(let ((data (loop for j below (* 1024 1024 (+ 0.5 (random 1.0)))
collect (random 255))))
(with-open-file (stream (get-resource-path i) :direction :output
:element-type '(unsigned-byte 8)
:if-exists :supersede)
:element-type '(unsigned-byte 8)
:if-exists :supersede)
(write-sequence data stream))
(with-open-file (stream (get-resource-path i "lisp") :direction :output
:if-exists :supersede)
:if-exists :supersede)
(write data :stream stream))
progress-step)))
progress-step))))
(generate-files)
(stoe/core/jobs::initialize)
(stoe/core/resources::initialize)
(plan 43)
(plan 47)
(diag "Sync load of binary files")
(dotimes (i 10)
(setf (aref *res-array* i) (load-bin-resource (get-resource-path i)))
(is (res-loaded-p (res-deref (aref *res-array* i))) t
(format nil "file ~2,'0d.dat loaded in memory" i)))
(dotimes (i 10 t)
(with-resource ((get-resource-path i) proxy)
(is (res-loaded-p proxy) t (format nil "file ~2,'0d.bin loaded in memory" i))
(setf (aref *res-array* i) proxy)))
(dotimes (i 5 t)
(unload-resource (aref *res-array* i))
(is (res-loaded-p (aref *res-array* i)) nil (format nil "file ~2,'0d.bin unloaded" i)))
(setf *res-array* (make-array '(10)))
(tg:gc :full t)
(is (hash-table-count stoe/core/resources::*resources-db*) 0 "All resources unloaded")
(dotimes (i 10)
(setf (aref *res-array* i) (load-bin-resource (get-resource-path i)))
(with-open-file (stream (get-resource-path (+ 10 i)) :direction :output
:element-type '(unsigned-byte 8) :if-exists :supersede)
(write-sequence (res-buffer (res-deref (aref *res-array* i))) stream))
(is (sb-ext:process-exit-code
(sb-ext:run-program "diff" (list (pathname-path (get-resource-path i))
(pathname-path (get-resource-path (+ 10 i))))
:search t)) 0
(format nil "file ~2,'0d.dat integrity" i)))
(with-resource ((get-resource-path i) proxy)
(setf (aref *res-array* i) proxy)
(with-open-file (stream (get-resource-path (+ 10 i))
:direction :output
:element-type '(unsigned-byte 8) :if-exists :supersede)
(write-sequence (raw-data (aref *res-array* i)) stream))
(is (sb-ext:process-exit-code
(sb-ext:run-program "diff" (list (pathname-path (get-resource-path i))
(pathname-path (get-resource-path (+ 10 i))))
:search t)) 0 (format nil "file ~2,'0d.bin integrity" i))))
(diag "Shared load of binary files")
(dotimes (i 10)
(let* ((proxy (load-bin-resource (get-resource-path i)))
(res (res-deref proxy)))
(is (slot-value res 'refcount) 2 (format nil "file ~2,'0d.dat has 2 refs" i))
(with-resource ((get-resource-path i) proxy)
(is (refcount (tg:weak-pointer-value (slot-value proxy 'resource))) 2 (format nil "file ~2,'0d.bin has 2 refs" i))
(unload-resource proxy)
(is (slot-value res 'refcount) 1 (format nil "file ~2,'0d.dat has 1 ref" i))
(is (refcount (tg:weak-pointer-value (slot-value (aref *res-array* i) 'resource))) 1 (format nil "file ~2,'0d.bin has 1 ref" i))
(unload-resource (aref *res-array* i))))
(is (hash-table-count stoe/core/resources::*resources-db*) 0 "All resources unloaded")
(diag "Async load of binary files")
(with-new-job-thread 2
(let (promise-list readyp)
(dotimes (i 10)
(multiple-value-bind (proxy promise) (load-bin-resource (get-resource-path i))
(setf (aref *res-array* i) proxy)
(push promise promise-list)))
(bb:alet ((res-list (bb:all promise-list)))
(setf readyp t))
(sleep 5)
(is readyp t "Load 10 files asynchronically")))
(stoe/core/resources::finalize)
(setf *res-array* nil)
(tg:gc :full t)
(finalize)
(stoe/core/jobs::finalize)
(uiop:delete-directory-tree *data-dir* :validate t)