From patchwork Tue May 30 09:20:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomi Valkeinen X-Patchwork-Id: 18660 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 12A73C328E for ; Tue, 30 May 2023 09:21:03 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id C2187627B1; Tue, 30 May 2023 11:21:02 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1685438462; bh=etUTNpglDs12cp0qIBTbbGRzSHyx4+IT4y7n3WHxxm8=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=kCbH1B0cTEkX3Dzl769I0UKXofizDzM0oyZYIZ/0qyJeAzc9ziqCpyaIwHkjshYWm JVOGBpfTWEdLd851Jru6J1z3TDIPrhvO9FDz+8MTs17aUz6K5RItVwps3tJaXwPrhE NY2fLYxqB6R36e8MeOs7GBQtnJtGezNan37SzOpUgQubuXHK8bAmw0CJt2lVWXKUGp ErhsQv0RlY/jvGKddmH3Tc4X8A1S9zPnB4s6C8aOTq8eWzVlr9ymSom1FtqrreUbvY eV5Z7grdPz/i62tPrXH8LUy1AmWG9foMcNejcYesKUwb5qH1F2ExhUFQ4d7ObYU10r BUu8H84QVDrJg== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id E9B06627DE for ; Tue, 30 May 2023 11:20:57 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="lAZ+XQ8M"; dkim-atps=neutral Received: from desky.lan (91-154-35-171.elisa-laajakaista.fi [91.154.35.171]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 02BB7AB; Tue, 30 May 2023 11:20:36 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1685438437; bh=etUTNpglDs12cp0qIBTbbGRzSHyx4+IT4y7n3WHxxm8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lAZ+XQ8MFSVDhp2KY9YmuIkGxgAmNvRn2BkGd5tDiYfJUlbWv5ERRAJ9p9/3Uz3lL Cj0HHaRPtjwtIpTcKuu2XorPkFRbbjWyhBlbFb+Hl/FcwpievyeFCXtjxTKBWkqZX+ vchiK/wp6gmeBIhaKi3Mxz3XQfDzkSadZYFw6FOE= To: libcamera-devel@lists.libcamera.org Date: Tue, 30 May 2023 12:20:15 +0300 Message-Id: <20230530092016.34953-5-tomi.valkeinen@ideasonboard.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230530092016.34953-1-tomi.valkeinen@ideasonboard.com> References: <20230530092016.34953-1-tomi.valkeinen@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 4/5] py: unittests.py: Add weakref helpers and use del 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-Patchwork-Original-From: Tomi Valkeinen via libcamera-devel From: Tomi Valkeinen Reply-To: Tomi Valkeinen Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add helpers to check if a weakref or a list of weakrefs is alive or dead. Also use 'del' for local variables instead of setting the variable to None. This makes debugging the test easier as the locals will be gone from locals() dict. Signed-off-by: Tomi Valkeinen Reviewed-by: Laurent Pinchart --- test/py/unittests.py | 60 ++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/test/py/unittests.py b/test/py/unittests.py index 0057ec92..fe1e8ef0 100755 --- a/test/py/unittests.py +++ b/test/py/unittests.py @@ -18,6 +18,18 @@ class BaseTestCase(unittest.TestCase): def assertZero(self, a, msg=None): self.assertEqual(a, 0, msg) + def assertIsAlive(self, wr, msg='object not alive'): + self.assertIsNotNone(wr(), msg) + + def assertIsDead(self, wr, msg='object not dead'): + self.assertIsNone(wr(), msg) + + def assertIsAllAlive(self, wr_list, msg='object not alive'): + self.assertTrue(all([wr() for wr in wr_list]), msg) + + def assertIsAllDead(self, wr_list, msg='object not dead'): + self.assertTrue(all([not wr() for wr in wr_list]), msg) + class SimpleTestMethods(BaseTestCase): def test_get_ref(self): @@ -28,14 +40,14 @@ class SimpleTestMethods(BaseTestCase): self.assertIsNotNone(cam) wr_cam = weakref.ref(cam) - cm = None + del cm gc.collect() - self.assertIsNotNone(wr_cm()) + self.assertIsAlive(wr_cm) - cam = None + del cam gc.collect() - self.assertIsNone(wr_cm()) - self.assertIsNone(wr_cam()) + self.assertIsDead(wr_cm) + self.assertIsDead(wr_cam) def test_acquire_release(self): cm = libcam.CameraManager.singleton() @@ -93,8 +105,8 @@ class CameraTesterBase(BaseTestCase): self.cam = None self.cm = None - self.assertIsNone(self.wr_cm()) - self.assertIsNone(self.wr_cam()) + self.assertIsDead(self.wr_cm) + self.assertIsDead(self.wr_cam) class AllocatorTestMethods(CameraTesterBase): @@ -114,11 +126,11 @@ class AllocatorTestMethods(CameraTesterBase): wr_stream = weakref.ref(stream) # stream should keep streamconfig and camconfig alive - streamconfig = None - camconfig = None + del streamconfig + del camconfig gc.collect() - self.assertIsNotNone(wr_camconfig()) - self.assertIsNotNone(wr_streamconfig()) + self.assertIsAlive(wr_camconfig) + self.assertIsAlive(wr_streamconfig) allocator = libcam.FrameBufferAllocator(cam) num_bufs = allocator.allocate(stream) @@ -127,29 +139,29 @@ class AllocatorTestMethods(CameraTesterBase): buffers = allocator.buffers(stream) self.assertIsNotNone(buffers) - buffers = None + del buffers buffer = allocator.buffers(stream)[0] self.assertIsNotNone(buffer) wr_buffer = weakref.ref(buffer) - allocator = None + del allocator gc.collect() - self.assertIsNotNone(wr_buffer()) - self.assertIsNotNone(wr_allocator()) - self.assertIsNotNone(wr_stream()) + self.assertIsAlive(wr_buffer) + self.assertIsAlive(wr_allocator) + self.assertIsAlive(wr_stream) - buffer = None + del buffer gc.collect() - self.assertIsNone(wr_buffer()) - self.assertIsNone(wr_allocator()) - self.assertIsNotNone(wr_stream()) + self.assertIsDead(wr_buffer) + self.assertIsDead(wr_allocator) + self.assertIsAlive(wr_stream) - stream = None + del stream gc.collect() - self.assertIsNone(wr_stream()) - self.assertIsNone(wr_camconfig()) - self.assertIsNone(wr_streamconfig()) + self.assertIsDead(wr_stream) + self.assertIsDead(wr_camconfig) + self.assertIsDead(wr_streamconfig) class SimpleCaptureMethods(CameraTesterBase):