File: distributed_store_test.rb

package info (click to toggle)
ruby-redis-store 1.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 320 kB
  • sloc: ruby: 1,514; makefile: 6
file content (112 lines) | stat: -rw-r--r-- 3,439 bytes parent folder | download | duplicates (2)
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
require 'test_helper'
require 'ostruct'

describe "Redis::DistributedStore" do
  def setup
    @dmr = Redis::DistributedStore.new [
      { :host => "localhost", :port => "6380", :db => 0 },
      { :host => "localhost", :port => "6381", :db => 0 }
    ]
    @rabbit = OpenStruct.new :name => "bunny"
    @white_rabbit = OpenStruct.new :color => "white"
    @dmr.set "rabbit", @rabbit
  end

  def teardown
    @dmr.ring.nodes.each { |server| server.flushdb }
  end

  it "accepts connection params" do
    dmr = Redis::DistributedStore.new [ :host => "localhost", :port => "6380", :db => "1" ]
    _(dmr.ring.nodes.size).must_equal(1)
    mr = dmr.ring.nodes.first
    _(mr.to_s).must_equal("Redis Client connected to localhost:6380 against DB 1")
  end

  it "forces reconnection" do
    @dmr.nodes.each do |node|
      node.expects(:reconnect)
    end

    @dmr.reconnect
  end

  it "sets an object" do
    @dmr.set "rabbit", @white_rabbit
    _(@dmr.get("rabbit")).must_equal(@white_rabbit)
  end

  it "gets an object" do
    _(@dmr.get("rabbit")).must_equal(@rabbit)
  end

  it "mget" do
    @dmr.set "rabbit2", @white_rabbit
    begin
      @dmr.mget "rabbit", "rabbit2" do |rabbits|
        rabbit, rabbit2 = rabbits
        _(rabbits.length).must_equal(2)
        _(rabbit).must_equal(@rabbit)
        _(rabbit2).must_equal(@white_rabbit)
      end
    rescue Redis::Distributed::CannotDistribute
      # Not supported on redis-rb < 4, and hence Ruby < 2.2.
    end
  end

  it "mapped_mget" do
    @dmr.set "rabbit2", @white_rabbit
    begin
      result = @dmr.mapped_mget("rabbit", "rabbit2")
      _(result.keys).must_equal %w[ rabbit rabbit2 ]
      _(result["rabbit"]).must_equal @rabbit
      _(result["rabbit2"]).must_equal @white_rabbit
    rescue Redis::Distributed::CannotDistribute
      # Not supported on redis-rb < 4, and hence Ruby < 2.2.
    end
  end

  it "passes through ring replica options" do
    dmr = Redis::DistributedStore.new [
                                    { :host => "localhost", :port => "6380", :db => 0 },
                                    { :host => "localhost", :port => "6381", :db => 0 }
                                ], replicas: 1024
    _(dmr.ring.replicas).must_equal 1024
  end

  it "uses a custom ring object" do
    my_ring = Redis::HashRing.new
    dmr = Redis::DistributedStore.new [
                                          { :host => "localhost", :port => "6380", :db => 0 },
                                          { :host => "localhost", :port => "6381", :db => 0 }
                                      ], ring: my_ring
    _(dmr.ring).must_equal my_ring
    _(dmr.ring.nodes.length).must_equal 2
  end

  describe '#redis_version' do
    it 'returns redis version' do
      @dmr.nodes.first.expects(:redis_version)
      @dmr.redis_version
    end
  end

  describe '#supports_redis_version?' do
    it 'returns redis version' do
      @dmr.nodes.first.expects(:supports_redis_version?).with('2.8.0')
      @dmr.supports_redis_version?('2.8.0')
    end
  end

  describe "namespace" do
    it "uses namespaced key" do
      @dmr = Redis::DistributedStore.new [
        { :host => "localhost", :port => "6380", :db => 0 },
        { :host => "localhost", :port => "6381", :db => 0 }
      ], :namespace => "theplaylist"

      @dmr.expects(:node_for).with("theplaylist:rabbit").returns(@dmr.nodes.first)
      @dmr.get "rabbit"
    end
  end
end unless ENV['CI']