diff options
Diffstat (limited to 'meta/lib/oeqa/core/threaded.py')
| -rw-r--r-- | meta/lib/oeqa/core/threaded.py | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/meta/lib/oeqa/core/threaded.py b/meta/lib/oeqa/core/threaded.py new file mode 100644 index 0000000000..e6f214088c --- /dev/null +++ b/meta/lib/oeqa/core/threaded.py | |||
| @@ -0,0 +1,91 @@ | |||
| 1 | # Copyright (C) 2017 Intel Corporation | ||
| 2 | # Released under the MIT license (see COPYING.MIT) | ||
| 3 | |||
| 4 | import multiprocessing | ||
| 5 | |||
| 6 | from unittest.suite import TestSuite | ||
| 7 | from oeqa.core.loader import OETestLoader | ||
| 8 | |||
| 9 | class OETestLoaderThreaded(OETestLoader): | ||
| 10 | def __init__(self, tc, module_paths, modules, tests, modules_required, | ||
| 11 | filters, process_num=0, *args, **kwargs): | ||
| 12 | super(OETestLoaderThreaded, self).__init__(tc, module_paths, modules, | ||
| 13 | tests, modules_required, filters, *args, **kwargs) | ||
| 14 | |||
| 15 | self.process_num = process_num | ||
| 16 | |||
| 17 | def discover(self): | ||
| 18 | suite = super(OETestLoaderThreaded, self).discover() | ||
| 19 | |||
| 20 | if self.process_num <= 0: | ||
| 21 | self.process_num = min(multiprocessing.cpu_count(), | ||
| 22 | len(suite._tests)) | ||
| 23 | |||
| 24 | suites = [] | ||
| 25 | for _ in range(self.process_num): | ||
| 26 | suites.append(self.suiteClass()) | ||
| 27 | |||
| 28 | def _search_for_module_idx(suites, case): | ||
| 29 | """ | ||
| 30 | Cases in the same module needs to be run | ||
| 31 | in the same thread because PyUnit keeps track | ||
| 32 | of setUp{Module, Class,} and tearDown{Module, Class,}. | ||
| 33 | """ | ||
| 34 | |||
| 35 | for idx in range(self.process_num): | ||
| 36 | suite = suites[idx] | ||
| 37 | for c in suite._tests: | ||
| 38 | if case.__module__ == c.__module__: | ||
| 39 | return idx | ||
| 40 | |||
| 41 | return -1 | ||
| 42 | |||
| 43 | def _search_for_depend_idx(suites, depends): | ||
| 44 | """ | ||
| 45 | Dependency cases needs to be run in the same | ||
| 46 | thread, because OEQA framework look at the state | ||
| 47 | of dependant test to figure out if skip or not. | ||
| 48 | """ | ||
| 49 | |||
| 50 | for idx in range(self.process_num): | ||
| 51 | suite = suites[idx] | ||
| 52 | |||
| 53 | for case in suite._tests: | ||
| 54 | if case.id() in depends: | ||
| 55 | return idx | ||
| 56 | return -1 | ||
| 57 | |||
| 58 | def _get_best_idx(suites): | ||
| 59 | sizes = [len(suite._tests) for suite in suites] | ||
| 60 | return sizes.index(min(sizes)) | ||
| 61 | |||
| 62 | def _fill_suites(suite): | ||
| 63 | idx = -1 | ||
| 64 | for case in suite: | ||
| 65 | if isinstance(case, TestSuite): | ||
| 66 | _fill_suites(case) | ||
| 67 | else: | ||
| 68 | idx = _search_for_module_idx(suites, case) | ||
| 69 | |||
| 70 | depends = {} | ||
| 71 | if 'depends' in self.tc._registry: | ||
| 72 | depends = self.tc._registry['depends'] | ||
| 73 | |||
| 74 | if idx == -1 and case.id() in depends: | ||
| 75 | case_depends = depends[case.id()] | ||
| 76 | idx = _search_for_depend_idx(suites, case_depends) | ||
| 77 | |||
| 78 | if idx == -1: | ||
| 79 | idx = _get_best_idx(suites) | ||
| 80 | |||
| 81 | suites[idx].addTest(case) | ||
| 82 | _fill_suites(suite) | ||
| 83 | |||
| 84 | suites_tmp = suites | ||
| 85 | suites = [] | ||
| 86 | for suite in suites_tmp: | ||
| 87 | if len(suite._tests) > 0: | ||
| 88 | suites.append(suite) | ||
| 89 | |||
| 90 | return suites | ||
| 91 | |||
