From patchwork Fri Jul 31 18:14:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 9121 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 2F4F9BD86F for ; Fri, 31 Jul 2020 18:14:23 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id F0507616FF; Fri, 31 Jul 2020 20:14:22 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=uajain.com header.i=@uajain.com header.b="HiR6M8Hg"; dkim-atps=neutral Received: from o1.f.az.sendgrid.net (o1.f.az.sendgrid.net [208.117.55.132]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D04E7611A2 for ; Fri, 31 Jul 2020 20:14:19 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uajain.com; h=from:subject:in-reply-to:references:mime-version:to:cc: content-transfer-encoding:content-type; s=s1; bh=tliDj6tjrNI3aLDqSMiIZntu529xthAawTbKFHUaav4=; b=HiR6M8HgbMT7x+2e5eCyByFls3nWGYSP/P0K+UBdZUiBjiT+B0KsyJ/NFHSKpHyoRCc8 xgYDJhtusorbKnqRTZOuoR6k1K4eIac3KeKb2XuCnNNSsl+1vx6P0HNMghnsB/OrSQHevp fB3S19Nf92bT5s7IuK/nM2dNnYV+b8ETA= Received: by filterdrecv-p3mdw1-canary-6c6ff5ff56-ngzws with SMTP id filterdrecv-p3mdw1-canary-6c6ff5ff56-ngzws-19-5F245F7A-2B 2020-07-31 18:14:18.684604995 +0000 UTC m=+176754.616199453 Received: from mail.uajain.com (unknown) by ismtpd0004p1maa1.sendgrid.net (SG) with ESMTP id 9Pe3IcQ-QciL4abFBGXmbw Fri, 31 Jul 2020 18:14:18.296 +0000 (UTC) From: Umang Jain Date: Fri, 31 Jul 2020 18:14:18 +0000 (UTC) Message-Id: <20200731181410.99892-4-email@uajain.com> In-Reply-To: <20200731181410.99892-1-email@uajain.com> References: <20200731181410.99892-1-email@uajain.com> Mime-Version: 1.0 X-SG-EID: 1Q40EQ7YGir8a9gjSIAdTjhngY657NMk9ckeo4dbHZDiOpywc/L3L9rFqlwE4KPcGYE5Iq+t2q0vZlJgodhIO7E4tCmj/R8GiO7znSV98Wx6NqD26y/vMGp0bm+HFnOnXp3b40OX0xIhBt9Xd8DLzOlb1OTvR86TGb2GbpgUxMQ6Bs0oV6a+H7+669T9knL/S3bQQTreuv4jAPGGfk6pRKQ57K4zaXnoamZbCFPiIBiLurGt2hAU1CiF0j4Aox4S9ZnnuYHJisa9qteZgM8HNYyLcAkz4IMW3Id1Vmk9UJw= To: libcamera-devel@lists.libcamera.org Subject: [libcamera-devel] [PATCH v4 3/4] tests: Add a test case for the Object::deleteLater() API, to verify 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" - 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 f41d6e7..f5aa144 100644 --- a/test/meson.build +++ b/test/meson.build @@ -34,6 +34,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 0000000..a1a6f5c --- /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)