def test_set_time_override_using_default(self):
        now = timeutils.utcnow_ts()

        # NOTE(kgriffs): Normally it's bad form to sleep in a unit test,
        # but this is the only way to test that set_time_override defaults
        # to setting the override to the current time.
        time.sleep(1)

        timeutils.set_time_override()
        overriden_now = timeutils.utcnow_ts()
        self.assertThat(now, matchers.LessThan(overriden_now))
    def test_set_time_override_using_default(self):
        now = timeutils.utcnow_ts()

        # NOTE(kgriffs): Normally it's bad form to sleep in a unit test,
        # but this is the only way to test that set_time_override defaults
        # to setting the override to the current time.
        time.sleep(1)

        timeutils.set_time_override()
        overriden_now = timeutils.utcnow_ts()
        self.assertThat(now, matchers.LessThan(overriden_now))
    def test_utcnow_ts(self):
        skynet_self_aware_ts = 872835240
        skynet_dt = datetime.datetime.utcfromtimestamp(skynet_self_aware_ts)
        self.assertEqual(self.skynet_self_aware_time, skynet_dt)

        # NOTE(kgriffs): timeutils.utcnow_ts() uses time.time()
        # IFF time override is not set.
        with mock.patch('time.time') as time_mock:
            time_mock.return_value = skynet_self_aware_ts
            ts = timeutils.utcnow_ts()
            self.assertEqual(ts, skynet_self_aware_ts)

        timeutils.set_time_override(skynet_dt)
        ts = timeutils.utcnow_ts()
        self.assertEqual(ts, skynet_self_aware_ts)
    def test_utcnow_ts(self):
        skynet_self_aware_ts = 872835240
        skynet_dt = datetime.datetime.utcfromtimestamp(skynet_self_aware_ts)
        self.assertEqual(self.skynet_self_aware_time, skynet_dt)

        # NOTE(kgriffs): timeutils.utcnow_ts() uses time.time()
        # IFF time override is not set.
        with mock.patch('time.time') as time_mock:
            time_mock.return_value = skynet_self_aware_ts
            ts = timeutils.utcnow_ts()
            self.assertEqual(ts, skynet_self_aware_ts)

        timeutils.set_time_override(skynet_dt)
        ts = timeutils.utcnow_ts()
        self.assertEqual(ts, skynet_self_aware_ts)
 def _exists_unlocked(self, key):
     now = timeutils.utcnow_ts()
     try:
         timeout = self._cache[key][0]
         return not timeout or now <= timeout
     except KeyError:
         return False
    def _get_unlocked(self, key, default=None):
        now = timeutils.utcnow_ts()

        try:
            timeout, value = self._cache[key]
        except KeyError:
            return (0, default)

        if timeout and now >= timeout:

            # NOTE(flaper87): Record expired,
            # remove it from the cache but catch
            # KeyError and ValueError in case
            # _purge_expired removed this key already.
            try:
                del self._cache[key]
            except KeyError:
                pass

            try:
                # NOTE(flaper87): Keys with ttl == 0
                # don't exist in the _keys_expires dict
                self._keys_expires[timeout].remove(key)
            except (KeyError, ValueError):
                pass

            return (0, default)

        return (timeout, value)
 def set(self, key, value, time=0, min_compress_len=0):
     """Sets the value for a key."""
     timeout = 0
     if time != 0:
         timeout = timeutils.utcnow_ts() + time
     self.cache[key] = (timeout, value)
     return True
Exemple #8
0
 def _exists_unlocked(self, key):
     now = timeutils.utcnow_ts()
     try:
         timeout = self._cache[key][0]
         return not timeout or now <= timeout
     except KeyError:
         return False
Exemple #9
0
 def set(self, key, value, time=0, min_compress_len=0):
     """Sets the value for a key."""
     timeout = 0
     if time != 0:
         timeout = timeutils.utcnow_ts() + time
     self.cache[key] = (timeout, value)
     return True
Exemple #10
0
    def _get_unlocked(self, key, default=None):
        now = timeutils.utcnow_ts()

        try:
            timeout, value = self._cache[key]
        except KeyError:
            return (0, default)

        if timeout and now >= timeout:

            # NOTE(flaper87): Record expired,
            # remove it from the cache but catch
            # KeyError and ValueError in case
            # _purge_expired removed this key already.
            try:
                del self._cache[key]
            except KeyError:
                pass

            try:
                # NOTE(flaper87): Keys with ttl == 0
                # don't exist in the _keys_expires dict
                self._keys_expires[timeout].remove(key)
            except (KeyError, ValueError):
                pass

            return (0, default)

        return (timeout, value)
 def test_utcnow_ts(self):
     skynet_self_aware_timestamp = 872835240
     dt = datetime.datetime.utcfromtimestamp(skynet_self_aware_timestamp)
     self.assertEqual(self.skynet_self_aware_time, dt)
     with mock.patch('datetime.datetime') as datetime_mock:
         datetime_mock.utcnow.return_value = self.skynet_self_aware_time
         ts = timeutils.utcnow_ts()
         self.assertEqual(ts, skynet_self_aware_timestamp)
