Skip to content

Commit

Permalink
trying to make test_stale_after_next_time pass consistently on python…
Browse files Browse the repository at this point in the history
… 2.7
  • Loading branch information
shaypal5 committed Jan 29, 2019
1 parent 8c47550 commit 998233b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
9 changes: 6 additions & 3 deletions cachier/mongo_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
# http://www.opensource.org/licenses/MIT-license
# Copyright (c) 2016, Shay Palachy <[email protected]>

import sys # to make sure that pymongo was imported
import pickle # for serialization of python objects
from datetime import datetime
import time # to sleep when waiting on Mongo cache\
import warnings # to warn if pymongo is missing

try:
from pymongo import (
Expand All @@ -20,9 +22,6 @@
from bson.binary import Binary # to save binary data to mongodb
except ImportError: # pragma: no cover
pass
# warnings.warn((
# "Cachier warning: pymongo was not found. "
# "MongoDB cores will not work."))

from .base_core import _BaseCore

Expand All @@ -39,6 +38,10 @@ class _MongoCore(_BaseCore):
_INDEX_NAME = 'func_1_key_1'

def __init__(self, mongetter, stale_after, next_time):
if 'pymongo' not in sys.modules:
warnings.warn((
"Cachier warning: pymongo was not found. "
"MongoDB cores will not function."))
_BaseCore.__init__(self, stale_after, next_time)
self.mongetter = mongetter
self.mongo_collection = self.mongetter()
Expand Down
6 changes: 4 additions & 2 deletions tests/test_pickle_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ def test_pickle_core():
_takes_5_seconds.clear_cache()


DELTA = timedelta(seconds=3)
SECONDS_IN_DELTA = 3
DELTA = timedelta(seconds=SECONDS_IN_DELTA)


@cachier(stale_after=DELTA, next_time=False)
Expand Down Expand Up @@ -85,9 +86,10 @@ def test_stale_after_next_time():
val3 = _stale_after_next_time(1, 3)
assert val1 == val2
assert val1 != val3
sleep(3)
sleep(SECONDS_IN_DELTA + 1)
val4 = _stale_after_next_time(1, 2)
assert val4 == val1
sleep(0.5)
val5 = _stale_after_next_time(1, 2)
assert val5 != val1
_stale_after_next_time.clear_cache()
Expand Down

0 comments on commit 998233b

Please sign in to comment.