File: test_operations.rb

package info (click to toggle)
ruby-dalli 3.2.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 684 kB
  • sloc: ruby: 6,552; sh: 20; makefile: 4
file content (379 lines) | stat: -rw-r--r-- 10,097 bytes parent folder | download
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# frozen_string_literal: true

require_relative '../helper'
require 'openssl'
require 'securerandom'

describe 'operations' do
  MemcachedManager.supported_protocols.each do |p|
    describe "using the #{p} protocol" do
      describe 'get' do
        it 'returns the value on a hit' do
          memcached_persistent(p) do |dc|
            dc.flush

            val1 = '1234567890' * 999_999
            dc.set('a', val1)
            val2 = dc.get('a')

            assert_equal val1, val2

            assert op_addset_succeeds(dc.set('a', nil))
            assert_nil dc.get('a')
          end
        end

        it 'returns nil on a miss' do
          memcached_persistent(p) do |dc|
            assert_nil dc.get('notexist')
          end
        end

        # This is historical, as the 'Not found' value was
        # treated as a special case at one time
        it 'allows "Not found" as value' do
          memcached_persistent(p) do |dc|
            dc.set('key1', 'Not found')

            assert_equal 'Not found', dc.get('key1')
          end
        end
      end

      describe 'gat' do
        it 'returns the value and touches on a hit' do
          memcached_persistent(p) do |dc|
            dc.flush
            dc.set 'key', 'value'

            assert_equal 'value', dc.gat('key', 10)
            assert_equal 'value', dc.gat('key')
          end
        end

        it 'returns nil on a miss' do
          memcached_persistent(p) do |dc|
            dc.flush

            assert_nil dc.gat('notexist', 10)
          end
        end
      end

      describe 'touch' do
        it 'returns true on a hit' do
          memcached_persistent(p) do |dc|
            dc.flush
            dc.set 'key', 'value'

            assert dc.touch('key', 10)
            assert dc.touch('key')
            assert_equal 'value', dc.get('key')
            assert_nil dc.touch('notexist')
          rescue Dalli::DalliError => e
            # This will happen when memcached is in lesser version than 1.4.8
            assert_equal 'Response error 129: Unknown command', e.message
          end
        end

        it 'returns nil on a miss' do
          memcached_persistent(p) do |dc|
            dc.flush

            assert_nil dc.touch('notexist')
          end
        end
      end

      describe 'set' do
        it 'returns a CAS when the key exists and updates the value' do
          memcached_persistent(p) do |dc|
            dc.flush
            dc.set('key', 'value')

            assert op_replace_succeeds(dc.set('key', 'value2'))

            assert_equal 'value2', dc.get('key')
          end
        end

        it 'returns a CAS when no pre-existing value exists' do
          memcached_persistent(p) do |dc|
            dc.flush

            assert op_replace_succeeds(dc.set('key', 'value2'))
            assert_equal 'value2', dc.get('key')
          end
        end
      end

      describe 'add' do
        it 'returns false when replacing an existing value and does not update the value' do
          memcached_persistent(p) do |dc|
            dc.flush
            dc.set('key', 'value')

            refute dc.add('key', 'value')

            assert_equal 'value', dc.get('key')
          end
        end

        it 'returns a CAS when no pre-existing value exists' do
          memcached_persistent(p) do |dc|
            dc.flush

            assert op_replace_succeeds(dc.add('key', 'value2'))
          end
        end
      end

      describe 'replace' do
        it 'returns a CAS when the key exists and updates the value' do
          memcached_persistent(p) do |dc|
            dc.flush
            dc.set('key', 'value')

            assert op_replace_succeeds(dc.replace('key', 'value2'))

            assert_equal 'value2', dc.get('key')
          end
        end

        it 'returns false when no pre-existing value exists' do
          memcached_persistent(p) do |dc|
            dc.flush

            refute dc.replace('key', 'value')
          end
        end
      end

      describe 'delete' do
        it 'returns true on a hit and deletes the entry' do
          memcached_persistent(p) do |dc|
            dc.flush
            dc.set('some_key', 'some_value')

            assert_equal 'some_value', dc.get('some_key')

            assert dc.delete('some_key')
            assert_nil dc.get('some_key')

            refute dc.delete('nonexist')
          end
        end

        it 'returns false on a miss' do
          memcached_persistent(p) do |dc|
            dc.flush

            refute dc.delete('nonexist')
          end
        end
      end

      describe 'fetch' do
        it 'fetches pre-existing values' do
          memcached_persistent(p) do |dc|
            dc.flush
            dc.set('fetch_key', 'Not found')
            res = dc.fetch('fetch_key') { flunk 'fetch block called' }

            assert_equal 'Not found', res
          end
        end

        it 'supports with default values' do
          memcached_persistent(p) do |dc|
            dc.flush

            expected = { 'blah' => 'blerg!' }
            executed = false
            value = dc.fetch('fetch_key') do
              executed = true
              expected
            end

            assert_equal expected, value
            assert executed

            executed = false
            value = dc.fetch('fetch_key') do
              executed = true
              expected
            end

            assert_equal expected, value
            refute executed
          end
        end

        it 'supports with falsey values' do
          memcached_persistent(p) do |dc|
            dc.flush

            dc.set('fetch_key', false)
            res = dc.fetch('fetch_key') { flunk 'fetch block called' }

            refute res
          end
        end

        it 'supports with nil values when cache_nils: true' do
          memcached_persistent(p, 21_345, '', cache_nils: true) do |dc|
            dc.flush

            dc.set('fetch_key', nil)
            res = dc.fetch('fetch_key') { flunk 'fetch block called' }

            assert_nil res
          end

          memcached_persistent(p, 21_345, '', cache_nils: false) do |dc|
            dc.flush
            dc.set('fetch_key', nil)
            executed = false
            res = dc.fetch('fetch_key') do
              executed = true
              'bar'
            end

            assert_equal 'bar', res
            assert executed
          end
        end
      end

      describe 'incr/decr' do
        it 'supports incrementing and decrementing existing values' do
          memcached_persistent(p) do |client|
            client.flush

            assert op_addset_succeeds(client.set('fakecounter', 0, 0, raw: true))
            assert_equal 1, client.incr('fakecounter', 1)
            assert_equal 2, client.incr('fakecounter', 1)
            assert_equal 3, client.incr('fakecounter', 1)
            assert_equal 1, client.decr('fakecounter', 2)
            assert_equal '1', client.get('fakecounter')
          end
        end

        it 'returns nil on a miss with no initial value' do
          memcached_persistent(p) do |client|
            client.flush

            resp = client.incr('mycounter', 1)

            assert_nil resp

            resp = client.decr('mycounter', 1)

            assert_nil resp
          end
        end

        it 'enables setting an initial value with incr and subsequently incrementing/decrementing' do
          memcached_persistent(p) do |client|
            client.flush

            resp = client.incr('mycounter', 1, 0, 2)

            assert_equal 2, resp
            resp = client.incr('mycounter', 1)

            assert_equal 3, resp

            resp = client.decr('mycounter', 2)

            assert_equal 1, resp
          end
        end

        it 'supports setting the initial value with decr and subsequently incrementing/decrementing' do
          memcached_persistent(p) do |dc|
            dc.flush

            resp = dc.decr('counter', 100, 5, 0)

            assert_equal 0, resp

            resp = dc.decr('counter', 10)

            assert_equal 0, resp

            resp = dc.incr('counter', 10)

            assert_equal 10, resp

            current = 10
            100.times do |x|
              resp = dc.incr('counter', 10)

              assert_equal current + ((x + 1) * 10), resp
            end
          end
        end

        it 'supports 64-bit values' do
          memcached_persistent(p) do |dc|
            dc.flush

            resp = dc.decr('10billion', 0, 5, 10)

            assert_equal 10, resp
            # go over the 32-bit mark to verify proper (un)packing
            resp = dc.incr('10billion', 10_000_000_000)

            assert_equal 10_000_000_010, resp

            resp = dc.decr('10billion', 1)

            assert_equal 10_000_000_009, resp

            resp = dc.decr('10billion', 0)

            assert_equal 10_000_000_009, resp

            resp = dc.incr('10billion', 0)

            assert_equal 10_000_000_009, resp

            resp = dc.decr('10billion', 9_999_999_999)

            assert_equal 10, resp

            resp = dc.incr('big', 100, 5, 0xFFFFFFFFFFFFFFFE)

            assert_equal 0xFFFFFFFFFFFFFFFE, resp
            resp = dc.incr('big', 1)

            assert_equal 0xFFFFFFFFFFFFFFFF, resp

            # rollover the 64-bit value, we'll get something undefined.
            resp = dc.incr('big', 1)

            refute_equal 0x10000000000000000, resp
            dc.reset
          end
        end
      end

      describe 'append/prepend' do
        it 'support the append and prepend operations' do
          memcached_persistent(p) do |dc|
            dc.flush

            assert op_addset_succeeds(dc.set('456', 'xyz', 0, raw: true))
            assert dc.prepend('456', '0')
            assert dc.append('456', '9')
            assert_equal '0xyz9', dc.get('456')

            refute dc.append('nonexist', 'abc')
            refute dc.prepend('nonexist', 'abc')
          end
        end
      end
    end
  end
end