From d62222e8ac10c9f5574c50ca5aa506b7fc881d81 Mon Sep 17 00:00:00 2001 From: Shay Palachy Date: Sat, 8 Feb 2020 17:56:52 +0200 Subject: [PATCH] trying to reproduce unstable hash bug on travis --- .travis.yml | 3 +++ cachier_text_cache_temp.pkl | 1 + tests/prepare_text_caching_test.py | 25 +++++++++++++++++++++++++ tests/test_pickle_core.py | 22 ++++++++++++++++++++++ 4 files changed, 51 insertions(+) create mode 100644 cachier_text_cache_temp.pkl create mode 100644 tests/prepare_text_caching_test.py diff --git a/.travis.yml b/.travis.yml index aa2ef12..71a9ebb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,6 +29,7 @@ matrix: - pip3 install codecov install: - pip3 install ".[test]" + before_script: python3 test/prepare_text_caching_test.py script: python3 -m pytest after_success: python 3 -m codecov - name: "Python 3.7.3 on macOS 10.14" @@ -42,6 +43,7 @@ matrix: - pip3 install codecov install: - pip3 install ".[test]" + before_script: python3 test/prepare_text_caching_test.py script: python3 -m pytest after_success: python3 -m codecov # ====== WINDOWS ========= @@ -85,6 +87,7 @@ install: - echo $TRAVIS_OS_NAME - pip install ".[test]" # - if [ "$TRAVIS_PYTHON_VERSION" == "2.7" ] && ["$TRAVIS_OS_NAME" == "linux"]; then pip install coverage pytest-cov .; else pip install ".[test]"; fi +before_script: python test/prepare_text_caching_test.py script: pytest after_success: - codecov # submit coverage to codecov.io diff --git a/cachier_text_cache_temp.pkl b/cachier_text_cache_temp.pkl new file mode 100644 index 0000000..bd5c511 --- /dev/null +++ b/cachier_text_cache_temp.pkl @@ -0,0 +1 @@ +€G?Ö¾2ÍM)F. \ No newline at end of file diff --git a/tests/prepare_text_caching_test.py b/tests/prepare_text_caching_test.py new file mode 100644 index 0000000..3ea30ad --- /dev/null +++ b/tests/prepare_text_caching_test.py @@ -0,0 +1,25 @@ +"""Prepares a pickle cache for the text hashing test function.""" + + +from cachier import cachier +from time import sleep +from random import random +from pickle import dump + +TEXT_VAL_TO_CHECK = 'foo' +TEXT_CACHE_FNAME = 'cachier_text_cache_temp.pkl' + + +@cachier(pickle_reload=False) +def text_caching(text): + sleep(1) + print(text) + print(hash(text)) + return random() + + +if __name__ == '__main__': + return_val = text_caching(TEXT_VAL_TO_CHECK) + print(return_val) + with open(TEXT_CACHE_FNAME, 'wb+') as f: + dump(return_val, f) diff --git a/tests/test_pickle_core.py b/tests/test_pickle_core.py index 0fef615..9b7e84a 100644 --- a/tests/test_pickle_core.py +++ b/tests/test_pickle_core.py @@ -16,6 +16,7 @@ from time import ( time, sleep ) +from pickle import load from datetime import timedelta from random import random import threading @@ -27,6 +28,12 @@ except ImportError: # python 2 from cachier import cachier from cachier.pickle_core import DEF_CACHIER_DIR +from .prepare_text_caching_test import ( + TEXT_VAL_TO_CHECK, + TEXT_CACHE_FNAME, + text_caching, +) + # Pickle core tests @@ -392,3 +399,18 @@ def test_pickle_core_custom_cache_dir(): assert end - start < 1 _takes_5_seconds_custom_dir.clear_cache() assert _takes_5_seconds_custom_dir.cache_dpath() == EXPANDED_CUSTOM_DIR + + +def test_text_hashing(): + text = TEXT_VAL_TO_CHECK + with open(TEXT_CACHE_FNAME, 'rb') as f: + first = load(f) + print('\npickled return val found for text cache text:') + print(first) + start_time = time() + second = text_caching(text) + print('second value returned:') + print(second) + call_time = time() - start_time + assert call_time < 1 + assert first == second