def test_get_threadlocal_var_empty(request, mocker): logger.info(f'{request._pyfuncitem.name}()') thread_locals = threading.local() with pytest.raises(ValueError): get_threadlocal_var(thread_locals, 'value') with pytest.raises(ValueError): get_threadlocal_var(thread_locals, 'nonexist')
def test_get_threadlocal_var_exist(request, mocker): logger.info(f'{request._pyfuncitem.name}()') thread_locals = threading.local() expValue = 1 thread_locals.value = expValue value = get_threadlocal_var(thread_locals, 'value') assert expValue == value with pytest.raises(ValueError): get_threadlocal_var(thread_locals, 'nonexist')
def run(self): w_logger = logging.getLogger(self.name) i = 0 ns.val = 0 for i in range(stop): ns.val += 1 i += 1 w_logger.debug(f"Thread: {self.name}, value: {ns.val}") value = get_threadlocal_var(ns, "val") assert i == value value = get_threadlocal_var(ns, "val") assert stop == value
def test_get_threadlocal_var_exist_different_thread(request, mocker): logger.info(f'{request._pyfuncitem.name}()') w1 = Worker() w2 = Worker() w1.start() w2.start() w1.join() w2.join() with pytest.raises(ValueError): get_threadlocal_var(ns, 'val') with pytest.raises(ValueError): get_threadlocal_var(ns, 'nonexist')
def test_threadlocal_var_empty(request, mocker): logger.info(f'{request._pyfuncitem.name}()') thread_locals = threading.local() expValue = 5 box = threadlocal_var(thread_locals, 'value', Box, value=expValue) value = box.value assert expValue == value box2 = get_threadlocal_var(thread_locals, 'value') value2 = box2.value assert expValue == value2 assert box == box2 with pytest.raises(ValueError): get_threadlocal_var(thread_locals, 'nonexist')