[RFC,6/27] framework: Use an enum for concurrency.

Submitted by Dylan Baker on Oct. 24, 2016, 7:54 p.m.

Details

Message ID 5c78aa176a0ff77416744d51b1d9bbd14b143b37.1477338764.git-series.dylan@pnwbakers.com
State New
Headers show
Series "Various cleanups for profiles" ( rev: 1 ) in Piglit

Not browsing as part of any series.

Commit Message

Dylan Baker Oct. 24, 2016, 7:54 p.m.
This uses an enum (which are new in python 3.4, but available via the
enum34 backport in pip). I'm putting this up as an RFC, since it does
introduce another dependency for python 2.7 and 3.3, but I think it
makes the code cleaner, since this doesn't require passing strings
around for controlling concurrency.

Signed-off-by: Dylan Baker <dylanx.c.baker@intel.com>
---
 framework/profile.py      | 17 +++++++++++++++--
 framework/programs/run.py | 18 +++++++++---------
 tox.ini                   |  1 +
 3 files changed, 25 insertions(+), 11 deletions(-)

Patch hide | download patch | download mbox

diff --git a/framework/profile.py b/framework/profile.py
index 821a630..954bf25 100644
--- a/framework/profile.py
+++ b/framework/profile.py
@@ -36,6 +36,10 @@  import itertools
 import multiprocessing
 import multiprocessing.dummy
 import os
+try:
+    import enum
+except ImportError:
+    import enum34 as enum
 
 import six
 
@@ -46,12 +50,20 @@  from framework.monitoring import Monitoring
 from framework.test.base import Test
 
 __all__ = [
+    'ConcurrentMode',
     'TestProfile',
     'load_test_profile',
     'merge_test_profiles'
 ]
 
 
+@enum.unique
+class ConcurrentMode(enum.Enum):
+    none = 0
+    some = 1
+    full = 2
+
+
 class TestDict(collections.MutableMapping):
     """A special kind of dict for tests.
 
@@ -488,11 +500,12 @@  def run(profile, logger, backend, concurrency):
     multi = multiprocessing.dummy.Pool()
 
     try:
-        if options.OPTIONS.concurrent == "all":
+        if concurrency is ConcurrentMode.full:
             run_threads(multi, six.iteritems(profile.test_list))
-        elif options.OPTIONS.concurrent == "none":
+        elif concurrency is ConcurrentMode.none:
             run_threads(single, six.iteritems(profile.test_list))
         else:
+            assert concurrency is ConcurrentMode.some
             # Filter and return only thread safe tests to the threaded pool
             run_threads(multi, (x for x in six.iteritems(profile.test_list)
                                 if x[1].run_concurrent))
diff --git a/framework/programs/run.py b/framework/programs/run.py
index be4d75d..4c2227f 100644
--- a/framework/programs/run.py
+++ b/framework/programs/run.py
@@ -126,15 +126,15 @@  def _run_parser(input_):
     conc_parser = parser.add_mutually_exclusive_group()
     conc_parser.add_argument('-c', '--all-concurrent',
                              action="store_const",
-                             default="some",
-                             const="all",
-                             dest="concurrency",
+                             default=framework.profile.ConcurrentMode.some,
+                             const=framework.profile.ConcurrentMode.full,
+                             dest="concurrent",
                              help="Run all tests concurrently")
     conc_parser.add_argument("-1", "--no-concurrency",
                              action="store_const",
-                             default="some",
-                             const="none",
-                             dest="concurrency",
+                             default=framework.profile.ConcurrentMode.some,
+                             const=framework.profile.ConcurrentMode.none,
+                             dest="concurrent",
                              help="Disable concurrent test runs")
     parser.add_argument("-p", "--platform",
                         choices=core.PLATFORMS,
@@ -222,7 +222,7 @@  def _create_metadata(args, name):
     opts = dict(options.OPTIONS)
     opts['profile'] = args.test_profile
     opts['log_level'] = args.log_level
-    opts['concurrent'] = args.concurrent
+    opts['concurrent'] = args.concurrent.name
     if args.platform:
         opts['platform'] = args.platform
 
@@ -274,7 +274,7 @@  def run(input_):
     # If dmesg is requested we must have serial run, this is because dmesg
     # isn't reliable with threaded run
     if args.dmesg or args.monitored:
-        args.concurrency = "none"
+        args.concurrent = framework.profile.ConcurrentMode.none
 
     # Pass arguments into Options
     options.OPTIONS.exclude_filter = args.exclude_tests
@@ -407,7 +407,7 @@  def resume(input_):
         profile,
         results.options['log_level'],
         backend,
-        results.options['concurrent'])
+        framework.profile.ConcurrentMode[results.options['concurrent']])
 
     backend.finalize()
 
diff --git a/tox.ini b/tox.ini
index 2d7b44a..27c2eb6 100644
--- a/tox.ini
+++ b/tox.ini
@@ -30,6 +30,7 @@  deps =
     six==1.5.2
     {accel,noaccel,streams}: jsonschema
     streams: jsonstreams>=0.4.0
+    py{27,33}: enum34
 commands = 
     {accel,noaccel}: py.test -rw unittests/framework unittests/suites []
     generator: py.test -rw unittests/generators []