tests: Add base class for fixtures that generate a target file
authorNikos Nikoleris <nikos.nikoleris@arm.com>
Tue, 18 Jun 2019 10:47:22 +0000 (11:47 +0100)
committerNikos Nikoleris <nikos.nikoleris@arm.com>
Fri, 9 Aug 2019 10:55:19 +0000 (10:55 +0000)
The new TargetFixture can be used as a base class for fixtures that
generate/download a file. These fixtures are guarrantied to be unique
and their setup function is only executed once.

Change-Id: I6a8737b06c4e74f3e29736ec363f61251d85da8c
Signed-off-by: Nikos Nikoleris <nikos.nikoleris@arm.com>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/19250
Reviewed-by: Giacomo Travaglini <giacomo.travaglini@arm.com>
Reviewed-by: Jason Lowe-Power <jason@lowepower.com>
Maintainer: Jason Lowe-Power <jason@lowepower.com>
Tested-by: kokoro <noreply+kokoro@google.com>
tests/gem5/fixture.py

index 00f43ef6af79af77f199b22482da74ae16326c8e..b04a334b0d0c1639cba84cc4f2fd394c8b587a7b 100644 (file)
@@ -1,3 +1,15 @@
+# Copyright (c) 2019 ARM Limited
+# All rights reserved
+#
+# The license below extends only to copyright in the software and shall
+# not be construed as granting a license to any other intellectual
+# property including but not limited to intellectual property relating
+# to a hardware implementation of the functionality of the software
+# licensed hereunder.  You may use the software subject to the license
+# terms below provided that you ensure that this notice is replicated
+# unmodified and in its entirety in all distributions of the software,
+# modified or unmodified, in source code or in binary form.
+#
 # Copyright (c) 2017 Mark D. Hill and David A. Wood
 # All rights reserved.
 #
@@ -29,6 +41,7 @@
 import os
 import tempfile
 import shutil
+import threading
 
 from testlib.fixture import Fixture, globalfixture
 from testlib.config import config, constants
@@ -59,6 +72,45 @@ class TempdirFixture(Fixture):
         # Set path to none so it's not deleted
         self.path = None
 
+class UniqueFixture(Fixture):
+    '''
+    Base class for fixtures that generate a target in the
+    filesystem. If the same fixture is used by more than one
+    test/suite, rather than creating a copy of the fixture, it returns
+    the same object and makes sure that setup is only executed
+    once. Devired classses should override the _init and _setup
+    functions.
+
+    :param target: The absolute path of the target in the filesystem.
+
+    '''
+    fixtures = {}
+
+    def __new__(cls, target):
+        if target in cls.fixtures:
+            obj = cls.fixtures[target]
+        else:
+            obj = super(UniqueFixture, cls).__new__(cls)
+            obj.lock = threading.Lock()
+            obj.target = target
+            cls.fixtures[target] = obj
+        return obj
+
+    def __init__(self, *args, **kwargs):
+        with self.lock:
+            if hasattr(self, '_init_done'):
+                return
+            super(UniqueFixture, self).__init__(self, **kwargs)
+            self._init(*args, **kwargs)
+            self._init_done = True
+
+    def setup(self, testitem):
+        with self.lock:
+            if hasattr(self, '_setup_done'):
+                return
+            self._setup_done = True
+            self._setup(testitem)
+
 
 class SConsFixture(Fixture):
     '''