aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorTom Tromey2012-08-15 13:11:54 -0600
committerTom Tromey2012-08-15 13:11:54 -0600
commitfc196ac95224330384227da8f5706631701e3610 (patch)
tree32a1ce0f7bd3f782eaf58e4f1be72b9228300261 /test
parent51100bb8d36f68842ab55fd0501af56dfc58cc51 (diff)
downloademacs-fc196ac95224330384227da8f5706631701e3610.tar.gz
emacs-fc196ac95224330384227da8f5706631701e3610.zip
This adds some tests of the threading code.
Diffstat (limited to 'test')
-rw-r--r--test/automated/threads.el165
1 files changed, 165 insertions, 0 deletions
diff --git a/test/automated/threads.el b/test/automated/threads.el
new file mode 100644
index 00000000000..b09e269b0d7
--- /dev/null
+++ b/test/automated/threads.el
@@ -0,0 +1,165 @@
1;;; threads.el --- tests for threads.
2
3;; Copyright (C) 2012 Free Software Foundation, Inc.
4
5;; This file is part of GNU Emacs.
6
7;; GNU Emacs is free software: you can redistribute it and/or modify
8;; it under the terms of the GNU General Public License as published by
9;; the Free Software Foundation, either version 3 of the License, or
10;; (at your option) any later version.
11
12;; GNU Emacs is distributed in the hope that it will be useful,
13;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;; GNU General Public License for more details.
16
17;; You should have received a copy of the GNU General Public License
18;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
19
20;;; Code:
21
22(ert-deftest threads-is-one ()
23 "test for existence of a thread"
24 (should (current-thread)))
25
26(ert-deftest threads-threadp ()
27 "test of threadp"
28 (should (threadp (current-thread))))
29
30(ert-deftest threads-type ()
31 "test of thread type"
32 (should (eq (type-of (current-thread)) 'thread)))
33
34(ert-deftest threads-name ()
35 "test for name of a thread"
36 (should
37 (string= "hi bob" (thread-name (make-thread #'ignore "hi bob")))))
38
39(ert-deftest threads-alive ()
40 "test for thread liveness"
41 (should
42 (thread-alive-p (make-thread #'ignore))))
43
44(ert-deftest threads-all-threads ()
45 "simple test for all-threads"
46 (should (listp (all-threads))))
47
48(defvar threads-test-global nil)
49
50(defun threads-test-thread1 ()
51 (setq threads-test-global 23))
52
53(ert-deftest threads-basic ()
54 "basic thread test"
55 (should
56 (progn
57 (setq threads-test-global nil)
58 (make-thread #'threads-test-thread1)
59 (while (not threads-test-global)
60 (thread-yield))
61 threads-test-global)))
62
63(ert-deftest threads-join ()
64 "test of thread-join"
65 (should
66 (progn
67 (setq threads-test-global nil)
68 (let ((thread (make-thread #'threads-test-thread1)))
69 (thread-join thread)
70 (and threads-test-global
71 (not (thread-alive-p thread)))))))
72
73(defvar threads-test-binding nil)
74
75(defun threads-test-thread2 ()
76 (let ((threads-test-binding 23))
77 (thread-yield))
78 (setq threads-test-global 23))
79
80(ert-deftest threads-let-binding ()
81 "simple test of threads and let bindings"
82 (should
83 (progn
84 (setq threads-test-binding nil)
85 (make-thread #'threads-test-thread2)
86 (while (not threads-test-global)
87 (thread-yield))
88 (and (not threads-test-binding)
89 threads-test-global))))
90
91(ert-deftest threads-mutexp ()
92 "simple test of mutexp"
93 (should-not (mutexp 'hi)))
94
95(ert-deftest threads-mutexp-2 ()
96 "another simple test of mutexp"
97 (should (mutexp (make-mutex))))
98
99(ert-deftest threads-mutex-type ()
100 "type-of mutex"
101 (should (eq (type-of (make-mutex)) 'mutex)))
102
103(ert-deftest threads-mutex-lock-unlock ()
104 "test mutex-lock and unlock"
105 (should
106 (let ((mx (make-mutex)))
107 (mutex-lock mx)
108 (mutex-unlock mx)
109 t)))
110
111(ert-deftest threads-mutex-recursive ()
112 "test mutex-lock and unlock"
113 (should
114 (let ((mx (make-mutex)))
115 (mutex-lock mx)
116 (mutex-lock mx)
117 (mutex-unlock mx)
118 (mutex-unlock mx)
119 t)))
120
121(defvar threads-mutex nil)
122(defvar threads-mutex-key nil)
123
124(defun threads-test-mlock ()
125 (mutex-lock threads-mutex)
126 (setq threads-mutex-key 23)
127 (while threads-mutex-key
128 (thread-yield))
129 (mutex-unlock threads-mutex))
130
131(ert-deftest threads-mutex-contention ()
132 "test of mutex contention"
133 (should
134 (progn
135 (setq threads-mutex (make-mutex))
136 (setq threads-mutex-key nil)
137 (make-thread #'threads-test-mlock)
138 ;; Wait for other thread to get the lock.
139 (while (not threads-mutex-key)
140 (thread-yield))
141 ;; Try now.
142 (setq threads-mutex-key nil)
143 (mutex-lock threads-mutex)
144 (mutex-unlock threads-mutex)
145 t)))
146
147(defun threads-test-mlock2 ()
148 (setq threads-mutex-key 23)
149 (mutex-lock threads-mutex))
150
151(ert-deftest threads-mutex-signal ()
152 "test signalling a blocked thread"
153 (should
154 (progn
155 (setq threads-mutex (make-mutex))
156 (setq threads-mutex-key nil)
157 (mutex-lock threads-mutex)
158 (let ((thr (make-thread #'threads-test-mlock2)))
159 (while (not threads-mutex-key)
160 (thread-yield))
161 (thread-signal thr 'quit nil)
162 (thread-join thr))
163 t)))
164
165;;; threads.el ends here