|
| 1 | +# Copyright 2022, Optimizely |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http:#www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | + |
| 14 | +from __future__ import annotations |
| 15 | +import time |
| 16 | +from unittest import TestCase |
| 17 | +from optimizely.odp.lru_cache import LRUCache, OptimizelySegmentsCache |
| 18 | + |
| 19 | + |
| 20 | +class LRUCacheTest(TestCase): |
| 21 | + def test_min_config(self): |
| 22 | + cache = LRUCache(1000, 2000) |
| 23 | + self.assertEqual(1000, cache.capacity) |
| 24 | + self.assertEqual(2000, cache.timeout) |
| 25 | + |
| 26 | + cache = LRUCache(0, 0) |
| 27 | + self.assertEqual(0, cache.capacity) |
| 28 | + self.assertEqual(0, cache.timeout) |
| 29 | + |
| 30 | + def test_save_and_lookup(self): |
| 31 | + max_size = 2 |
| 32 | + cache = LRUCache(max_size, 1000) |
| 33 | + |
| 34 | + self.assertIsNone(cache.peek(1)) |
| 35 | + cache.save(1, 100) # [1] |
| 36 | + cache.save(2, 200) # [1, 2] |
| 37 | + cache.save(3, 300) # [2, 3] |
| 38 | + self.assertIsNone(cache.peek(1)) |
| 39 | + self.assertEqual(200, cache.peek(2)) |
| 40 | + self.assertEqual(300, cache.peek(3)) |
| 41 | + |
| 42 | + cache.save(2, 201) # [3, 2] |
| 43 | + cache.save(1, 101) # [2, 1] |
| 44 | + self.assertEqual(101, cache.peek(1)) |
| 45 | + self.assertEqual(201, cache.peek(2)) |
| 46 | + self.assertIsNone(cache.peek(3)) |
| 47 | + |
| 48 | + self.assertIsNone(cache.lookup(3)) # [2, 1] |
| 49 | + self.assertEqual(201, cache.lookup(2)) # [1, 2] |
| 50 | + cache.save(3, 302) # [2, 3] |
| 51 | + self.assertIsNone(cache.peek(1)) |
| 52 | + self.assertEqual(201, cache.peek(2)) |
| 53 | + self.assertEqual(302, cache.peek(3)) |
| 54 | + |
| 55 | + self.assertEqual(302, cache.lookup(3)) # [2, 3] |
| 56 | + cache.save(1, 103) # [3, 1] |
| 57 | + self.assertEqual(103, cache.peek(1)) |
| 58 | + self.assertIsNone(cache.peek(2)) |
| 59 | + self.assertEqual(302, cache.peek(3)) |
| 60 | + |
| 61 | + self.assertEqual(len(cache.map), max_size) |
| 62 | + self.assertEqual(len(cache.map), cache.capacity) |
| 63 | + |
| 64 | + def test_size_zero(self): |
| 65 | + cache = LRUCache(0, 1000) |
| 66 | + |
| 67 | + self.assertIsNone(cache.lookup(1)) |
| 68 | + cache.save(1, 100) # [1] |
| 69 | + self.assertIsNone(cache.lookup(1)) |
| 70 | + |
| 71 | + def test_size_less_than_zero(self): |
| 72 | + cache = LRUCache(-2, 1000) |
| 73 | + |
| 74 | + self.assertIsNone(cache.lookup(1)) |
| 75 | + cache.save(1, 100) # [1] |
| 76 | + self.assertIsNone(cache.lookup(1)) |
| 77 | + |
| 78 | + def test_timeout(self): |
| 79 | + max_timeout = .5 |
| 80 | + |
| 81 | + cache = LRUCache(1000, max_timeout) |
| 82 | + |
| 83 | + cache.save(1, 100) # [1] |
| 84 | + cache.save(2, 200) # [1, 2] |
| 85 | + cache.save(3, 300) # [1, 2, 3] |
| 86 | + time.sleep(1.1) # wait to expire |
| 87 | + cache.save(4, 400) # [1, 2, 3, 4] |
| 88 | + cache.save(1, 101) # [2, 3, 4, 1] |
| 89 | + |
| 90 | + self.assertEqual(101, cache.lookup(1)) # [4, 1] |
| 91 | + self.assertIsNone(cache.lookup(2)) |
| 92 | + self.assertIsNone(cache.lookup(3)) |
| 93 | + self.assertEqual(400, cache.lookup(4)) |
| 94 | + |
| 95 | + def test_timeout_zero(self): |
| 96 | + max_timeout = 0 |
| 97 | + cache = LRUCache(1000, max_timeout) |
| 98 | + |
| 99 | + cache.save(1, 100) # [1] |
| 100 | + cache.save(2, 200) # [1, 2] |
| 101 | + time.sleep(1) # wait to expire |
| 102 | + |
| 103 | + self.assertEqual(100, cache.lookup(1), "should not expire when timeout is 0") |
| 104 | + self.assertEqual(200, cache.lookup(2)) |
| 105 | + |
| 106 | + def test_timeout_less_than_zero(self): |
| 107 | + max_timeout = -2 |
| 108 | + cache = LRUCache(1000, max_timeout) |
| 109 | + |
| 110 | + cache.save(1, 100) # [1] |
| 111 | + cache.save(2, 200) # [1, 2] |
| 112 | + time.sleep(1) # wait to expire |
| 113 | + |
| 114 | + self.assertEqual(100, cache.lookup(1), "should not expire when timeout is less than 0") |
| 115 | + self.assertEqual(200, cache.lookup(2)) |
| 116 | + |
| 117 | + def test_reset(self): |
| 118 | + cache = LRUCache(1000, 600) |
| 119 | + cache.save('wow', 'great') |
| 120 | + cache.save('tow', 'freight') |
| 121 | + |
| 122 | + self.assertEqual(cache.lookup('wow'), 'great') |
| 123 | + self.assertEqual(len(cache.map), 2) |
| 124 | + |
| 125 | + cache.reset() |
| 126 | + |
| 127 | + self.assertEqual(cache.lookup('wow'), None) |
| 128 | + self.assertEqual(len(cache.map), 0) |
| 129 | + |
| 130 | + cache.save('cow', 'crate') |
| 131 | + self.assertEqual(cache.lookup('cow'), 'crate') |
| 132 | + |
| 133 | + # type checker test |
| 134 | + # confirm that LRUCache matches OptimizelySegmentsCache protocol |
| 135 | + _: OptimizelySegmentsCache = LRUCache(0, 0) |
0 commit comments