File: sentinel_command_test.rb

package info (click to toggle)
ruby-redis 3.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 736 kB
  • ctags: 1,128
  • sloc: ruby: 8,149; makefile: 5
file content (80 lines) | stat: -rw-r--r-- 2,157 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
# encoding: UTF-8

require File.expand_path("helper", File.dirname(__FILE__))

class SentinalCommandsTest < Test::Unit::TestCase

  include Helper::Client

  def test_sentinel_command_master

    handler = lambda do |id|
      {
        :sentinel => lambda do |command, *args|
          ["name", "master1", "ip", "127.0.0.1"]
        end
      }
    end

    RedisMock.start(handler.call(:s1)) do |port|
      redis = Redis.new(:host => "127.0.0.1", :port => port)

      result = redis.sentinel('master', 'master1')
      assert_equal result, { "name" => "master1", "ip" => "127.0.0.1" }
    end
  end

  def test_sentinel_command_masters

    handler = lambda do |id|
      {
        :sentinel => lambda do |command, *args|
          [%w[name master1 ip 127.0.0.1 port 6381], %w[name master1 ip 127.0.0.1 port 6382]]
        end
      }
    end

    RedisMock.start(handler.call(:s1)) do |port|
      redis = Redis.new(:host => "127.0.0.1", :port => port)

      result = redis.sentinel('masters')
      assert_equal result[0], { "name" => "master1", "ip" => "127.0.0.1", "port" => "6381" }
      assert_equal result[1], { "name" => "master1", "ip" => "127.0.0.1", "port" => "6382" }
    end
  end

  def test_sentinel_command_get_master_by_name

    handler = lambda do |id|
      {
        :sentinel => lambda do |command, *args|
          ["127.0.0.1", "6381"]
        end
      }
    end

    RedisMock.start(handler.call(:s1)) do |port|
      redis = Redis.new(:host => "127.0.0.1", :port => port)

      result = redis.sentinel('get-master-addr-by-name', 'master1')
      assert_equal result, ["127.0.0.1", "6381"]
    end
  end

  def test_sentinel_command_ckquorum
    handler = lambda do |id|
      {
        :sentinel => lambda do |command, *args|
          "+OK 2 usable Sentinels. Quorum and failover authorization can be reached"
        end
      }
    end

    RedisMock.start(handler.call(:s1)) do |port|
      redis = Redis.new(:host => "127.0.0.1", :port => port)

      result = redis.sentinel('ckquorum', 'master1')
      assert_equal result, "OK 2 usable Sentinels. Quorum and failover authorization can be reached"
    end
  end
end