File: redis_store_test.rb

package info (click to toggle)
ruby-redis-activesupport 5.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 268 kB
  • sloc: ruby: 867; makefile: 4
file content (782 lines) | stat: -rw-r--r-- 25,318 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
require 'test_helper'
require 'ostruct'
require 'connection_pool'

describe ActiveSupport::Cache::RedisStore do
  def setup
    @store  = ActiveSupport::Cache::RedisStore.new
    @dstore = ActiveSupport::Cache::RedisStore.new "redis://127.0.0.1:6379/5", "redis://127.0.0.1:6379/6"
    @pool_store  = ActiveSupport::Cache::RedisStore.new("redis://127.0.0.1:6379/2", pool_size: 5, pool_timeout: 10)
    @external_pool_store = ActiveSupport::Cache::RedisStore.new(pool: ::ConnectionPool.new(size: 1, timeout: 1) { ::Redis::Store::Factory.create("redis://127.0.0.1:6379/3") })

    @pool_store.data.class.must_equal ::ConnectionPool
    @pool_store.data.instance_variable_get(:@size).must_equal 5
    @external_pool_store.data.class.must_equal ::ConnectionPool
    @external_pool_store.data.instance_variable_get(:@size).must_equal 1


    @rabbit = OpenStruct.new :name => "bunny"
    @white_rabbit = OpenStruct.new :color => "white"

    with_store_management do |store|
      store.write "rabbit", @rabbit
      store.delete "counter"
      store.delete "rub-a-dub"
      store.delete({hkey: 'test'})
    end
  end

  it "uses redis client passed as an option" do
    redis = Redis.new(url: "redis://127.0.0.1:6380/1")
    store = ActiveSupport::Cache::RedisStore.new(client: redis)

    store.data.must_equal(redis)
  end

  it "connects using an hash of options" do
    address = { host: '127.0.0.1', port: '6380', db: '1' }
    store = ActiveSupport::Cache::RedisStore.new(address.merge(pool_size: 5, pool_timeout: 10))
    redis = Redis.new(url: "redis://127.0.0.1:6380/1")
    redis.flushall

    store.data.class.must_equal(::ConnectionPool)
    store.data.instance_variable_get(:@size).must_equal(5)
    store.data.instance_variable_get(:@timeout).must_equal(10)

    store.write("rabbit", 0)

    redis.exists?("rabbit").must_equal(true)
  end

  it "connects using an string of options" do
    address = "redis://127.0.0.1:6380/1"
    store = ActiveSupport::Cache::RedisStore.new(address, pool_size: 5, pool_timeout: 10)
    redis = Redis.new(url: address)
    redis.flushall

    store.data.class.must_equal(::ConnectionPool)
    store.data.instance_variable_get(:@size).must_equal(5)
    store.data.instance_variable_get(:@timeout).must_equal(10)

    store.write("rabbit", 0)

    redis.exists?("rabbit").must_equal(true)
  end

  it "connects using the passed hash of options" do
    address = { host: '127.0.0.1', port: '6380', db: '1' }.merge(pool_size: 5, pool_timeout: 10)
    store = ActiveSupport::Cache::RedisStore.new(address)
    redis = Redis.new(url: "redis://127.0.0.1:6380/1")
    redis.flushall
    address[:db] = '0' # Should not use this db

    store.data.class.must_equal(::ConnectionPool)

    store.write("rabbit", 0)

    redis.exists?("rabbit").must_equal(true)
  end

  it "raises an error if :pool isn't a pool" do
    assert_raises(RuntimeError, 'pool must be an instance of ConnectionPool') do
      ActiveSupport::Cache::RedisStore.new(pool: 'poolio')
    end
  end

  it "namespaces all operations" do
    address = "redis://127.0.0.1:6380/1/cache-namespace"
    store   = ActiveSupport::Cache::RedisStore.new(address)
    redis   = Redis.new(url: address)

    store.write("white-rabbit", 0)

    redis.exists?('cache-namespace:white-rabbit').must_equal(true)
  end

  it "creates a normal store when given no addresses" do
    underlying_store = instantiate_store
    underlying_store.must_be_instance_of(::Redis::Store)
  end

  it "creates a normal store when given nil" do
    underlying_store = instantiate_store nil
    underlying_store.must_be_instance_of(::Redis::Store)
  end

  it "creates a normal store when given options only" do
    underlying_store = instantiate_store(:expires_in => 1.second)
    underlying_store.must_be_instance_of(::Redis::Store)
  end

  it "creates a normal store when given a single address" do
    underlying_store = instantiate_store("redis://127.0.0.1:6380/1")
    underlying_store.must_be_instance_of(::Redis::Store)
  end

  it "creates a normal store when given a single address and options" do
    underlying_store = instantiate_store("redis://127.0.0.1:6380/1",
                                         { :expires_in => 1.second})
    underlying_store.must_be_instance_of(::Redis::Store)
  end

  it "creates a distributed store when given multiple addresses" do
    underlying_store = instantiate_store("redis://127.0.0.1:6380/1",
                                         "redis://127.0.0.1:6381/1")
    underlying_store.must_be_instance_of(::Redis::DistributedStore)
  end

  it "creates a distributed store when given multiple address and options" do
    underlying_store = instantiate_store("redis://127.0.0.1:6380/1",
                                         "redis://127.0.0.1:6381/1",
                                         :expires_in => 1.second)
    underlying_store.must_be_instance_of(::Redis::DistributedStore)
  end

  it "reads the data" do
    with_store_management do |store|
      store.read("rabbit").must_equal(@rabbit)
    end
  end

  it "writes the data" do
    with_store_management do |store|
      store.write "rabbit", @white_rabbit
      store.read("rabbit").must_equal(@white_rabbit)
    end
  end

  it "writes the data with specified namespace" do
    with_store_management do |store|
      store.write "rabbit", @white_rabbit, namespace:'namespaced'
      store.read("namespaced:rabbit").must_equal(@white_rabbit)
    end
  end

  it "writes the data with expiration time" do
    with_store_management do |store|
      store.write "rabbit", @white_rabbit, :expires_in => 1.second
      store.read("rabbit").must_equal(@white_rabbit)
      sleep 2
      store.read("rabbit").must_be_nil
    end
  end

  it "respects expiration time in seconds" do
    with_store_management do |store|
      store.write "rabbit", @white_rabbit
      store.read("rabbit").must_equal(@white_rabbit)
      store.expire "rabbit", 1.second
      sleep 2
      store.read("rabbit").must_be_nil
    end
  end

  it "respects expiration time in seconds for object key" do
    with_store_management do |store|
      store.write({ hkey: 'test' }, @white_rabbit)
      store.read({ hkey: 'test' }).must_equal(@white_rabbit)
      store.expire({ hkey: 'test' }, 1.second)
      sleep 2
      store.read({ hkey: 'test' }).must_be_nil
    end
  end

  it "does't write data if :unless_exist option is true" do
    with_store_management do |store|
      store.write "rabbit", @white_rabbit, :unless_exist => true
      store.read("rabbit").must_equal(@rabbit)
    end
  end

  if RUBY_VERSION.match(/1\.9/)
    it "reads raw data" do
      with_store_management do |store|
        result = store.read("rabbit", :raw => true)
        result.must_include("ActiveSupport::Cache::Entry")
        result.must_include("\x0FOpenStruct{\x06:\tnameI\"\nbunny\x06:\x06EF")
      end
    end
  else
    it "reads raw data" do
      with_store_management do |store|
        result = store.read("rabbit", :raw => true)
        result.must_include("ActiveSupport::Cache::Entry")
        result.must_include("\017OpenStruct{\006:\tname")
      end
    end
  end

  it "writes raw data" do
    with_store_management do |store|
      store.write "rabbit", @white_rabbit, :raw => true
      store.read("rabbit", :raw => true).must_equal(%(#<OpenStruct color=\"white\">))
    end
  end

  it "deletes data" do
    with_store_management do |store|
      store.delete "rabbit"
      store.read("rabbit").must_be_nil
    end
  end

  it "deletes namespaced data" do
    with_store_management do |store|
      store.write "rabbit", @white_rabbit, namespace:'namespaced'
      store.delete "rabbit", namespace:'namespaced'
      store.read("namespaced:rabbit").must_be_nil
    end
  end

  it "deletes matched data" do
    with_store_management do |store|
      store.write "rabbit2", @white_rabbit
      store.write "rub-a-dub", "Flora de Cana"
      store.delete_matched "rabb*"
      store.read("rabbit").must_be_nil
      store.read("rabbit2").must_be_nil
      store.exist?("rub-a-dub").must_equal(true)
    end
  end

  it 'deletes matched data with a regexp' do
    with_store_management do |store|
      store.write "rabbit2", @white_rabbit
      store.write "rub-a-dub", "Flora de Cana"
      store.delete_matched(/rabb*/)

      store.read("rabbit").must_be_nil
      store.read("rabbit2").must_be_nil
      store.exist?("rub-a-dub").must_equal(true)
    end
  end

  it "verifies existence of an object in the store" do
    with_store_management do |store|
      store.exist?("rabbit").must_equal(true)
      store.exist?("rab-a-dub").must_equal(false)
    end
  end

  it "increments a key" do
    with_store_management do |store|
      3.times { store.increment "counter" }
      store.read("counter", :raw => true).to_i.must_equal(3)
    end
  end

  it "decrements a key" do
    with_store_management do |store|
      3.times { store.increment "counter" }
      2.times { store.decrement "counter" }
      store.read("counter", :raw => true).to_i.must_equal(1)
    end
  end

  it "increments an object key" do
    with_store_management do |store|
      3.times { store.increment({ hkey: 'test' }) }
      store.read({ hkey: 'test' }, :raw => true).to_i.must_equal(3)
    end
  end

  it "decrements an object key" do
    with_store_management do |store|
      3.times { store.increment({ hkey: 'test' }) }
      2.times { store.decrement({ hkey: 'test' }) }
      store.read({hkey: 'test'}, :raw => true).to_i.must_equal(1)
    end
  end

  it "increments a raw key" do
    with_store_management do |store|
      assert store.write("raw-counter", 1, :raw => true)
      store.increment("raw-counter", 2)
      store.read("raw-counter", :raw => true).to_i.must_equal(3)
    end
  end

  it "increments a key with options argument" do
    with_store_management do |store|
      assert store.write("raw-counter", 1, :raw => true)
      store.increment("raw-counter", 2, nil)
      store.read("raw-counter", :raw => true).to_i.must_equal(3)
    end
  end

  it "decrements a raw key" do
    with_store_management do |store|
      assert store.write("raw-counter", 3, :raw => true)
      store.decrement("raw-counter", 2)
      store.read("raw-counter", :raw => true).to_i.must_equal(1)
    end
  end

  it "increments a key by given value" do
    with_store_management do |store|
      store.increment "counter", 3
      store.read("counter", :raw => true).to_i.must_equal(3)
    end
  end

  it "decrements a key by given value" do
    with_store_management do |store|
      3.times { store.increment "counter" }
      store.decrement "counter", 2
      store.read("counter", :raw => true).to_i.must_equal(1)
    end
  end

  it "decrements a key with an options argument" do
    with_store_management do |store|
      3.times { store.increment "counter" }
      store.decrement "counter", 2, nil
      store.read("counter", :raw => true).to_i.must_equal(1)
    end
  end

  it "increments a key with expiration time" do
    with_store_management do |store|
      store.increment "counter", 1, :expires_in => 1.second
      store.read("counter", :raw => true).to_i.must_equal(1)
      sleep 2
      store.read("counter", :raw => true).must_be_nil
    end
  end

  it "decrements a key with expiration time" do
    with_store_management do |store|
      store.decrement "counter", 1, :expires_in => 1.second
      store.read("counter", :raw => true).to_i.must_equal(-1)
      sleep 2
      store.read("counter", :raw => true).must_be_nil
    end
  end

  it "clears the store" do
    with_store_management do |store|
      store.clear
      store.with { |client| client.keys("*") }.flatten.must_be_empty
    end
  end

  it "provides store stats" do
    with_store_management do |store|
      store.stats.wont_be_empty
    end
  end

  it "fetches data" do
    with_store_management do |store|
      store.fetch("rabbit").must_equal(@rabbit)
      store.fetch("rub-a-dub").must_be_nil
      store.fetch("rub-a-dub") { "Flora de Cana" }
      store.fetch("rub-a-dub").must_equal("Flora de Cana")
    end
  end

  it "fetches data with expiration time" do
    with_store_management do |store|
      store.fetch("rabbit", :force => true) {} # force cache miss
      store.fetch("rabbit", :force => true, :expires_in => 1.second) { @white_rabbit }
      store.fetch("rabbit").must_equal(@white_rabbit)
      sleep 2
      store.fetch("rabbit").must_be_nil
    end
  end

  it "fetches namespaced data" do
    with_store_management do |store|
      store.delete("rabbit", namespace:'namespaced')
      store.fetch("rabbit", namespace:'namespaced'){@rabbit}.must_equal(@rabbit)
      store.read("rabbit", namespace:'namespaced').must_equal(@rabbit)
    end
  end

  describe "race_condition_ttl on fetch" do
    it "persist entry for longer than given ttl" do
      options = { force: true, expires_in: 1.second, race_condition_ttl: 2.seconds, version: Time.now.to_i }
      @store.fetch("rabbit", options) { @rabbit }
      sleep 1.1
      @store.delete("rabbit").must_equal(1)
    end

    it "limits stampede time to read-write duration" do
      first_rabbit = second_rabbit = nil
      options = { force: true, expires_in: 1.second, race_condition_ttl: 2.seconds, version: Time.now.to_i }
      @store.fetch("rabbit", options) { @rabbit }
      sleep 1

      th1 = Thread.new do
        first_rabbit = @store.fetch("rabbit", options) do
          sleep 1
          @white_rabbit
        end
      end

      sleep 0.1

      th2 = Thread.new do
        second_rabbit = @store.fetch("rabbit") { @white_rabbit }
      end

      th1.join
      th2.join

      first_rabbit.must_equal(@white_rabbit)
      second_rabbit.must_equal(@rabbit)

      @store.fetch("rabbit").must_equal(@white_rabbit)
    end
  end

  it "reads multiple keys" do
    @store.write "irish whisky", "Jameson"
    result = @store.read_multi "rabbit", "irish whisky"
    result['rabbit'].must_equal(@rabbit)
    result['irish whisky'].must_equal("Jameson")
  end

  it "reads multiple keys and returns only the matched ones" do
    @store.delete 'irish whisky'
    result = @store.read_multi "rabbit", "irish whisky"
    result.wont_include('irish whisky')
    result.must_include('rabbit')
  end

  it "reads multiple namespaced keys" do
    @store.write "rub-a-dub", "Flora de Cana", namespace:'namespaced'
    @store.write "irish whisky", "Jameson", namespace:'namespaced'
    result = @store.read_multi "rub-a-dub", "irish whisky", namespace:'namespaced'
    result['rub-a-dub'].must_equal("Flora de Cana")
    result['irish whisky'].must_equal("Jameson")
  end

  it "read_multi return an empty {} when given an empty array" do
    result = @store.read_multi(*[])
    result.must_equal({})
  end

  it "read_multi return an empty {} when given an empty array with option" do
    result = @store.read_multi(*[], option: true)
    result.must_equal({})
  end

  it "read_multi returns values with raw option" do
    @store.write "raw-value-a", "A", raw: true
    @store.write "raw-value-b", "B", raw: true

    result = @store.read_multi("raw-value-a", "raw-value-b", :raw => true)
    result.must_equal({ "raw-value-a" => "A", "raw-value-b" => "B" })
  end

  describe "fetch_multi" do
    it "reads existing keys and fills in anything missing" do
      @store.write "bourbon", "makers"

      result = @store.fetch_multi("bourbon", "rye") do |key|
        "#{key}-was-missing"
      end

      result.must_equal({ "bourbon" => "makers", "rye" => "rye-was-missing" })
      @store.read("rye").must_equal("rye-was-missing")
    end

    it "fetch command within fetch_multi block" do
      @store.delete 'rye'
      @store.write "bourbon", "makers"

      result = @store.fetch_multi("bourbon", "rye") do |key|
        @store.fetch "inner-#{key}" do
          "#{key}-was-missing"
        end
      end

      result.must_equal({ "bourbon" => "makers", "rye" => "rye-was-missing" })
      @store.read("rye").must_equal("rye-was-missing")
      @store.read("inner-rye").must_equal("rye-was-missing")
    end

    it "return an empty {} when given an empty array" do
      result = @store.fetch_multi(*[]) { 1 }
      result.must_equal({})
    end

    it "return an empty {} when given an empty array with option" do
      result = @store.read_multi(*[], option: true)
      result.must_equal({})
    end
  end

  describe "fetch_multi namespaced keys" do
    it "reads existing keys and fills in anything missing" do
      @store.write "bourbon", "makers", namespace: 'namespaced'

      result = @store.fetch_multi("bourbon", "rye", namespace: 'namespaced') do |key|
        "#{key}-was-missing"
      end

      result.must_equal({ "bourbon" => "makers", "rye" => "rye-was-missing" })
      @store.read("namespaced:rye").must_equal("rye-was-missing")
    end

    it "fetch command within fetch_multi block" do
      @store.delete 'namespaced:rye'
      @store.write "bourbon", "makers", namespace: 'namespaced'

      result = @store.fetch_multi("bourbon", "rye", namespace: 'namespaced') do |key|
        @store.fetch "namespaced:inner-#{key}" do
          "#{key}-was-missing"
        end
      end

      result.must_equal({ "bourbon" => "makers", "rye" => "rye-was-missing" })
      @store.read("namespaced:rye").must_equal("rye-was-missing")
      @store.read("namespaced:inner-rye").must_equal("rye-was-missing")
    end
  end

  describe "fetch_multi nested keys" do
    it "reads existing keys and fills in anything missing" do
      @store.write ["bourbon", "bourbon-extended"], "makers"

      bourbon_key = ["bourbon", "bourbon-extended"]
      rye_key = ["rye", "rye-extended"]

      result = @store.fetch_multi(bourbon_key, rye_key) do |key|
        "#{key}-was-missing"
      end

      result.must_equal({ bourbon_key => "makers", rye_key => "#{rye_key}-was-missing" })
      @store.read(rye_key).must_equal("#{rye_key}-was-missing")
    end
  end

  describe "notifications" do
    it "notifies on #fetch" do
      with_notifications do
        @store.fetch("radiohead") { "House Of Cards" }
      end

      read, generate, write = @events
      if ActiveSupport::VERSION::MAJOR < 5
        read_payload = { :key => 'radiohead', :super_operation => :fetch }
      else
        read_payload = { :key => 'radiohead', :super_operation => :fetch, hit: false }
      end

      read.name.must_equal('cache_read.active_support')
      read.payload.must_equal(read_payload) if ActiveSupport::VERSION::MAJOR < 6

      generate.name.must_equal('cache_generate.active_support')
      generate.payload.must_equal({ :key => 'radiohead' }) if ActiveSupport::VERSION::MAJOR < 6

      write.name.must_equal('cache_write.active_support')
      write.payload.must_equal({ :key => 'radiohead' }) if ActiveSupport::VERSION::MAJOR < 6
    end

    it "notifies on #read" do
      with_notifications do
        @store.read "metallica"
      end

      read = @events.first
      read.name.must_equal('cache_read.active_support')
      read.payload.must_equal({ :key => 'metallica', :hit => false }) if ActiveSupport::VERSION::MAJOR < 6
    end

    it "notifies on #write" do
      with_notifications do
        @store.write "depeche mode", "Enjoy The Silence"
      end

      write = @events.first
      write.name.must_equal('cache_write.active_support')
      write.payload.must_equal({ :key => 'depeche mode' }) if ActiveSupport::VERSION::MAJOR < 6
    end

    it "notifies on #delete" do
      with_notifications do
        @store.delete "the new cardigans"
      end

      delete = @events.first
      delete.name.must_equal('cache_delete.active_support')
      delete.payload.must_equal({ :key => 'the new cardigans' }) if ActiveSupport::VERSION::MAJOR < 6
    end

    it "notifies on #exist?" do
      with_notifications do
        @store.exist? "the smiths"
      end

      exist = @events.first
      exist.name.must_equal('cache_exist?.active_support')
      exist.payload.must_equal({ :key => 'the smiths' }) if ActiveSupport::VERSION::MAJOR < 6
    end

    it "notifies on #read_multi" do
      @store.write "depeche mode", "Enjoy The Silence"

      with_notifications do
        @store.read_multi "metallica", "depeche mode"
      end

      read = @events.first
      read.name.must_equal('cache_read_multi.active_support')
      read.payload.must_equal({ :key => ["metallica", "depeche mode"], :hits => ["depeche mode"] }) if ActiveSupport::VERSION::MAJOR < 6
    end

    it "notifies on #delete_matched" do
      with_notifications do
        @store.delete_matched "afterhours*"
      end

      delete_matched = @events.first
      delete_matched.name.must_equal('cache_delete_matched.active_support')
      delete_matched.payload.must_equal({ :key => %("afterhours*") }) if ActiveSupport::VERSION::MAJOR < 6
    end

    it "notifies on #increment" do
      with_notifications do
        @store.increment "pearl jam"
      end

      increment = @events.first
      increment.name.must_equal('cache_increment.active_support')
      increment.payload.must_equal({ :key => 'pearl jam', :amount => 1 }) if ActiveSupport::VERSION::MAJOR < 6
    end

    it "notifies on #decrement" do
      with_notifications do
        @store.decrement "placebo"
      end

      decrement = @events.first
      decrement.name.must_equal('cache_decrement.active_support')
      decrement.payload.must_equal({ :key => 'placebo', :amount => 1 }) if ActiveSupport::VERSION::MAJOR < 6
    end

    # it "notifies on cleanup" # TODO implement in ActiveSupport::Cache::RedisStore

    it "should notify on clear" do
      with_notifications do
        @store.clear
      end

      clear = @events.first
      clear.name.must_equal('cache_clear.active_support')
      clear.payload.must_equal({ :key => nil }) if ActiveSupport::VERSION::MAJOR < 6
    end
  end

  describe "raise_errors => true" do
    def setup
      @raise_error_store = ActiveSupport::Cache::RedisStore.new("redis://127.0.0.1:6380/1", :raise_errors => true)
      @raise_error_store.stubs(:with).raises(Redis::CannotConnectError)
    end

    it "raises on read when redis is unavailable" do
      assert_raises(Redis::CannotConnectError) do
        @raise_error_store.read("rabbit")
      end
    end

    it "raises on read_multi when redis is unavailable" do
      assert_raises(Redis::CannotConnectError) do
        @raise_error_store.read_multi("rabbit", "white-rabbit")
      end
    end

    it "raises on fetch_multi when redis is unavailable" do
      assert_raises(Redis::CannotConnectError) do
        @raise_error_store.fetch_multi("rabbit", "white-rabbit") do |key|
          key.upcase
        end
      end
    end

    it "raises on writes when redis is unavailable" do
      assert_raises(Redis::CannotConnectError) do
        @raise_error_store.write "rabbit", @white_rabbit, :expires_in => 1.second
      end
    end

    it "raises on delete when redis is unavailable" do
      assert_raises(Redis::CannotConnectError) do
        @raise_error_store.delete "rabbit"
      end
    end

    it "raises on delete_matched when redis is unavailable" do
      assert_raises(Redis::CannotConnectError) do
        @raise_error_store.delete_matched "rabb*"
      end
    end
  end

  describe "raise_errors => false" do
    def setup
      @raise_error_store = ActiveSupport::Cache::RedisStore.new("redis://127.0.0.1:6380/1")
      @raise_error_store.stubs(:with).raises(Redis::CannotConnectError)
    end

    it "returns nil from read when redis is unavailable" do
      @raise_error_store.read("rabbit").must_be_nil
    end

    it "returns empty hash from read_multi when redis is unavailable" do
      @raise_error_store.read_multi("rabbit", "white-rabbit").must_equal({})
    end

    it "returns result hash from fetch_multi when redis is unavailable" do
      @raise_error_store.fetch_multi("rabbit", "white-rabbit") do |key|
        key.upcase
      end.must_equal({
        "rabbit" => "RABBIT",
        "white-rabbit" => "WHITE-RABBIT",
      })
    end

    it "returns false when redis is unavailable" do
      @raise_error_store.write("rabbit", @white_rabbit, :expires_in => 1.second).must_equal(false)
    end

    it "returns false when redis is unavailable" do
      @raise_error_store.delete("rabbit").must_equal(false)
    end

    it "raises on delete_matched when redis is unavailable" do
      @raise_error_store.delete_matched("rabb*").must_equal(false)
    end
  end

  private
    def instantiate_store(*addresses)
      ActiveSupport::Cache::RedisStore.new(*addresses).instance_variable_get(:@data)
    end

    def with_store_management
      yield @store
      yield @dstore
      yield @pool_store
      yield @external_pool_store
    end

    def with_notifications
      @events = [ ]
      ActiveSupport::Cache::RedisStore.instrument = true if instrument?
      ActiveSupport::Notifications.subscribe(/^cache_(.*)\.active_support$/) do |*args|
        @events << ActiveSupport::Notifications::Event.new(*args)
      end
      yield
    ensure
      ActiveSupport::Cache::RedisStore.instrument = false if instrument?
    end

    # ActiveSupport::Cache.instrument is always +true+ since Rails 4.2.0
    def instrument?
      ActiveSupport::VERSION::MAJOR < 4 ||
       ActiveSupport::VERSION::MAJOR == 4 && ActiveSupport::VERSION::MINOR < 2
    end
end