stoe/test/resources.lisp

84 lines
3.3 KiB
Common Lisp

#|
This file is a part of stoe project.
Copyright (c) 2015 Renaud Casenave-Péré (renaud@casenave-pere.fr)
|#
(uiop:define-package :stoe/test/resources
(:use :cl :prove
:stoe/core/utils
:stoe/core/resources))
(in-package :stoe/test/resources)
(setq *random-state* (make-random-state))
(defvar *data-dir* #P".data/")
(defparameter *res-array* (make-array '(10)))
(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 (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)
(write-sequence data stream))
(with-open-file (stream (get-resource-path i "lisp") :direction :output
:if-exists :supersede)
(write data :stream stream))
progress-step))))
(generate-files)
(stoe/core/resources::initialize)
(plan 47)
(diag "Sync load of binary files")
(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)
(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)
(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 (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")
(stoe/core/resources::finalize)
(setf *res-array* nil)
(tg:gc :full t)
(finalize)
(uiop:delete-directory-tree *data-dir* :validate t)