1 | ;;+----------------------------------------------------------------------------- |
---|
2 | ;;+ Isidorus |
---|
3 | ;;+ (c) 2008-2009 Marc Kuester, Christoph Ludwig, Lukas Giessmann |
---|
4 | ;;+ |
---|
5 | ;;+ Isidorus is freely distributable under the LGPL license. |
---|
6 | ;;+ You can find a detailed description in trunk/docs/LGPL-LICENSE.txt. |
---|
7 | ;;+----------------------------------------------------------------------------- |
---|
8 | |
---|
9 | |
---|
10 | (defpackage :threading-test |
---|
11 | (:use :cl |
---|
12 | :it.bese.FiveAM |
---|
13 | :isidorus-threading |
---|
14 | :bordeaux-threads) |
---|
15 | (:export :run-threading-tests |
---|
16 | :test-helpers |
---|
17 | :test-with-reader-lock |
---|
18 | :test-with-writer-lock |
---|
19 | :threading-test)) |
---|
20 | |
---|
21 | |
---|
22 | (in-package :threading-test) |
---|
23 | |
---|
24 | |
---|
25 | (def-suite threading-test |
---|
26 | :description "tests various key functions of the threading module") |
---|
27 | |
---|
28 | (in-suite threading-test) |
---|
29 | |
---|
30 | (test test-helpers |
---|
31 | "Tests the helper functions current-readers, add-thread-to-reader-list |
---|
32 | and remove-thread-from-reader-list" |
---|
33 | (is-true isidorus-threading::*readerlist-lock*) |
---|
34 | (is-true isidorus-threading::*writer-lock*) |
---|
35 | (is-false isidorus-threading::*current-readers*) |
---|
36 | (is-false (progn |
---|
37 | (isidorus-threading::remove-thread-from-reader-list) |
---|
38 | (current-readers))) |
---|
39 | (is (= 1 (length (progn |
---|
40 | (isidorus-threading::add-thread-to-reader-list) |
---|
41 | (current-readers))))) |
---|
42 | (is (eql (first (current-readers)) (current-thread))) |
---|
43 | (is (= 1 (length isidorus-threading::*current-readers*))) |
---|
44 | (is-true (let ((copy-of-readers |
---|
45 | (current-readers))) |
---|
46 | (setf copy-of-readers nil) |
---|
47 | isidorus-threading::*current-readers*)) |
---|
48 | (setf isidorus-threading::*current-readers* nil) |
---|
49 | (is-false (current-readers)) |
---|
50 | (is (= 2 (length (progn |
---|
51 | (isidorus-threading::add-thread-to-reader-list) |
---|
52 | (isidorus-threading::add-thread-to-reader-list) |
---|
53 | (isidorus-threading::current-readers))))) |
---|
54 | (is (= 1 (progn |
---|
55 | (isidorus-threading::remove-thread-from-reader-list) |
---|
56 | (push t isidorus-threading::*current-readers*) |
---|
57 | (length (current-readers))))) |
---|
58 | (setf isidorus-threading::*current-readers* nil)) |
---|
59 | |
---|
60 | |
---|
61 | (test test-with-reader-lock |
---|
62 | "Tests the macro with-reader-lock" |
---|
63 | (is-true isidorus-threading::*readerlist-lock*) |
---|
64 | (is-true isidorus-threading::*writer-lock*) |
---|
65 | (is-false isidorus-threading::*current-readers*) |
---|
66 | (let ((thread-1 |
---|
67 | (make-thread #'(lambda() |
---|
68 | (with-reader-lock (sleep 3))))) |
---|
69 | (thread-2 |
---|
70 | (make-thread #'(lambda() |
---|
71 | (with-reader-lock (sleep 3))))) |
---|
72 | (thread-3 |
---|
73 | (make-thread #'(lambda() |
---|
74 | (with-reader-lock (sleep 3)))))) |
---|
75 | (is (= 3 (length (current-readers)))) |
---|
76 | (is-true (find thread-1 (current-readers))) |
---|
77 | (is-true (find thread-2 (current-readers))) |
---|
78 | (is-true (find thread-3 (current-readers))) |
---|
79 | (sleep 4) |
---|
80 | (is-false (current-readers))) |
---|
81 | (setf isidorus-threading::*current-readers* nil) |
---|
82 | (make-thread #'(lambda() |
---|
83 | (with-lock-held (isidorus-threading::*readerlist-lock*) |
---|
84 | (sleep 3)))) |
---|
85 | (let ((start-time |
---|
86 | (get-universal-time))) |
---|
87 | (isidorus-threading::add-thread-to-reader-list) |
---|
88 | (is (<= (+ 2 start-time) (get-universal-time)))) |
---|
89 | (setf isidorus-threading::*current-readers* nil) |
---|
90 | (let ((start-time |
---|
91 | (get-universal-time))) |
---|
92 | (make-thread #'(lambda() (with-reader-lock (sleep 3)))) |
---|
93 | (make-thread #'(lambda() (with-reader-lock (sleep 3)))) |
---|
94 | (is (> (+ start-time 3) (get-universal-time))) |
---|
95 | (is (= 2 (length (current-readers)))) |
---|
96 | (sleep 4)) |
---|
97 | (is-false (current-readers))) |
---|
98 | |
---|
99 | |
---|
100 | (test test-with-writer-lock |
---|
101 | "Tests the macro with-writer-lock" |
---|
102 | (is-true isidorus-threading::*readerlist-lock*) |
---|
103 | (is-true isidorus-threading::*writer-lock*) |
---|
104 | (is-false isidorus-threading::*current-readers*) |
---|
105 | (let ((start-time |
---|
106 | (get-universal-time))) |
---|
107 | (with-writer-lock nil) |
---|
108 | (is (>= (+ 1 start-time) (get-universal-time)))) |
---|
109 | (make-thread #'(lambda() |
---|
110 | (with-reader-lock #'(lambda() |
---|
111 | (sleep 3))))) |
---|
112 | (let ((start-time |
---|
113 | (get-universal-time))) |
---|
114 | (make-thread #'(lambda() (with-writer-lock (sleep 3)))) |
---|
115 | (make-thread #'(lambda() (with-reader-lock (sleep 3)))) |
---|
116 | (is-false (current-readers)) |
---|
117 | (with-writer-lock nil) |
---|
118 | (is (<= (+ 3 start-time) (get-universal-time)))) |
---|
119 | (let ((start-time |
---|
120 | (get-universal-time))) |
---|
121 | (make-thread #'(lambda() (with-reader-lock (sleep 3)))) |
---|
122 | (make-thread #'(lambda() (with-reader-lock (sleep 3)))) |
---|
123 | (make-thread #'(lambda() (with-reader-lock (sleep 3)))) |
---|
124 | (with-writer-lock nil) |
---|
125 | (is (<= (+ start-time 3) (get-universal-time))))) |
---|
126 | |
---|
127 | |
---|
128 | (defun run-threading-tests () |
---|
129 | "Runs all defined tests in this package" |
---|
130 | (it.bese.fiveam:run! 'test-helpers) |
---|
131 | (it.bese.fiveam:run! 'test-with-reader-lock) |
---|
132 | (it.bese.fiveam:run! 'test-with-writer-lock)) |
---|