stoe/test/jobs.lisp

57 lines
1.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/jobs
(:use :cl :prove
:stoe/core/containers
:stoe/core/thread
:stoe/core/jobs))
(in-package :stoe/test/jobs)
(stoe/core/jobs::initialize)
(defmacro with-new-job-thread (count &body body)
`(progn
(dotimes (i ,count)
(push-new-job-thread (format nil "Test thread ~d" i)))
,@body
(mapc (lambda (thread) (terminate-thread thread))
stoe/core/jobs::*thread-list*)
(sleep 0.5)
(stoe/core/jobs::update 0)))
(defun counter (x)
(dotimes (i 10 x)
(format t "~a~%" x)
(incf x)))
(plan 3)
(with-new-job-thread 1
(async-job () (counter 0))
(sleep 1)
(async-job () (counter 0))
(sleep 1)
(async-job () (counter 0))
(sleep 1))
(is (size stoe/core/jobs::*job-queue*) 0 "1 thread, 3 jobs, 1 at a time.")
(async-job () (counter 0))
(async-job () (counter 0))
(async-job () (counter 0))
(with-new-job-thread 1
(sleep 1))
(is (size stoe/core/jobs::*job-queue*) 0 "1 thread, 3 jobs, all at once.")
(with-new-job-thread 3
(async-job () (counter 0))
(async-job () (counter 0))
(async-job () (counter 0))
(sleep 1))
(is (size stoe/core/jobs::*job-queue*) 0 "3 threads, 3 jobs, all at once.")
(finalize)
(stoe/core/jobs::finalize)