File: blocking_commands.rb

package info (click to toggle)
ruby-redis 5.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,160 kB
  • sloc: ruby: 11,445; makefile: 117; sh: 24
file content (189 lines) | stat: -rw-r--r-- 5,414 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
# frozen_string_literal: true

module Lint
  module BlockingCommands
    def setup
      super

      r.rpush('{zap}foo', 's1')
      r.rpush('{zap}foo', 's2')
      r.rpush('{zap}bar', 's1')
      r.rpush('{zap}bar', 's2')

      r.zadd('{szap}foo', %w[0 a 1 b 2 c])
      r.zadd('{szap}bar', %w[0 c 1 d 2 e])
    end

    def to_protocol(obj)
      case obj
      when String
        "$#{obj.length}\r\n#{obj}\r\n"
      when Array
        "*#{obj.length}\r\n" + obj.map { |e| to_protocol(e) }.join
      else
        raise
      end
    end

    def mock(options = {}, &blk)
      commands = build_mock_commands(options)
      redis_mock(commands, { timeout: TIMEOUT }, &blk)
    end

    def build_mock_commands(options = {})
      {
        blmove: lambda do |*args|
          sleep options[:delay] if options.key?(:delay)
          to_protocol(args.last)
        end,
        blpop: lambda do |*args|
          sleep options[:delay] if options.key?(:delay)
          to_protocol([args.first, args.last])
        end,
        brpop: lambda do |*args|
          sleep options[:delay] if options.key?(:delay)
          to_protocol([args.first, args.last])
        end,
        brpoplpush: lambda do |*args|
          sleep options[:delay] if options.key?(:delay)
          to_protocol(args.last)
        end,
        bzpopmax: lambda do |*args|
          sleep options[:delay] if options.key?(:delay)
          to_protocol([args.first, args.last])
        end,
        bzpopmin: lambda do |*args|
          sleep options[:delay] if options.key?(:delay)
          to_protocol([args.first, args.last])
        end
      }
    end

    def test_blmove
      target_version "6.2" do
        assert_equal 's1', r.blmove('{zap}foo', '{zap}bar', 'LEFT', 'RIGHT')
        assert_equal ['s2'], r.lrange('{zap}foo', 0, -1)
        assert_equal ['s1', 's2', 's1'], r.lrange('{zap}bar', 0, -1)
      end
    end

    def test_blmove_timeout
      target_version "6.2" do
        mock do |r|
          assert_equal '0', r.blmove('{zap}foo', '{zap}bar', 'LEFT', 'RIGHT')
          assert_equal LOW_TIMEOUT.to_s, r.blmove('{zap}foo', '{zap}bar', 'LEFT', 'RIGHT', timeout: LOW_TIMEOUT)
        end
      end
    end

    def test_blpop
      assert_equal ['{zap}foo', 's1'], r.blpop('{zap}foo')
      assert_equal ['{zap}foo', 's2'], r.blpop(['{zap}foo'])
      assert_equal ['{zap}bar', 's1'], r.blpop(['{zap}bar', '{zap}foo'])
      assert_equal ['{zap}bar', 's2'], r.blpop(['{zap}foo', '{zap}bar'])
    end

    def test_blpop_timeout
      mock do |r|
        assert_equal ['{zap}foo', '0'], r.blpop('{zap}foo')
        assert_equal ['{zap}foo', LOW_TIMEOUT.to_s], r.blpop('{zap}foo', timeout: LOW_TIMEOUT)
      end
    end

    class FakeDuration
      def initialize(int)
        @int = int
      end

      def to_int
        @int
      end
    end

    def test_blpop_integer_like_timeout
      assert_raises ArgumentError do
        assert_equal ["{zap}foo", "1"], r.blpop("{zap}foo", timeout: FakeDuration.new(1))
      end
    end

    def test_brpop
      assert_equal ['{zap}foo', 's2'], r.brpop('{zap}foo')
      assert_equal ['{zap}foo', 's1'], r.brpop(['{zap}foo'])
      assert_equal ['{zap}bar', 's2'], r.brpop(['{zap}bar', '{zap}foo'])
      assert_equal ['{zap}bar', 's1'], r.brpop(['{zap}foo', '{zap}bar'])
    end

    def test_brpop_timeout
      mock do |r|
        assert_equal ['{zap}foo', '0'], r.brpop('{zap}foo')
        assert_equal ['{zap}foo', LOW_TIMEOUT.to_s], r.brpop('{zap}foo', timeout: LOW_TIMEOUT)
      end
    end

    def test_brpoplpush
      assert_equal 's2', r.brpoplpush('{zap}foo', '{zap}qux')
      assert_equal ['s2'], r.lrange('{zap}qux', 0, -1)
    end

    def test_brpoplpush_timeout
      mock do |r|
        assert_equal '0', r.brpoplpush('{zap}foo', '{zap}bar')
        assert_equal LOW_TIMEOUT.to_s, r.brpoplpush('{zap}foo', '{zap}bar', timeout: LOW_TIMEOUT)
      end
    end

    def test_bzpopmin
      assert_equal ['{szap}foo', 'a', 0.0], r.bzpopmin('{szap}foo', '{szap}bar', timeout: 1)
    end

    def test_bzpopmin_float_timeout
      target_version "6.0" do
        assert_nil r.bzpopmin('{szap}aaa', '{szap}bbb', timeout: LOW_TIMEOUT)
      end
    end

    def test_bzpopmax
      assert_equal ['{szap}foo', 'c', 2.0], r.bzpopmax('{szap}foo', '{szap}bar', timeout: 1)
    end

    def test_bzpopmax_float_timeout
      target_version "6.0" do
        assert_nil r.bzpopmax('{szap}aaa', '{szap}bbb', timeout: LOW_TIMEOUT)
      end
    end

    def test_blmove_socket_timeout
      target_version "6.2" do
        mock(delay: TIMEOUT * 5) do |r|
          assert_raises(Redis::TimeoutError) do
            r.blmove('{zap}foo', '{zap}bar', 'LEFT', 'RIGHT', timeout: LOW_TIMEOUT)
          end
        end
      end
    end

    def test_blpop_socket_timeout
      mock(delay: TIMEOUT * 5) do |r|
        assert_raises(Redis::TimeoutError) do
          r.blpop('{zap}foo', timeout: LOW_TIMEOUT)
        end
      end
    end

    def test_brpop_socket_timeout
      mock(delay: TIMEOUT * 5) do |r|
        assert_raises(Redis::TimeoutError) do
          r.brpop('{zap}foo', timeout: LOW_TIMEOUT)
        end
      end
    end

    def test_brpoplpush_socket_timeout
      mock(delay: TIMEOUT * 5) do |r|
        assert_raises(Redis::TimeoutError) do
          r.brpoplpush('{zap}foo', '{zap}bar', timeout: LOW_TIMEOUT)
        end
      end
    end
  end
end