From patchwork Wed Jul 10 19:17:06 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 1647 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 882BF6157B for ; Wed, 10 Jul 2019 21:17:46 +0200 (CEST) Received: from pendragon.ideasonboard.com (softbank126163157105.bbtec.net [126.163.157.105]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 5E75354B for ; Wed, 10 Jul 2019 21:17:45 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1562786266; bh=nClxrVfSMwnbHVqa+iOoMRsPGU5WwfEC/3Y5sbTELjA=; h=From:To:Subject:Date:In-Reply-To:References:From; b=G8ui7RpwGFkdAVs2gkFyuPceDu9wDI91B8VQdZg5jnSXxtVZbRp5k1h872jdYtzwX Ag+NCYFmJ8ZjzvGwTk6SfcZPbbc61KQRLT1HzYqqDfl+V2Hg2tPNbHCAMk+zX274fc judFnB4tn0XuUrmeH+T4LRBrQKsmzPid3cDnW3os= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Wed, 10 Jul 2019 22:17:06 +0300 Message-Id: <20190710191708.13049-4-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190710191708.13049-1-laurent.pinchart@ideasonboard.com> References: <20190710191708.13049-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 4/6] test: Add Thread test cases X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 10 Jul 2019 19:17:46 -0000 The Thread test case verifies that - a Thread instance is created for the main thread - a new Thread can be created, started, and stopped Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund --- test/meson.build | 1 + test/threads.cpp | 93 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 test/threads.cpp diff --git a/test/meson.build b/test/meson.build index 41b54ffd358c..bd0e0d98f0e7 100644 --- a/test/meson.build +++ b/test/meson.build @@ -21,6 +21,7 @@ public_tests = [ internal_tests = [ ['camera-sensor', 'camera-sensor.cpp'], + ['threads', 'threads.cpp'], ] foreach t : public_tests diff --git a/test/threads.cpp b/test/threads.cpp new file mode 100644 index 000000000000..9a2d39dfd106 --- /dev/null +++ b/test/threads.cpp @@ -0,0 +1,93 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * threads.cpp - Threads test + */ + +#include +#include +#include + +#include "thread.h" +#include "test.h" + +using namespace std; +using namespace libcamera; + +class InstrumentedThread : public Thread +{ +public: + InstrumentedThread(unsigned int iterations) + : iterations_(iterations) + { + } + +protected: + void run() + { + for (unsigned int i = 0; i < iterations_; ++i) { + this_thread::sleep_for(chrono::milliseconds(50)); + } + } + +private: + unsigned int iterations_; +}; + +class ThreadTest : public Test +{ +protected: + int init() + { + return 0; + } + + int run() + { + /* Test Thread() retrieval for the main thread. */ + Thread *thread = Thread::current(); + if (!thread) { + cout << "Thread::current() failed to main thread" + << endl; + return TestFail; + } + + if (!thread->isRunning()) { + cout << "Main thread is not running" << endl; + return TestFail; + } + + /* Test starting the main thread, the test shall not crash. */ + thread->start(); + + /* Test the running state of a custom thread. */ + thread = new Thread(); + thread->start(); + + if (!thread->isRunning()) { + cout << "Thread is not running after being started" + << endl; + return TestFail; + } + + thread->exit(0); + thread->wait(); + + if (thread->isRunning()) { + cout << "Thread is still running after finishing" + << endl; + return TestFail; + } + + delete thread; + + return TestPass; + } + + void cleanup() + { + } +}; + +TEST_REGISTER(ThreadTest)