This file is indexed.

/usr/lib/python2.7/dist-packages/oslo_messaging/tests/drivers/zmq/test_zmq_ttl_cache.py is in python-oslo.messaging 5.10.0-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#    Copyright 2016 Mirantis, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import time

from oslo_messaging._drivers.zmq_driver.server import zmq_ttl_cache
from oslo_messaging.tests import utils as test_utils


class TestZmqTTLCache(test_utils.BaseTestCase):

    def setUp(self):
        super(TestZmqTTLCache, self).setUp()

        def call_count_decorator(unbound_method):
            def wrapper(self, *args, **kwargs):
                wrapper.call_count += 1
                return unbound_method(self, *args, **kwargs)
            wrapper.call_count = 0
            return wrapper

        zmq_ttl_cache.TTLCache._update_cache = \
            call_count_decorator(zmq_ttl_cache.TTLCache._update_cache)

        self.cache = zmq_ttl_cache.TTLCache(ttl=1)

    def _test_in_operator(self):
        self.cache.add(1)

        self.assertTrue(1 in self.cache)

        time.sleep(0.5)

        self.cache.add(2)

        self.assertTrue(1 in self.cache)
        self.assertTrue(2 in self.cache)

        time.sleep(0.75)

        self.cache.add(3)

        self.assertFalse(1 in self.cache)
        self.assertTrue(2 in self.cache)
        self.assertTrue(3 in self.cache)

        time.sleep(0.5)

        self.assertFalse(2 in self.cache)
        self.assertTrue(3 in self.cache)

    def test_in_operator_with_executor(self):
        self._test_in_operator()

    def test_in_operator_without_executor(self):
        self.cache._executor.stop()
        self._test_in_operator()

    def _is_expired(self, item):
        with self.cache._lock:
            return self.cache._is_expired(self.cache._expiration_times[item],
                                          time.time())

    def test_executor(self):
        self.cache.add(1)

        self.assertEqual([1], sorted(self.cache._expiration_times.keys()))
        self.assertFalse(self._is_expired(1))

        time.sleep(0.75)

        self.assertEqual(1, self.cache._update_cache.call_count)

        self.cache.add(2)

        self.assertEqual([1, 2], sorted(self.cache._expiration_times.keys()))
        self.assertFalse(self._is_expired(1))
        self.assertFalse(self._is_expired(2))

        time.sleep(0.75)

        self.assertEqual(2, self.cache._update_cache.call_count)

        self.cache.add(3)

        if 1 in self.cache:
            self.assertEqual([1, 2, 3],
                             sorted(self.cache._expiration_times.keys()))
            self.assertTrue(self._is_expired(1))
        else:
            self.assertEqual([2, 3],
                             sorted(self.cache._expiration_times.keys()))
        self.assertFalse(self._is_expired(2))
        self.assertFalse(self._is_expired(3))

        time.sleep(0.75)

        self.assertEqual(3, self.cache._update_cache.call_count)

        self.assertEqual([3], sorted(self.cache._expiration_times.keys()))
        self.assertFalse(self._is_expired(3))

    def cleanUp(self):
        self.cache.cleanup()
        super(TestZmqTTLCache, self).cleanUp()