From 4ed012bf1a9089807fdbcfbe048ec24dbac10f5d Mon Sep 17 00:00:00 2001 From: yh_cc Date: Mon, 2 May 2022 15:39:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0cache=5Fresults=E7=9A=84?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/core/utils/test_cache_results.py | 145 ++++++++++++------------- tests/helpers/common/utils.py | 18 ++- 2 files changed, 75 insertions(+), 88 deletions(-) diff --git a/tests/core/utils/test_cache_results.py b/tests/core/utils/test_cache_results.py index b652ff70..5657ae81 100644 --- a/tests/core/utils/test_cache_results.py +++ b/tests/core/utils/test_cache_results.py @@ -1,29 +1,16 @@ -import time import os import pytest -from subprocess import Popen, PIPE +import subprocess from io import StringIO import sys from fastNLP.core.utils.cache_results import cache_results -from tests.helpers.common.utils import check_time_elapse - from fastNLP.core import rank_zero_rm def get_subprocess_results(cmd): - pipe = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE) - output, err = pipe.communicate() - if output: - output = output.decode('utf8') - else: - output = '' - if err: - err = err.decode('utf8') - else: - err = '' - res = output + err - return res + output = subprocess.check_output(cmd, shell=True) + return output.decode('utf8') class Capturing(list): @@ -48,12 +35,12 @@ class TestCacheResults: try: @cache_results(cache_fp) def demo(): - time.sleep(1) + print("¥") return 1 - res = demo() - with check_time_elapse(1, op='lt'): + with Capturing() as output: res = demo() + assert '¥' not in output[0] finally: rank_zero_rm(cache_fp) @@ -63,12 +50,13 @@ class TestCacheResults: try: @cache_results(cache_fp, _refresh=True) def demo(): - time.sleep(1.5) + print("¥") return 1 res = demo() - with check_time_elapse(1, op='ge'): + with Capturing() as output: res = demo() + assert '¥' in output[0] finally: rank_zero_rm(cache_fp) @@ -77,19 +65,21 @@ class TestCacheResults: try: @cache_results(cache_fp) def demo(): - time.sleep(2) + print('¥') return 1 - with check_time_elapse(1, op='gt'): + with Capturing() as output: res = demo() + assert '¥' in output[0] @cache_results(cache_fp) def demo(): - time.sleep(2) + print('¥') return 1 - with check_time_elapse(1, op='lt'): + with Capturing() as output: res = demo() + assert '¥' not in output[0] finally: rank_zero_rm('demo.pkl') @@ -98,27 +88,28 @@ class TestCacheResults: try: @cache_results(cache_fp) def demo(): - time.sleep(2) + print('¥') return 1 - with check_time_elapse(1, op='gt'): + with Capturing() as output: res = demo() + assert '¥' in output[0] @cache_results(cache_fp) def demo(): - time.sleep(1) + print('¥¥') return 1 - with check_time_elapse(1, op='lt'): - with Capturing() as output: - res = demo() - assert 'is different from its last cache' in output[0] + with Capturing() as output: + res = demo() + assert 'different' in output[0] + assert '¥' not in output[0] # 关闭check_hash应该不warning的 - with check_time_elapse(1, op='lt'): - with Capturing() as output: - res = demo(_check_hash=0) - assert 'is different from its last cache' not in output[0] + with Capturing() as output: + res = demo(_check_hash=0) + assert 'different' not in output[0] + assert '¥' not in output[0] finally: rank_zero_rm('demo.pkl') @@ -128,28 +119,29 @@ class TestCacheResults: try: @cache_results(cache_fp, _check_hash=False) def demo(): - time.sleep(2) + print('¥') return 1 - with check_time_elapse(1, op='gt'): - res = demo() + with Capturing() as output: + res = demo(_check_hash=0) + assert '¥' in output[0] @cache_results(cache_fp, _check_hash=False) def demo(): - time.sleep(1) + print('¥¥') return 1 # 默认不会check - with check_time_elapse(1, op='lt'): - with Capturing() as output: - res = demo() - assert 'is different from its last cache' not in output[0] + with Capturing() as output: + res = demo() + assert 'different' not in output[0] + assert '¥' not in output[0] # check也可以 - with check_time_elapse(1, op='lt'): - with Capturing() as output: - res = demo(_check_hash=True) - assert 'is different from its last cache' in output[0] + with Capturing() as output: + res = demo(_check_hash=True) + assert 'different' in output[0] + assert '¥' not in output[0] finally: rank_zero_rm('demo.pkl') @@ -159,22 +151,22 @@ class TestCacheResults: cache_fp = 'demo.pkl' test_type = 'func_refer_fun_change' try: - with check_time_elapse(3, op='gt'): - cmd = f'python {__file__} --cache_fp {cache_fp} --test_type {test_type} --turn 0' - res = get_subprocess_results(cmd) - + cmd = f'python {__file__} --cache_fp {cache_fp} --test_type {test_type} --turn 0' + res = get_subprocess_results(cmd) + assert "¥" in res # 引用的function没有变化 - with check_time_elapse(2, op='lt'): - cmd = f'python {__file__} --cache_fp {cache_fp} --test_type {test_type} --turn 0' - res = get_subprocess_results(cmd) - assert 'Read cache from' in res - assert 'is different from its last cache' not in res + cmd = f'python {__file__} --cache_fp {cache_fp} --test_type {test_type} --turn 0' + res = get_subprocess_results(cmd) + assert "¥" not in res + + assert 'Read' in res + assert 'different' not in res # 引用的function有变化 - with check_time_elapse(2, op='lt'): - cmd = f'python {__file__} --cache_fp {cache_fp} --test_type {test_type} --turn 1' - res = get_subprocess_results(cmd) - assert 'is different from its last cache' in res + cmd = f'python {__file__} --cache_fp {cache_fp} --test_type {test_type} --turn 1' + res = get_subprocess_results(cmd) + assert "¥" not in res + assert 'different' in res finally: rank_zero_rm(cache_fp) @@ -184,22 +176,21 @@ class TestCacheResults: cache_fp = 'demo.pkl' test_type = 'refer_class_method_change' try: - with check_time_elapse(3, op='gt'): - cmd = f'python {__file__} --cache_fp {cache_fp} --test_type {test_type} --turn 0' - res = get_subprocess_results(cmd) + cmd = f'python {__file__} --cache_fp {cache_fp} --test_type {test_type} --turn 0' + res = get_subprocess_results(cmd) + assert "¥" in res # 引用的class没有变化 - with check_time_elapse(2, op='lt'): - cmd = f'python {__file__} --cache_fp {cache_fp} --test_type {test_type} --turn 0' - res = get_subprocess_results(cmd) - assert 'Read cache from' in res - assert 'is different from its last cache' not in res - - # 引用的class有变化 - with check_time_elapse(2, op='lt'): - cmd = f'python {__file__} --cache_fp {cache_fp} --test_type {test_type} --turn 1' - res = get_subprocess_results(cmd) - assert 'is different from its last cache' in res + cmd = f'python {__file__} --cache_fp {cache_fp} --test_type {test_type} --turn 0' + res = get_subprocess_results(cmd) + assert 'Read' in res + assert 'different' not in res + assert "¥" not in res + + cmd = f'python {__file__} --cache_fp {cache_fp} --test_type {test_type} --turn 1' + res = get_subprocess_results(cmd) + assert 'different' in res + assert "¥" not in res finally: rank_zero_rm(cache_fp) @@ -278,8 +269,8 @@ if __name__ == '__main__': @cache_results(cache_fp) def demo_refer_other_func(): - time.sleep(3) b = demo() + print("¥") return b res = demo_refer_other_func() @@ -296,7 +287,7 @@ if __name__ == '__main__': # pdb.set_trace() @cache_results(cache_fp) def demo_func(): - time.sleep(3) + print("¥") b = demo.demo() return b diff --git a/tests/helpers/common/utils.py b/tests/helpers/common/utils.py index 6efd9779..78c05e95 100644 --- a/tests/helpers/common/utils.py +++ b/tests/helpers/common/utils.py @@ -3,11 +3,11 @@ from contextlib import contextmanager @contextmanager -def check_time_elapse(seconds, op='lt'): +def check_time_elapse(seconds:float, op='lt'): """ 检测某一段程序所花费的时间,是否 op 给定的seconds - :param int seconds: + :param seconds: :param str op: :return: """ @@ -15,19 +15,15 @@ def check_time_elapse(seconds, op='lt'): yield end = time.time() if op == 'lt': - assert end-start < seconds + assert end-start < seconds, (end-start, seconds) elif op == 'gt': - assert end-start > seconds + assert end-start > seconds, (end-start, seconds) elif op == 'eq': - assert end - start == seconds + assert end - start == seconds, (end-start, seconds) elif op == 'le': - assert end - start <= seconds + assert end - start <= seconds, (end-start, seconds) elif op == 'ge': - assert end - start >= seconds + assert end - start >= seconds, (end-start, seconds) else: raise ValueError("Only supports lt,gt,eq,le,ge.") - - - -