Exemple #12
0
 def test_utcnow_ts(self):
     skynet_self_aware_timestamp = 872835240
     dt = datetime.datetime.utcfromtimestamp(skynet_self_aware_timestamp)
     self.assertEqual(self.skynet_self_aware_time, dt)
     with mock.patch('datetime.datetime') as datetime_mock:
         datetime_mock.utcnow.return_value = self.skynet_self_aware_time
         ts = timeutils.utcnow_ts()
         self.assertEqual(ts, skynet_self_aware_timestamp)
Exemple #13
0
    def _set_unlocked(self, key, value, ttl=0):
        expires_at = 0
        if ttl != 0:
            expires_at = timeutils.utcnow_ts() + ttl

        self._cache[key] = (expires_at, value)

        if expires_at:
            self._keys_expires[expires_at].add(key)
 def test_utcnow_ts(self):
     skynet_self_aware_timestamp = 872835240
     dt = datetime.datetime.utcfromtimestamp(skynet_self_aware_timestamp)
     expect = dt.replace(tzinfo=iso8601.iso8601.UTC)
     self.assertEqual(self.skynet_self_aware_time, expect)
     with mock.patch('datetime.datetime') as datetime_mock:
         datetime_mock.utcnow.return_value = self.skynet_self_aware_time
         ts = timeutils.utcnow_ts()
         self.assertEqual(ts, skynet_self_aware_timestamp)
    def _set_unlocked(self, key, value, ttl=0):
        expires_at = 0
        if ttl != 0:
            expires_at = timeutils.utcnow_ts() + ttl

        self._cache[key] = (expires_at, value)

        if expires_at:
            self._keys_expires[expires_at].add(key)
 def test_utcnow_ts(self):
     skynet_self_aware_timestamp = 872835240
     dt = datetime.datetime.utcfromtimestamp(skynet_self_aware_timestamp)
     expect = dt.replace(tzinfo=iso8601.iso8601.UTC)
     self.assertEqual(self.skynet_self_aware_time, expect)
     with mock.patch('datetime.datetime') as datetime_mock:
         datetime_mock.utcnow.return_value = self.skynet_self_aware_time
         ts = timeutils.utcnow_ts()
         self.assertEqual(ts, skynet_self_aware_timestamp)
    def _incr_append(self, key, other):
        with lockutils.lock(key):
            timeout, value = self._get_unlocked(key)

            if value is None:
                return None

            ttl = timeutils.utcnow_ts() - timeout
            new_value = value + other
            self._set_unlocked(key, new_value, ttl)
            return new_value
Exemple #18
0
    def _incr_append(self, key, other):
        with lockutils.lock(key):
            timeout, value = self._get_unlocked(key)

            if value is None:
                return None

            ttl = timeutils.utcnow_ts() - timeout
            new_value = value + other
            self._set_unlocked(key, new_value, ttl)
            return new_value
Exemple #19
0
    def get(self, key):
        """Retrieves the value for a key or None.

        this expunges expired keys during each get"""

        now = timeutils.utcnow_ts()
        for k in self.cache.keys():
            (timeout, _value) = self.cache[k]
            if timeout and now >= timeout:
                del self.cache[k]

        return self.cache.get(key, (0, None))[1]
    def get(self, key):
        """Retrieves the value for a key or None.

        this expunges expired keys during each get"""

        now = timeutils.utcnow_ts()
        for k in self.cache.keys():
            (timeout, _value) = self.cache[k]
            if timeout and now >= timeout:
                del self.cache[k]

        return self.cache.get(key, (0, None))[1]
    def _purge_expired(self):
        """Removes expired keys from the cache."""

        now = timeutils.utcnow_ts()
        for timeout in sorted(self._keys_expires.keys()):

            # NOTE(flaper87): If timeout is greater
            # than `now`, stop the iteration, remaining
            # keys have not expired.
            if now < timeout:
                break

            # NOTE(flaper87): Unset every key in
            # this set from the cache if its timeout
            # is equal to `timeout`. (The key might
            # have been updated)
            for subkey in self._keys_expires.pop(timeout):
                try:
                    if self._cache[subkey][0] == timeout:
                        del self._cache[subkey]
                except KeyError:
                    continue
Exemple #22
0
    def _purge_expired(self):
        """Removes expired keys from the cache."""

        now = timeutils.utcnow_ts()
        for timeout in sorted(self._keys_expires.keys()):

            # NOTE(flaper87): If timeout is greater
            # than `now`, stop the iteration, remaining
            # keys have not expired.
            if now < timeout:
                break

            # NOTE(flaper87): Unset every key in
            # this set from the cache if its timeout
            # is equal to `timeout`. (The key might
            # have been updated)
            for subkey in self._keys_expires.pop(timeout):
                try:
                    if self._cache[subkey][0] == timeout:
                        del self._cache[subkey]
                except KeyError:
                    continue
 def test_utcnow_ts(self):
     skynet_self_aware_timestamp = 872806440.0
     with mock.patch('datetime.datetime') as datetime_mock:
         datetime_mock.utcnow.return_value = self.skynet_self_aware_time
         ts = timeutils.utcnow_ts()
         self.assertEqual(ts, skynet_self_aware_timestamp)