From patchwork Sat Jun 3 07:56:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomi Valkeinen X-Patchwork-Id: 18696 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 67A9DC3200 for ; Sat, 3 Jun 2023 07:57:22 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id E017862896; Sat, 3 Jun 2023 09:57:21 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1685779041; bh=xnsU+dBTrM++WqFLAtAgLiiKiQrid4kqBDwvRdKooBA=; 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=u45RAwiTEFc6Jj1MK5/1qtNaV7r1RWfeZ3la+9B92eu4gk2cSZAJyT7zDIXSbqW8N huWdhmX8fGNb27zr1r1Pv5nedyXhNNdxxS+pUg5qygL/vidD+iduno9MpOMHOgvpWB HkRaMcauN7yV53aol6jTsx+818ulP6hfOsK2Aez5MN0R/+zDYJJNqsiobEaTVB0k6g fo4rLPXkdWyiWteh6fGaWeP7v1JA3KG4Ryqgo8tBdDUB2i239Mb3PEmMn77WIp0JSW afsxhBdIYDzv3fUVwh8//kCjR/No+sJBwQYxEQXwP9yuuKQqUP9eN26ZqZAIlJ36Or 2yMmcEIxJyJKw== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 8F76F62709 for ; Sat, 3 Jun 2023 09:57:15 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="NhaF/WZy"; 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 1871BB2A; Sat, 3 Jun 2023 09:56:52 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1685779012; bh=xnsU+dBTrM++WqFLAtAgLiiKiQrid4kqBDwvRdKooBA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NhaF/WZyL/yCatPWzz3l3yR/RNfJhbvkoPpcO1BlYbekr+IOZrnEYr/W4NiBOQvPj cFTUyP/PDyyqBx2XsjRIpQju3CiYSMePUz7ccOugeua1JuRyMm4KcIqX+ijGZ6bS1q 3KftdmDGTt7gfngpFTqmUC+qDaj5gq+ihMvgRaug= To: libcamera-devel@lists.libcamera.org Date: Sat, 3 Jun 2023 10:56:13 +0300 Message-Id: <20230603075615.20663-12-tomi.valkeinen@ideasonboard.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230603075615.20663-1-tomi.valkeinen@ideasonboard.com> References: <20230603075615.20663-1-tomi.valkeinen@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v5 11/13] py: unittests.py: Add test for refs & keep-alives 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 a test for references and keep-alives by doing a capture, reusing the Requests once and testing that the objects are freed as soon as all the refs and keep-alives are gone. Signed-off-by: Tomi Valkeinen Reviewed-by: Laurent Pinchart --- test/py/unittests.py | 128 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/test/py/unittests.py b/test/py/unittests.py index 0c844a20..024d9752 100755 --- a/test/py/unittests.py +++ b/test/py/unittests.py @@ -31,6 +31,134 @@ class BaseTestCase(unittest.TestCase): self.assertTrue(all([not wr() for wr in wr_list]), msg) +# Test references and keep-alives by doing a capture, reusing the Requests once +# and testing that the objects are freed as soon as all the refs and keep-alives +# are gone. +class CaptureRefTestMethods(BaseTestCase): + def test_ref(self): + cm = libcam.CameraManager.singleton() + wr_cm = weakref.ref(cm) + + cam = cm.get('platform/vimc.0 Sensor B') + self.assertIsNotNone(cam) + wr_cam = weakref.ref(cam) + + cam.acquire() + + camconfig = cam.generate_configuration([libcam.StreamRole.StillCapture]) + self.assertTrue(camconfig.size == 1) + wr_camconfig = weakref.ref(camconfig) + + streamconfig = camconfig.at(0) + wr_streamconfig = weakref.ref(streamconfig) + + cam.configure(camconfig) + + stream = streamconfig.stream + wr_stream = weakref.ref(stream) + + # stream keeps streamconfig and camconfig alive + del streamconfig + del camconfig + gc.collect() + self.assertIsAlive(wr_camconfig) + self.assertIsAlive(wr_streamconfig) + + allocator = libcam.FrameBufferAllocator(cam) + num_bufs = allocator.allocate(stream) + self.assertTrue(num_bufs > 0) + wr_allocator = weakref.ref(allocator) + + buffers = allocator.buffers(stream) + self.assertIsNotNone(buffers) + + wr_buffers = [weakref.ref(b) for b in buffers] + + del allocator + self.assertIsAlive(wr_allocator) + + reqs = [] + wr_reqs = [] + for i in range(num_bufs): + req = cam.create_request(i) + self.assertIsNotNone(req) + + wr_reqs.append(weakref.ref(req)) + + req.add_buffer(stream, buffers[i]) + + reqs.append(req) + + del buffers + del stream + + self.assertIsDead(wr_stream) + + cam.start() + + reqs_target = num_bufs * 2 + reqs_queued = 0 + reqs_captured = 0 + + for req in reqs: + cam.queue_request(req) + reqs_queued += 1 + + del req + del reqs + + # All buffers and reqs should be alive + self.assertIsAllAlive(wr_buffers) + self.assertIsAllAlive(wr_reqs) + + sel = selectors.DefaultSelector() + sel.register(cm.event_fd, selectors.EVENT_READ) + + while True: + events = sel.select() + if not events: + continue + del events + + for ev in cm.get_events(): + self.assertEqual(ev.type, libcam.Event.Type.RequestCompleted) + + reqs_captured += 1 + self.assertLessEqual(reqs_captured, reqs_target) + + if reqs_queued < reqs_target: + req: libcam.Request = typing.cast(libcam.Request, ev.request) + req.reuse() + cam.queue_request(req) + reqs_queued += 1 + del req + + del ev + + if reqs_captured == reqs_target: + break + + del sel + + # The allocator and all buffers and reqs should be dead + self.assertIsAllDead(wr_buffers) + self.assertIsAllDead(wr_reqs) + self.assertIsDead(wr_allocator) + + events = cam.stop() + self.assertZero(len(events)) + del events + cam.release() + + del cm + self.assertIsAlive(wr_cm) + self.assertIsAlive(wr_cam) + + del cam + self.assertIsDead(wr_cam) + self.assertIsDead(wr_cm) + + class SimpleTestMethods(BaseTestCase): def test_get_ref(self): cm = libcam.CameraManager.singleton()