unicache 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/test/test_all.rb ADDED
@@ -0,0 +1,248 @@
1
+ require 'test/unit'
2
+ require 'unicache'
3
+
4
+ class TestableUniCache < UniCache
5
+ attr_reader :callbackType
6
+ attr_reader :callbacks
7
+ attr_accessor :cb_table
8
+ end
9
+
10
+
11
+ class UniCacheTest < Test::Unit::TestCase
12
+
13
+ def test_put_get
14
+ c = UniCache.new( 2 )
15
+
16
+ assert_equal( 2, c.size )
17
+
18
+ c.put( 'foo', 'bar' )
19
+
20
+ assert_equal( 1, c.length )
21
+
22
+ c.put( 'dii', 'duu' )
23
+
24
+ assert_equal( 2, c.length )
25
+
26
+ assert_equal( 'duu', c.get( 'dii' ) )
27
+ assert_equal( 'bar', c.get( 'foo' ) )
28
+
29
+ c[ 'foo2' ] = 'bar'
30
+
31
+ assert_equal( 2, c.length )
32
+
33
+ c[ 'dii2' ] = 'duu'
34
+
35
+ assert_equal( 2, c.length )
36
+
37
+ assert_equal( 'duu', c[ 'dii2' ] )
38
+ assert_equal( 'bar', c[ 'foo2' ] )
39
+
40
+ assert_equal( nil, c.get( 'dii' ) )
41
+ assert_equal( nil, c.get( 'foo' ) )
42
+ end
43
+
44
+
45
+ def test_eviction
46
+ c = UniCache.new( 2 )
47
+ c[0] = 0
48
+ c[1] = 1
49
+ c[2] = 2
50
+
51
+ # 0 evicted.
52
+ assert_equal( nil, c[0] )
53
+ assert_equal( 1, c[1] )
54
+ assert_equal( 2, c[2] )
55
+
56
+ # 1 latest.
57
+ c[1]
58
+ c[0] = 0
59
+
60
+ # 2 evicted as oldest.
61
+ assert_equal( nil, c[2] )
62
+ assert_equal( 0, c[0] )
63
+ assert_equal( 1, c[1] )
64
+
65
+ assert_equal( nil, c[3] )
66
+
67
+ end
68
+
69
+
70
+ def test_resize
71
+ c = UniCache.new( 2 )
72
+
73
+ assert_equal( 2, c.size )
74
+
75
+ c.put( 'foo', 'bar' )
76
+ c.put( 'dii', 'duu' )
77
+
78
+ assert_equal( 2, c.length )
79
+
80
+ c.resize( 1 )
81
+
82
+ assert_equal( 1, c.length )
83
+
84
+ exception = false
85
+ begin
86
+ c.resize( 0 )
87
+ rescue UniCache::SizeError
88
+ exception = true
89
+ end
90
+ assert_equal( true, exception )
91
+
92
+ c.put( 'foo', 'bar' )
93
+ c.put( 'dii', 'duu' )
94
+
95
+ assert_equal( 'duu', c.get( 'dii' ) )
96
+ assert_equal( nil, c.get( 'foo' ) )
97
+
98
+ end
99
+
100
+
101
+ def test_existance
102
+
103
+ c = UniCache.new( 2 )
104
+ c[0] = 0
105
+ c[2] = 2
106
+ c[1] = 1
107
+
108
+ assert_equal( nil, c.peek(0) )
109
+ assert_equal( 1, c.peek(1) )
110
+ assert_equal( 2, c.peek(2) )
111
+
112
+ assert_equal( false, c.exist?(0) )
113
+ assert_equal( true, c.exist?(1) )
114
+ assert_equal( true, c.exist?(2) )
115
+
116
+ # 2 should be evicted.
117
+ c[0] = 0
118
+ assert_equal( true, c.exist?(0) )
119
+ assert_equal( true, c.exist?(1) )
120
+ assert_equal( false, c.exist?(2) )
121
+
122
+ c.remove( 0 )
123
+
124
+ assert_equal( 1, c.length )
125
+
126
+ assert_equal( true, c.exist?(1) )
127
+
128
+ assert_equal( 1, c.length )
129
+ c[2] = 2
130
+ assert_equal( 2, c.length )
131
+ oldest = c.remove
132
+ assert_equal( [1,1], oldest )
133
+
134
+ c.clear
135
+
136
+ exception = false
137
+ begin
138
+ c.remove( 0 )
139
+ rescue UniCache::RemoveError
140
+ exception = true
141
+ end
142
+ assert_equal( true, exception )
143
+
144
+ assert_equal( 0, c.length )
145
+ assert_equal( 2, c.size )
146
+ assert_equal( false, c.exist?(1) )
147
+ assert_equal( false, c.exist?(0) )
148
+
149
+ # Clear for empty cache.
150
+ c.clear
151
+
152
+ end
153
+
154
+
155
+ def test_callbacks
156
+ c = TestableUniCache.new( 2 )
157
+ c.cb_table = {}
158
+
159
+ # Register dummy callbacks.
160
+ c.callbackType.each do |type|
161
+ eval "c.registerCallback( :#{type.to_s}, Proc.new{ |k,v,o| o.cb_table[ :#{type.to_s} ] = [ k, v ] } )"
162
+ end
163
+
164
+ c.removeCallback( :getdata )
165
+ c.registerCallback( :getdata, Proc.new{ |k| k+1 } )
166
+
167
+ # Wrong type.
168
+ exception = false
169
+ begin
170
+ c.registerCallback( :my_callback, Proc.new {} )
171
+ rescue UniCache::CallbackError
172
+ exception = true
173
+ end
174
+ assert_equal( true, exception )
175
+
176
+ # Invalid proc.
177
+ exception = false
178
+ begin
179
+ c.registerCallback( :add, true )
180
+ rescue UniCache::CallbackError
181
+ exception = true
182
+ end
183
+ assert_equal( true, exception )
184
+
185
+
186
+ c[0] = 0
187
+ assert_equal( {:add=>[0, 0], :put=>[0, 0]}, c.cb_table )
188
+ c.cb_table.clear
189
+
190
+ c[0] = 1
191
+ assert_equal( {:overwrite=>[0, 1], :valueremove=>[0, 0], :put=>[0, 1]}, c.cb_table )
192
+ c.cb_table.clear
193
+
194
+ c[0]
195
+ assert_equal( {:hit=>[0, 1]}, c.cb_table )
196
+ c.cb_table.clear
197
+
198
+ c[1]
199
+ assert_equal( {:miss=>[1, nil], :add=>[1, 2], :put=>[1, 2]}, c.cb_table )
200
+ c.cb_table.clear
201
+
202
+ c.removeCallback
203
+ c.resize( 1 )
204
+ c[0] = 0
205
+ c[1]
206
+ assert_equal( {}, c.cb_table )
207
+ c.cb_table.clear
208
+
209
+ end
210
+
211
+
212
+ def test_thread
213
+
214
+ c = UniCache.new( 2 )
215
+
216
+ # Trying to destroy entry.
217
+ t1 = Thread.new do
218
+ sleep( 1 )
219
+ c.remove( 0 )
220
+ end
221
+
222
+ c.registerCallback( :getdata, Proc.new do 'data' end )
223
+
224
+ gotValue = nil
225
+
226
+ # Block value through get.
227
+ t2 = Thread.new do
228
+ c.get( 0 ) do |val|
229
+ sleep( 4 )
230
+ gotValue = val
231
+ end
232
+ end
233
+
234
+ # Distractions.
235
+ t3 = Thread.new do
236
+ sleep( 1 )
237
+ 100.times do
238
+ c[0]
239
+ c[0] = 2
240
+ end
241
+ end
242
+
243
+ [t1, t2, t3].each do |t| t.join end
244
+
245
+ assert_equal( 'data', gotValue )
246
+ end
247
+
248
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unicache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tero Isannainen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-12-31 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! 'UniCache is a universal purpose cache with Least Recently Used
15
+
16
+ replacement policy by default. Cache can be configured to use another policy. UniCache
17
+ is intended to be Thread safe. User can register callbacks that are run at various
18
+ UniCache events.'
19
+ email: tero.isannainen@gmail.com
20
+ executables: []
21
+ extensions: []
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - README.rdoc
26
+ - CHANGELOG.rdoc
27
+ - LICENSE
28
+ - Rakefile
29
+ - lib/unicache.rb
30
+ - test/test_all.rb
31
+ - doc/top-level-namespace.html
32
+ - doc/js/full_list.js
33
+ - doc/js/app.js
34
+ - doc/js/jquery.js
35
+ - doc/index.html
36
+ - doc/css/common.css
37
+ - doc/css/full_list.css
38
+ - doc/css/style.css
39
+ - doc/UniCache.html
40
+ - doc/file.README.html
41
+ - doc/method_list.html
42
+ - doc/file_list.html
43
+ - doc/UniCache/CallbackError.html
44
+ - doc/UniCache/FetchError.html
45
+ - doc/UniCache/LruEviction.html
46
+ - doc/UniCache/Error.html
47
+ - doc/UniCache/SizeError.html
48
+ - doc/UniCache/RemoveError.html
49
+ - doc/class_list.html
50
+ - doc/_index.html
51
+ - doc/file.CHANGELOG.html
52
+ - doc/frames.html
53
+ homepage:
54
+ licenses:
55
+ - Ruby
56
+ post_install_message: Check README...
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: 1.9.3
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 1.8.23
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Universal purpose Cache with callback facility.
78
+ test_files: []
79
+ has_rdoc: