[libcamera-devel,v4,10/15] py: unittests.py: Add weakref helpers and use del
diff mbox series

Message ID 20230309142601.70556-11-tomi.valkeinen@ideasonboard.com
State Accepted
Headers show
Series
  • py: New python bindings event handling
Related show

Commit Message

Tomi Valkeinen March 9, 2023, 2:25 p.m. UTC
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 <tomi.valkeinen@ideasonboard.com>
---
 test/py/unittests.py | 60 ++++++++++++++++++++++++++------------------
 1 file changed, 36 insertions(+), 24 deletions(-)

Comments

Laurent Pinchart March 12, 2023, 2:54 p.m. UTC | #1
Hi Tomi,

Thank you for the patch.

On Thu, Mar 09, 2023 at 04:25:56PM +0200, Tomi Valkeinen via libcamera-devel wrote:
> 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 <tomi.valkeinen@ideasonboard.com>

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> ---
>  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):

Patch
diff mbox series

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):