[v2,5/26,RFC] framework: Use an enum for concurrency.

Submitted by Dylan Baker on Oct. 27, 2016, 9:31 p.m.

Details

Message ID 907ec9d5549ee61101ec497a98fae9d84f22af1d.1477603560.git-series.dylan@pnwbakers.com
State New
Headers show

Not browsing as part of any series.

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 c01b9b5..741a492 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 []