| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
import multiprocessing as mp |
| 19 |
import inspect |
| 20 |
import os |
| 21 |
import sys |
| 22 |
import traceback |
| 23 |
import queue |
| 24 |
import unittest |
| 25 |
import common |
| 26 |
from contextlib import redirect_stdout |
| 27 |
import io |
| 28 |
|
| 29 |
_modules = {} |
| 30 |
|
| 31 |
|
| 32 |
def _import(filename): |
| 33 |
import importlib.util |
| 34 |
module_name = os.path.basename(filename)[:-3] |
| 35 |
dirname = os.path.dirname(filename) |
| 36 |
if filename in _modules: |
| 37 |
return _modules[filename] |
| 38 |
spec = importlib.util.spec_from_file_location( |
| 39 |
module_name, filename, submodule_search_locations=[dirname]) |
| 40 |
origin = importlib.util.module_from_spec(spec) |
| 41 |
sys.modules[module_name] = origin |
| 42 |
spec.loader.exec_module(origin) |
| 43 |
_modules[filename] = origin |
| 44 |
return origin |
| 45 |
|
| 46 |
|
| 47 |
def _execute(inQueue, outQueue): |
| 48 |
while True: |
| 49 |
datum = inQueue.get() |
| 50 |
if datum is None: |
| 51 |
break |
| 52 |
ex = None |
| 53 |
ret = None |
| 54 |
(func_name, func_file, args, kwargs) = datum |
| 55 |
try: |
| 56 |
f = io.StringIO() |
| 57 |
with redirect_stdout(f): |
| 58 |
module = _import(func_file) |
| 59 |
func = getattr(module, func_name) |
| 60 |
ret = func(*args, **kwargs) |
| 61 |
except Exception as ex1: |
| 62 |
traceback.print_exc() |
| 63 |
ex = ex1 |
| 64 |
out = f.getvalue() |
| 65 |
|
| 66 |
outQueue.put([ret, out, ex]) |
| 67 |
|
| 68 |
|
| 69 |
class Client(object): |
| 70 |
def __init__(self): |
| 71 |
self.start() |
| 72 |
|
| 73 |
def start(self): |
| 74 |
ctx = mp.get_context("spawn") |
| 75 |
self.inQueue = ctx.Queue() |
| 76 |
self.outQueue = ctx.Queue() |
| 77 |
self.process = ctx.Process(target=_execute, args=(self.inQueue, self.outQueue), daemon=True) |
| 78 |
self.process.start() |
| 79 |
self.timeout = 20 |
| 80 |
|
| 81 |
def execute(self, function, *args, **kwargs): |
| 82 |
self.inQueue.put([function.__name__, os.path.abspath( |
| 83 |
inspect.getfile(function)), args, kwargs]) |
| 84 |
try: |
| 85 |
(ret, out, ex) = self.outQueue.get(True, self.timeout) |
| 86 |
except queue.Empty: |
| 87 |
raise AssertionError("function {func} FAILED with args: {args} and kwargs: {kwargs}" |
| 88 |
.format(func=function, args=args, kwargs=kwargs)) |
| 89 |
if ex is not None: |
| 90 |
raise ex |
| 91 |
return ret |
| 92 |
|
| 93 |
def restart(self): |
| 94 |
self.stop() |
| 95 |
self.start() |
| 96 |
|
| 97 |
def stop(self): |
| 98 |
self.inQueue.put(None) |
| 99 |
self.process.join() |
| 100 |
|
| 101 |
def __enter__(self): |
| 102 |
return self |
| 103 |
|
| 104 |
def __exit__(self, type, value, traceback): |
| 105 |
self.stop() |
| 106 |
return False |
| 107 |
|
| 108 |
|
| 109 |
def TestCase(cls=None, **kwargs): |
| 110 |
""" Decorator that makes tests run in a subprocess """ |
| 111 |
if cls: |
| 112 |
return _prepare(cls) |
| 113 |
|
| 114 |
def modify(cls): |
| 115 |
return _prepare(cls, **kwargs) |
| 116 |
return modify |
| 117 |
|
| 118 |
|
| 119 |
def _hook(filename, clsname, funcname, *args): |
| 120 |
module = _import(filename) |
| 121 |
cls = getattr(module, clsname) |
| 122 |
inst = '_instance_%s' % cls.__name__ |
| 123 |
if not inst in module.__dict__: |
| 124 |
setattr(module, inst, cls()) |
| 125 |
inst = getattr(module, inst) |
| 126 |
getattr(inst, funcname)(*args) |
| 127 |
|
| 128 |
|
| 129 |
def _prepare(orig, individual=False): |
| 130 |
clsname = orig.__name__ |
| 131 |
filename = os.path.abspath(inspect.getfile(orig)) |
| 132 |
|
| 133 |
class ProxyClass(orig): |
| 134 |
def __init__(self, *args): |
| 135 |
orig.__init__(self, *args) |
| 136 |
|
| 137 |
@classmethod |
| 138 |
def tearDownClass(cls): |
| 139 |
ProxyClass._client.execute( |
| 140 |
_hook, filename, clsname, '_tearDownClass') |
| 141 |
ProxyClass._client.stop() |
| 142 |
|
| 143 |
@classmethod |
| 144 |
def setUpClass(cls): |
| 145 |
ProxyClass._client = Client() |
| 146 |
ProxyClass._client.execute(_hook, filename, clsname, '_setUpClass') |
| 147 |
|
| 148 |
def setUp(self): |
| 149 |
if common.fast: |
| 150 |
raise unittest.SkipTest("fast") |
| 151 |
if individual: |
| 152 |
ProxyClass._client.restart() |
| 153 |
ProxyClass._client.execute(_hook, filename, clsname, '_setUp') |
| 154 |
if hasattr(self, "setUpLocals"): |
| 155 |
ProxyClass._client.execute( |
| 156 |
_hook, filename, clsname, '_set', self.setUpLocals()) |
| 157 |
|
| 158 |
def _set(self, dic): |
| 159 |
for k, v in dic.items(): |
| 160 |
setattr(self, k, v) |
| 161 |
|
| 162 |
def tearDown(self): |
| 163 |
ProxyClass._client.execute(_hook, filename, clsname, '_tearDown') |
| 164 |
|
| 165 |
class ProxyMethod(object): |
| 166 |
def __init__(self, name): |
| 167 |
self.name = name |
| 168 |
self.__name__ = name |
| 169 |
self.__qualname__ = "%s.%s" % (clsname, name) |
| 170 |
|
| 171 |
def __call__(self): |
| 172 |
ProxyClass._client.execute(_hook, filename, clsname, self.name) |
| 173 |
|
| 174 |
for k, v in orig.__dict__.items(): |
| 175 |
if k.startswith("test"): |
| 176 |
test = ProxyMethod("_" + k) |
| 177 |
test.__name__ = k |
| 178 |
type.__setattr__(ProxyClass, k, test) |
| 179 |
type.__setattr__(ProxyClass, "_" + k, v) |
| 180 |
|
| 181 |
type.__setattr__(ProxyClass, "_setUp", orig.setUp) |
| 182 |
type.__setattr__(ProxyClass, "_setUpClass", orig.setUpClass) |
| 183 |
type.__setattr__(ProxyClass, "_tearDown", orig.tearDown) |
| 184 |
type.__setattr__(ProxyClass, "_tearDownClass", orig.tearDownClass) |
| 185 |
|
| 186 |
return ProxyClass |