[Piglit] [PATCH v2] core.py: Fix sporadic loss of tests in summary

Dylan Baker baker.dylan.c at gmail.com
Fri May 31 18:31:09 PDT 2013


The checking in TestrunResult to determine if a file was proper json or
not is very fragile: it assumes that the last line of the file will be
}. Sometimes this triggers and rebuilds a valid json file, dropping the
last test and resulting in python overhead.

This patch replaces that check with a try/except block. This block
attempts to load the json file with json.loads, and on failure attempts
to fix the json.

V2: - replaces json.loads with json.load: this was causing an error but
	  being caught by the except block.
	- Corrects issue created by the try block chaging file.tell() so
	  that file.seek would fail in self.__repairFile()

Signed-off-by: Dylan Baker <baker.dylan.c at gmail.com>
---
 framework/core.py | 46 ++++++++++++++++++++++++++--------------------
 1 file changed, 26 insertions(+), 20 deletions(-)

diff --git a/framework/core.py b/framework/core.py
index 14a8161..f415508 100644
--- a/framework/core.py
+++ b/framework/core.py
@@ -297,14 +297,8 @@ class TestrunResult:
                 is returned.
         '''
 
-        saved_position = file.tell()
+        file.seek(0)
         lines = file.readlines()
-        file.seek(saved_position)
-
-        if lines[-1] == '}':
-            # JSON object was closed properly. No repair is
-            # necessary.
-            return file
 
         # JSON object was not closed properly.
         #
@@ -353,19 +347,31 @@ class TestrunResult:
         json.dump(raw_dict, file, indent=JSONWriter.INDENT)
 
     def parseFile(self, file):
-        file = self.__repairFile(file)
-        raw_dict = json.load(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)
+        # Attempt to open the json file normally, if it fails then attempt to
+        # repair it.
+        try:
+            results = json.load(file)
+            self.name = results['name']
+            self.glxinfo = results['glxinfo']
+            self.lspci = results['lspci']
+            self.time_elapsed = results['time_elapsed']
+            self.tests = results['tests']
+            self.options = results['options']
+        except ValueError:
+            file = self.__repairFile(file)
+            raw_dict = json.load(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)
 
 #############################################################################
 ##### Generic Test classes
-- 
1.8.1.4



More information about the Piglit mailing list