From patchwork Fri Jul 31 15:14:24 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 9097 X-Patchwork-Delegate: laurent.pinchart@ideasonboard.com Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 1EAE7BD878 for ; Fri, 31 Jul 2020 15:15:42 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A11E561DE1; Fri, 31 Jul 2020 17:15:41 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="ADvE19ji"; dkim-atps=neutral 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 84AC160398 for ; Fri, 31 Jul 2020 17:15:40 +0200 (CEST) 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 AEFB353C; Fri, 31 Jul 2020 17:15:39 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596208539; bh=jsaOI15SXIosTpJEs0q1SE8kaVTm+N7BCr3ISUpBs6o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ADvE19jiDI835kuYkOx4SZ8xV9mpCN/bZlR6Ajg2CunFQ1W1T4fXsXUm2ZPr6F+Qk 2GmGfs6dldZd8I1uXN099bqZl6SzI8KDFuGiC4xW1iwfKRF+virhGSX0sOICi8RwNb uA/LWq/kUpz47rMh5TD2NKUHfWARNv7pGfdSnAgQ= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Fri, 31 Jul 2020 18:14:24 +0300 Message-Id: <20200731151424.28003-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200731133947.84258-1-email@uajain.com> References: <20200731133947.84258-1-email@uajain.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3.1 4/3] test: Add Object::deleteLater() 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: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" From: Umang Jain Add a test case for the Object::deleteLater() API, to verify that - the object is deleted from the correct thread - multiple deleteLater() calls delete the object once only Signed-off-by: Umang Jain Reviewed-by: Laurent Pinchart Signed-off-by: Laurent Pinchart --- test/meson.build | 1 + test/object-delete.cpp | 97 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 test/object-delete.cpp diff --git a/test/meson.build b/test/meson.build index c0fb9bdfac17..775187159dec 100644 --- a/test/meson.build +++ b/test/meson.build @@ -33,6 +33,7 @@ internal_tests = [ ['hotplug-cameras', 'hotplug-cameras.cpp'], ['message', 'message.cpp'], ['object', 'object.cpp'], + ['object-delete', 'object-delete.cpp'], ['object-invoke', 'object-invoke.cpp'], ['pixel-format', 'pixel-format.cpp'], ['signal-threads', 'signal-threads.cpp'], diff --git a/test/object-delete.cpp b/test/object-delete.cpp new file mode 100644 index 000000000000..a1a6f5ccefd5 --- /dev/null +++ b/test/object-delete.cpp @@ -0,0 +1,97 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2020, Google Inc. + * + * object.cpp - Object deletion tests + */ + +#include + +#include + +#include "libcamera/internal/thread.h" + +#include "test.h" + +using namespace std; +using namespace libcamera; + +class TestObject : public Object +{ +public: + TestObject(unsigned int *count) + : deleteCount_(count) + { + } + + ~TestObject() + { + /* Count the deletions from the correct thread. */ + if (thread() == Thread::current()) + (*deleteCount_)++; + } + + unsigned int *deleteCount_; +}; + +class NewThread : public Thread +{ +public: + NewThread(Object *obj) + : object_(obj) + { + } + +protected: + void run() + { + object_->deleteLater(); + } + +private: + Object *object_; +}; + +class ObjectDeleteTest : public Test +{ +protected: + int run() + { + /* + * Test that deferred deletion is executed from the object's + * thread, not the caller's thread. + */ + unsigned int count = 0; + TestObject *obj = new TestObject(&count); + + NewThread thread(obj); + thread.start(); + thread.wait(); + + Thread::current()->dispatchMessages(Message::Type::DeferredDelete); + + if (count != 1) { + cout << "Failed to dispatch DeferredDelete (" << count << ")" << endl; + return TestFail; + } + + /* + * Test that multiple calls to deleteLater() delete the object + * once only. + */ + count = 0; + obj = new TestObject(&count); + obj->deleteLater(); + obj->deleteLater(); + + Thread::current()->dispatchMessages(Message::Type::DeferredDelete); + if (count != 1) { + cout << "Multiple deleteLater() failed (" << count << ")" << endl; + return TestFail; + } + + return TestPass; + } +}; + +TEST_REGISTER(ObjectDeleteTest)