From patchwork Thu Mar 9 14:25:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomi Valkeinen X-Patchwork-Id: 18371 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 82885C32A1 for ; Thu, 9 Mar 2023 14:26:28 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4340362701; Thu, 9 Mar 2023 15:26:28 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1678371988; bh=3wTUx+H1Obw0NPayo4vNEyDPTT+A2Yvuao3QMWAHhyc=; 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=OxKZrk6q1BT0OovsoxzMT0KzC0ljGuPVK0NfyaFJPnbMIsUzon4/UWBKHWTxPhfrF JrmbcAwfQbJZt2YjNbUJh1LS6sP3wZIjEE0Iw+aT3qUw417857HRIA4JJelT7oH8kz jUtmC7WHZ97i2swOLlayj5zVobuqYGYk8hCiUSFW2HZGw0pyAltepYmN3DjWoRc5II jHl5F/NwztUoI/JUk4r0rxBU0tc/YgFM/z6ih21zZM7dh4PONcgpxh2XlYLxINa5+j TNM/4x33GvgDjQ8LzcmM5dXhzVk73pr+Idr0iT+8fkogZzGCfvoOsgenZYDa4oMClq +H/qzmLeEYwSg== 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 84517626EA for ; Thu, 9 Mar 2023 15:26:20 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="aOBTipCH"; dkim-atps=neutral Received: from desky.lan (91-154-32-225.elisa-laajakaista.fi [91.154.32.225]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 163658B1; Thu, 9 Mar 2023 15:26:20 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1678371980; bh=3wTUx+H1Obw0NPayo4vNEyDPTT+A2Yvuao3QMWAHhyc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aOBTipCHf0vohCFEK8GiGU3Sabp6oaCc8qWoc79XHFKlBqFj4OgxoCrY+66OThB5Y 5bx74k8Ks+h0zFzsg6Ed8+EMScKy0Y7/yZizuHQnrLr7dGE/0AIsHAiWozcy/aenPl pqBgmiEUOf2U3QKgvDuQe/kCMkKvfSRxf/ny/Z9I= To: libcamera-devel@lists.libcamera.org Date: Thu, 9 Mar 2023 16:25:56 +0200 Message-Id: <20230309142601.70556-11-tomi.valkeinen@ideasonboard.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230309142601.70556-1-tomi.valkeinen@ideasonboard.com> References: <20230309142601.70556-1-tomi.valkeinen@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 10/15] 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 fab791d1..3bd2d7fd 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() @@ -89,8 +101,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): @@ -110,11 +122,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) @@ -123,29 +135,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):