File: redis_protocol_spec.rb

package info (click to toggle)
ruby-em-redis 0.3.0%2Bgh-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 240 kB
  • ctags: 73
  • sloc: ruby: 1,548; makefile: 9; sh: 4
file content (136 lines) | stat: -rw-r--r-- 3,608 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
require File.expand_path(File.dirname(__FILE__) + "/test_helper.rb")

EM.describe EM::Protocols::Redis do
  default_timeout 1

  before do
    @c = TestConnection.new
  end

  # Converting array of arguments to Redis protocol
  should "send commands correctly" do
    @c.call_command(["SET", "foo", "abc"])
    @c.sent_data.should == "*3\r\n$3\r\nset\r\n$3\r\nfoo\r\n$3\r\nabc\r\n"
    done
  end

  should "send integers in commands correctly" do
    @c.call_command(["SET", "foo", 1_000_000])
    @c.sent_data.should == "*3\r\n$3\r\nset\r\n$3\r\nfoo\r\n$7\r\n1000000\r\n"
    done
  end

  # Specific calls
  #
  # SORT
  should "send sort command" do
    @c.sort "foo"
    @c.sent_data.should == "*2\r\n$4\r\nsort\r\n$3\r\nfoo\r\n"
    done
  end

  should "send sort command with all optional parameters" do
    @c.sort "foo", :by => "foo_sort_*", :limit => [0, 10], :get => "data_*", :order => "DESC ALPHA"
    @c.sent_data.should == "*11\r\n$4\r\nsort\r\n$3\r\nfoo\r\n$2\r\nBY\r\n$10\r\nfoo_sort_*\r\n$3\r\nGET\r\n$6\r\ndata_*\r\n$4\r\nDESC\r\n$5\r\nALPHA\r\n$5\r\nLIMIT\r\n$1\r\n0\r\n$2\r\n10\r\n"
    done
  end

  should "parse keys response into an array" do
    @c.keys("*") do |resp|
      resp.should == ["a","b","c"]
      done
    end
    @c.receive_data "$5\r\na b c\r\n"
  end


  # Inline response
  should "parse an inline response" do
    @c.call_command(["PING"]) do |resp|
      resp.should == "OK"
      done
    end
    @c.receive_data "+OK\r\n"
  end

  should "parse an inline integer response" do
    @c.call_command(["integer"]) do |resp|
      resp.should == 0
      done
    end
    @c.receive_data ":0\r\n"
  end

  should "call processor if any" do
    @c.call_command(["EXISTS"]) do |resp|
      resp.should == false
      done
    end
    @c.receive_data ":0\r\n"
  end

  should "parse an inline error response" do
    lambda do
      @c.call_command(["blarg"])
      @c.receive_data "-FAIL\r\n"
    end.should.raise(EM::P::Redis::RedisError)
    done
  end

  should "trigger a given error callback (specified with on_error) for inline error response instead of raising an error" do
    lambda do
      @c.call_command(["blarg"])
      @c.on_error {|code| code.should == "FAIL"; done }
      @c.receive_data "-FAIL\r\n"
    end.should.not.raise(EM::P::Redis::RedisError)
  end

  should "trigger a given error callback for inline error response instead of raising an error" do
    lambda do
      @c.call_command(["blarg"])
      @c.errback { |code| code.should == "FAIL"; done }
      @c.receive_data "-FAIL\r\n"
    end.should.not.raise(EM::P::Redis::RedisError)
  end

  # Bulk response
  should "parse a bulk response" do
    @c.call_command(["GET", "foo"]) do |resp|
      resp.should == "bar"
      done
    end
    @c.receive_data "$3\r\n"
    @c.receive_data "bar\r\n"
  end

  should "distinguish nil in a bulk response" do
    @c.call_command(["GET", "bar"]) do |resp|
      resp.should == nil
      done
    end
    @c.receive_data "$-1\r\n"
  end

  # Multi-bulk response
  should "parse a multi-bulk response" do
    @c.call_command(["RANGE", 0, 10]) do |resp|
      resp.should == ["a", "b", "foo"]
      done
    end
    @c.receive_data "*3\r\n"
    @c.receive_data "$1\r\na\r\n"
    @c.receive_data "$1\r\nb\r\n"
    @c.receive_data "$3\r\nfoo\r\n"
  end

  should "distinguish nil in a multi-bulk response" do
    @c.call_command(["RANGE", 0, 10]) do |resp|
      resp.should == ["a", nil, "foo"]
      done
    end
    @c.receive_data "*3\r\n"
    @c.receive_data "$1\r\na\r\n"
    @c.receive_data "$-1\r\n"
    @c.receive_data "$3\r\nfoo\r\n"
  end
end