From patchwork Thu Jan 23 02:55:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 2731 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 0BEC36044D for ; Thu, 23 Jan 2020 03:55:40 +0100 (CET) Received: from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi [81.175.216.236]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 92DB19C0 for ; Thu, 23 Jan 2020 03:55:39 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1579748139; bh=rqG8akmlAjZy2QTAXPrqOlhG3XD6FWfBZRZzqQe47p8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=oenQHnlGGqLJJodmfaAexMs0uvghL5aQPia6q+LoYlgPwaBCq0eFaEedc9o1DdEYy gUNxyEXafBxGOYz2IawS2fMJNP1/GCS9b406v6A/RTFRBT5LotHDuWlt2ZFyJb4pOC vWQsvKOwFQP/pePip906G/2TE3/FBKUHAanwFRXk= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Thu, 23 Jan 2020 04:55:20 +0200 Message-Id: <20200123025520.12149-2-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200123025520.12149-1-laurent.pinchart@ideasonboard.com> References: <20200123025520.12149-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 2/2] test: threads: Add wait() timeout test X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 23 Jan 2020 02:55:40 -0000 Add a test case to wait with a timeout, testing both a too short and a long enough duration. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham Reviewed-by: Niklas Söderlund --- test/threads.cpp | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/test/threads.cpp b/test/threads.cpp index 9a2d39dfd106..fc4b07cc6428 100644 --- a/test/threads.cpp +++ b/test/threads.cpp @@ -15,24 +15,22 @@ using namespace std; using namespace libcamera; -class InstrumentedThread : public Thread +class DelayThread : public Thread { public: - InstrumentedThread(unsigned int iterations) - : iterations_(iterations) + DelayThread(chrono::steady_clock::duration duration) + : duration_(duration) { } protected: void run() { - for (unsigned int i = 0; i < iterations_; ++i) { - this_thread::sleep_for(chrono::milliseconds(50)); - } + this_thread::sleep_for(duration_); } private: - unsigned int iterations_; + chrono::steady_clock::duration duration_; }; class ThreadTest : public Test @@ -82,6 +80,27 @@ protected: delete thread; + /* Test waiting for completion with a timeout. */ + thread = new DelayThread(chrono::milliseconds(500)); + thread->start(); + thread->exit(0); + + bool timeout = thread->wait(chrono::milliseconds(100)); + + if (!timeout) { + cout << "Waiting for thread didn't time out" << endl; + return TestFail; + } + + timeout = thread->wait(chrono::milliseconds(1000)); + + if (timeout) { + cout << "Waiting for thread timed out" << endl; + return TestFail; + } + + delete thread; + return TestPass; }