sin_lru_redux 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/cache_test.rb DELETED
@@ -1,146 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'lru_redux'
4
- require 'minitest/autorun'
5
- require 'minitest/pride'
6
-
7
- class CacheTest < Minitest::Test
8
- def setup
9
- @c = LruRedux::Cache.new(3)
10
- end
11
-
12
- def teardown
13
- assert @c.send(:valid?) # rubocop:disable Minitest/AssertionInLifecycleHook
14
- end
15
-
16
- def test_drops_old
17
- @c[:a] = 1
18
- @c[:b] = 2
19
- @c[:c] = 3
20
- @c[:d] = 4
21
-
22
- assert_equal [[:d, 4], [:c, 3], [:b, 2]], @c.to_a
23
- assert_nil @c[:a]
24
- end
25
-
26
- def test_fetch
27
- @c[:a] = nil
28
- @c[:b] = 2
29
-
30
- assert_nil @c.fetch(:a) { 1 } # rubocop:disable Style/RedundantFetchBlock
31
- assert_equal 3, @c.fetch(:c) { 3 } # rubocop:disable Style/RedundantFetchBlock
32
- assert_equal [[:a, nil], [:b, 2]], @c.to_a
33
- end
34
-
35
- def test_getset # rubocop:disable Minitest/MultipleAssertions
36
- assert_equal 1, @c.getset(:a) { 1 }
37
- @c.getset(:b) { 2 }
38
-
39
- assert_equal 1, @c.getset(:a) { 11 }
40
- @c.getset(:c) { 3 }
41
-
42
- assert_equal 4, @c.getset(:d) { 4 }
43
- assert_equal [[:d, 4], [:c, 3], [:a, 1]], @c.to_a
44
- end
45
-
46
- def test_pushes_lru_to_back
47
- @c[:a] = 1
48
- @c[:b] = 2
49
- @c[:c] = 3
50
-
51
- @c[:a]
52
- @c[:d] = 4
53
-
54
- assert_equal [[:d, 4], [:a, 1], [:c, 3]], @c.to_a
55
- assert_nil @c[:b]
56
- end
57
-
58
- def test_delete # rubocop:disable Minitest/MultipleAssertions
59
- @c[:a] = 1
60
- @c[:b] = 2
61
- @c[:c] = 3
62
- @c.delete(:a)
63
-
64
- assert_equal [[:c, 3], [:b, 2]], @c.to_a
65
- assert_nil @c[:a]
66
-
67
- # Regression test for a bug in the legacy delete method
68
- @c.delete(:b)
69
- @c[:d] = 4
70
- @c[:e] = 5
71
- @c[:f] = 6
72
-
73
- assert_equal [[:f, 6], [:e, 5], [:d, 4]], @c.to_a
74
- assert_nil @c[:b]
75
- end
76
-
77
- def test_key?
78
- @c[:a] = 1
79
- @c[:b] = 2
80
-
81
- assert @c.key?(:a)
82
- refute @c.key?(:c)
83
- end
84
-
85
- def test_update
86
- @c[:a] = 1
87
- @c[:b] = 2
88
- @c[:c] = 3
89
- @c[:a] = 99
90
-
91
- assert_equal [[:a, 99], [:c, 3], [:b, 2]], @c.to_a
92
- end
93
-
94
- def test_clear
95
- @c[:a] = 1
96
- @c[:b] = 2
97
- @c[:c] = 3
98
-
99
- @c.clear
100
-
101
- assert_empty @c.to_a
102
- end
103
-
104
- def test_grow
105
- @c[:a] = 1
106
- @c[:b] = 2
107
- @c[:c] = 3
108
- @c.max_size = 4
109
- @c[:d] = 4
110
-
111
- assert_equal [[:d, 4], [:c, 3], [:b, 2], [:a, 1]], @c.to_a
112
- end
113
-
114
- def test_shrink
115
- @c[:a] = 1
116
- @c[:b] = 2
117
- @c[:c] = 3
118
- @c.max_size = 1
119
-
120
- assert_equal [[:c, 3]], @c.to_a
121
- end
122
-
123
- def test_each
124
- @c.max_size = 2
125
- @c[:a] = 1
126
- @c[:b] = 2
127
- @c[:c] = 3
128
-
129
- pairs = []
130
- @c.each do |pair| # rubocop:disable Style/MapIntoArray
131
- pairs << pair
132
- end
133
-
134
- assert_equal [[:c, 3], [:b, 2]], pairs
135
- end
136
-
137
- def test_values
138
- @c[:a] = 1
139
- @c[:b] = 2
140
- @c[:c] = 3
141
- @c[:d] = 4
142
-
143
- assert_equal [4, 3, 2], @c.values
144
- assert_nil @c[:a]
145
- end
146
- end
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require './test/cache_test'
4
-
5
- class ThreadSafeCacheTest < CacheTest
6
- def setup
7
- @c = LruRedux::ThreadSafeCache.new(3)
8
- end
9
-
10
- def test_recursion
11
- @c[:a] = 1
12
- @c[:b] = 2
13
-
14
- # should not blow up
15
- @c.each do |k, _| # rubocop:disable Style/HashEachMethods
16
- @c[k]
17
- end
18
- end
19
- end
@@ -1,93 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'timecop'
4
- require './test/cache_test'
5
-
6
- class TTLCacheTest < CacheTest
7
- def setup
8
- Timecop.freeze(Time.now)
9
- @c = LruRedux::TTL::Cache.new 3, 5 * 60
10
- end
11
-
12
- def teardown
13
- Timecop.return
14
-
15
- assert @c.send(:valid?) # rubocop:disable Minitest/AssertionInLifecycleHook
16
- end
17
-
18
- def test_ttl
19
- assert_equal 300, @c.ttl
20
-
21
- @c.ttl = 10 * 60
22
-
23
- assert_equal 600, @c.ttl
24
- end
25
-
26
- # TTL tests using Timecop
27
- def test_ttl_eviction_on_access
28
- @c[:a] = 1
29
- @c[:b] = 2
30
-
31
- Timecop.freeze(Time.now + 330)
32
-
33
- @c[:c] = 3
34
-
35
- assert_equal([[:c, 3]], @c.to_a)
36
- end
37
-
38
- def test_ttl_eviction_on_expire
39
- @c[:a] = 1
40
- @c[:b] = 2
41
-
42
- Timecop.freeze(Time.now + 330)
43
-
44
- @c.expire
45
-
46
- assert_empty @c.to_a
47
- end
48
-
49
- def test_ttl_eviction_on_new_max_size
50
- @c[:a] = 1
51
- @c[:b] = 2
52
-
53
- Timecop.freeze(Time.now + 330)
54
-
55
- @c.max_size = 10
56
-
57
- assert_empty @c.to_a
58
- end
59
-
60
- def test_ttl_eviction_on_new_ttl
61
- @c[:a] = 1
62
- @c[:b] = 2
63
-
64
- Timecop.freeze(Time.now + 330)
65
-
66
- @c.ttl = 10 * 60
67
-
68
- assert_equal([[:b, 2], [:a, 1]], @c.to_a)
69
-
70
- @c.ttl = 2 * 60
71
-
72
- assert_empty @c.to_a
73
- end
74
-
75
- def test_ttl_precedence_over_lru
76
- @c[:a] = 1
77
-
78
- Timecop.freeze(Time.now + 60)
79
-
80
- @c[:b] = 2
81
- @c[:c] = 3
82
-
83
- @c[:a]
84
-
85
- assert_equal [[:a, 1], [:c, 3], [:b, 2]], @c.to_a
86
-
87
- Timecop.freeze(Time.now + 270)
88
-
89
- @c[:d] = 4
90
-
91
- assert_equal [[:d, 4], [:c, 3], [:b, 2]], @c.to_a
92
- end
93
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require './test/ttl/cache_test'
4
-
5
- class TTLThreadSafeCacheTest < TTLCacheTest
6
- def setup
7
- Timecop.freeze(Time.now)
8
- @c = LruRedux::TTL::ThreadSafeCache.new 3, 5 * 60
9
- end
10
-
11
- def test_recursion
12
- @c[:a] = 1
13
- @c[:b] = 2
14
-
15
- # should not blow up
16
- @c.each do |k, _| # rubocop:disable Style/HashEachMethods
17
- @c[k]
18
- end
19
- end
20
- end