[Piglit] [PATCH 4/9] TestrunResult: Merge parseFile into the constructor

Dylan Baker baker.dylan.c at gmail.com
Tue Oct 15 12:13:26 CEST 2013


This adds an optional argument to the constructor for a results file.
This fixes a race condition where a TestrunResult is created, some
entries are added, and then parseFile is called, which overwrites the
contents of the TestrunResult.

Signed-off-by: Dylan Baker <baker.dylan.c at gmail.com>
---
 framework/core.py    | 45 ++++++++++++++++++++++-----------------------
 framework/summary.py |  4 ++--
 2 files changed, 24 insertions(+), 25 deletions(-)

diff --git a/framework/core.py b/framework/core.py
index 752d98c..f003a44 100644
--- a/framework/core.py
+++ b/framework/core.py
@@ -278,7 +278,7 @@ class GroupResult(dict):
 
 
 class TestrunResult:
-    def __init__(self):
+    def __init__(self, resultfile=None):
         self.serialized_keys = ['options',
                                 'name',
                                 'tests',
@@ -292,6 +292,25 @@ class TestrunResult:
         self.time_elapsed = None
         self.tests = {}
 
+        if resultfile:
+            # Attempt to open the json file normally, if it fails then attempt
+            # to repair it.
+            try:
+                raw_dict = json.load(resultfile)
+            except ValueError:
+                raw_dict = json.load(self.__repairFile(resultfile))
+
+            # Check that only expected keys were unserialized.
+            for key in raw_dict:
+                if key not in self.serialized_keys:
+                    raise Exception('unexpected key in results file: ', str(key))
+
+            self.__dict__.update(raw_dict)
+
+            # Replace each raw dict in self.tests with a TestResult.
+            for (path, result) in self.tests.items():
+                self.tests[path] = TestResult(result)
+
     def __repairFile(self, file):
         '''
         Reapair JSON file if necessary
@@ -360,25 +379,6 @@ class TestrunResult:
         raw_dict = dict([(k, self.__dict__[k]) for k in keys])
         json.dump(raw_dict, file, indent=JSONWriter.INDENT)
 
-    def parseFile(self, file):
-        # Attempt to open the json file normally, if it fails then attempt to
-        # repair it.
-        try:
-            raw_dict = json.load(file)
-        except ValueError:
-            raw_dict = json.load(self.__repairFile(file))
-
-        # Check that only expected keys were unserialized.
-        for key in raw_dict:
-            if key not in self.serialized_keys:
-                raise Exception('unexpected key in results file: ', str(key))
-
-        self.__dict__.update(raw_dict)
-
-        # Replace each raw dict in self.tests with a TestResult.
-        for (path, result) in self.tests.items():
-            self.tests[path] = TestResult(result)
-
 
 class Environment:
     def __init__(self, concurrent=True, execute=True, include_filter=[],
@@ -616,10 +616,9 @@ def loadTestResults(relativepath):
     else:
         filepath = path
 
-    testrun = TestrunResult()
     try:
-        with open(filepath, 'r') as file:
-            testrun.parseFile(file)
+        with open(filepath, 'r') as resultfile:
+            testrun = TestrunResult(resultfile)
     except OSError:
         traceback.print_exc()
         raise Exception('Could not read tests results')
diff --git a/framework/summary.py b/framework/summary.py
index 1a15fa7..7afbe7b 100644
--- a/framework/summary.py
+++ b/framework/summary.py
@@ -43,10 +43,10 @@ def load_result(resultfile):
     # is a folder containing a json file
     try:
         with open(resultfile, 'r') as file:
-            result.parseFile(file)
+            result = TestrunResult(file)
     except IOError:
         with open(path.join(resultfile, 'main'), 'r') as file:
-            result.parseFile(file)
+            result = TestrunResult(file)
 
     return result
 
-- 
1.8.1.5



More information about the Piglit mailing list