emacs/test/lisp/thread-tests.el

97 lines
3.4 KiB
EmacsLisp
Raw Normal View History

;;; thread-tests.el --- Test suite for thread.el -*- lexical-binding: t; -*-
2024-01-02 09:47:10 +08:00
;; Copyright (C) 2018-2024 Free Software Foundation, Inc.
;; Author: Gemini Lasswell <gazally@runbox.com>
;; Keywords: threads
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
(require 'ert)
(require 'thread)
;; Declare the functions used here in case Emacs has been configured
;; --without-threads.
(declare-function make-mutex "thread.c" (&optional name))
(declare-function mutex-lock "thread.c" (mutex))
(declare-function mutex-unlock "thread.c" (mutex))
(declare-function make-thread "thread.c" (function &optional name))
(declare-function thread-join "thread.c" (thread))
(declare-function thread-yield "thread.c" ())
(defvar thread-tests-flag)
(defvar thread-tests-mutex (when (featurep 'threads) (make-mutex "mutex1")))
(defun thread-tests--thread-function ()
(setq thread-tests-flag t)
(with-mutex thread-tests-mutex
(sleep-for 0.01)))
(ert-deftest thread-tests-thread-list-send-error ()
"A thread can be sent an error signal from the *Thread List* buffer."
(skip-unless (featurep 'threads))
(cl-letf (((symbol-function 'y-or-n-p) (lambda (_prompt) t)))
(with-mutex thread-tests-mutex
(setq thread-tests-flag nil)
(let ((thread (make-thread #'thread-tests--thread-function
"thread-tests-wait")))
(while (not thread-tests-flag)
(thread-yield))
(list-threads)
(goto-char (point-min))
(re-search-forward
"^thread-tests.+[[:blank:]]+Blocked[[:blank:]]+.+mutex1.+?")
(thread-list-send-error-signal)
(should-error (thread-join thread))
(list-threads)
(goto-char (point-min))
(should-error (re-search-forward "thread-tests"))))))
(ert-deftest thread-tests-thread-list-show-backtrace ()
"Show a backtrace for another thread from the *Thread List* buffer."
(skip-unless (featurep 'threads))
(let (thread)
(with-mutex thread-tests-mutex
(setq thread-tests-flag nil)
(setq thread
(make-thread #'thread-tests--thread-function "thread-tests-back"))
(while (not thread-tests-flag)
(thread-yield))
(list-threads)
(goto-char (point-min))
(re-search-forward
"^thread-tests.+[[:blank:]]+Blocked[[:blank:]]+.+mutex1.+?")
(thread-list-pop-to-backtrace)
(goto-char (point-min))
(re-search-forward "thread-tests-back")
(re-search-forward "mutex-lock")
(re-search-forward "thread-tests--thread-function"))
(thread-join thread)))
(ert-deftest thread-tests-list-threads-error-when-not-configured ()
"Signal an error running `list-threads' if threads are not configured."
Use new ERT `skip-when` macro in tests * test/lisp/autorevert-tests.el (auto-revert-test02-auto-revert-deleted-file): * test/lisp/emacs-lisp/benchmark-tests.el (benchmark-tests): * test/lisp/emacs-lisp/find-func-tests.el (find-func-tests--library-completion): * test/lisp/eshell/esh-proc-tests.el (esh-proc-test/kill-pipeline): * test/lisp/filenotify-tests.el (file-notify-test11-symlinks): * test/lisp/ibuffer-tests.el (ibuffer-0autoload): * test/lisp/international/ucs-normalize-tests.el (ucs-normalize-part1): * test/lisp/net/network-stream-tests.el (echo-server-nowait) (connect-to-tls-ipv4-nowait, connect-to-tls-ipv6-nowait) (open-network-stream-tls-wait, open-network-stream-tls-nowait) (open-network-stream-tls, open-network-stream-tls-nocert) (open-gnutls-stream-new-api-nowait) (open-gnutls-stream-old-api-nowait): * test/lisp/progmodes/cperl-mode-tests.el (cperl-test-bug-10483) (cperl-test-hyperactive-electric-else): * test/lisp/progmodes/elisp-mode-tests.el (eval-last-sexp-print-format-sym-echo) (eval-last-sexp-print-format-small-int-echo) (eval-last-sexp-print-format-large-int-echo) (eval-defun-prints-edebug-when-instrumented): * test/lisp/progmodes/python-tests.el (python-ffap-module-path-1): * test/lisp/shadowfile-tests.el (shadow-test00-clusters) (shadow-test01-sites, shadow-test02-files) (shadow-test03-expand-cluster-in-file-name) (shadow-test04-contract-file-name, shadow-test05-file-match) (shadow-test06-literal-groups, shadow-test07-regexp-groups) (shadow-test08-shadow-todo, shadow-test09-shadow-copy-files): * test/lisp/simple-tests.el (eval-expression-print-format-sym-echo) (eval-expression-print-format-small-int-echo) (eval-expression-print-format-large-int-echo): * test/lisp/term-tests.el (term-simple-lines) (term-carriage-return, term-line-wrap, term-colors) (term-colors-bold-is-bright, term-cursor-movement) (term-scrolling-region, term-set-directory) (term-line-wrapping-then-motion, term-to-margin): * test/lisp/thread-tests.el (thread-tests-list-threads-error-when-not-configured): * test/lisp/vc/vc-tests.el (backend): * test/manual/scroll-tests.el (scroll-tests-scroll-margin-0) (scroll-tests-scroll-margin-negative) (scroll-tests-scroll-margin-max) (scroll-tests-scroll-margin-over-max) (scroll-tests-scroll-margin-whole-window): * test/misc/test-custom-libs.el (test-custom-libs): * test/src/emacs-module-tests.el (module/async-pipe): * test/src/fileio-tests.el (fileio-tests--odd-symlink-chars): * test/src/filelock-tests.el (filelock-tests-lock-spoiled) (filelock-tests-file-locked-p-spoiled) (filelock-tests-unlock-spoiled) (filelock-tests-kill-buffer-spoiled) (filelock-tests-detect-external-change): * test/src/image-tests.el (image-tests-image-size/error-on-nongraphical-display) (image-tests-image-mask-p/error-on-nongraphical-display) (image-tests-image-metadata/error-on-nongraphical-display): * test/src/process-tests.el (make-process/mix-stderr) (process-tests/fd-setsize-no-crash/make-network-process) (process-tests/fd-setsize-no-crash/make-serial-process): Use ERT `skip-when` macro in tests.
2023-09-03 21:04:02 +02:00
(skip-when (featurep 'threads))
(should-error (list-threads)))
(provide 'thread-tests)
;;; thread-tests.el ends here