File: packet_spec.rb

package info (click to toggle)
ruby-remcached 0.4.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 200 kB
  • sloc: ruby: 1,000; sh: 9; makefile: 2
file content (143 lines) | stat: -rw-r--r-- 4,287 bytes parent folder | download | duplicates (4)
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
$: << File.dirname(__FILE__) + '/../lib'
require 'remcached'

describe Memcached::Packet do

  context "when generating a request" do
    it "should set default values" do
      pkt = Memcached::Request.new
      pkt[:magic].should == 0x80
    end

    context "example 4.2.1" do
      before :all do
        pkt = Memcached::Request.new(:key => 'Hello')
        @s = pkt.to_s
      end

      it "should serialize correctly" do
        @s.should == ("\x80\x00\x00\x05" +
          "\x00\x00\x00\x00" +
          "\x00\x00\x00\x05" +
          "\x00\x00\x00\x00" +
          "\x00\x00\x00\x00" +
          "\x00\x00\x00\x00" +
          "Hello").force_encoding("ASCII-8BIT")
      end
    end

    context "example 4.3.1 (add)" do
      before :all do
        pkt = Memcached::Request::Add.new(:flags => 0xdeadbeef,
                                          :expiration => 0xe10,
                                          :key => "Hello",
                                          :value => "World")
        @s = pkt.to_s
      end

      it "should serialize correctly" do
        @s.should == ("\x80\x02\x00\x05" +
          "\x08\x00\x00\x00" +
          "\x00\x00\x00\x12" +
          "\x00\x00\x00\x00" +
          "\x00\x00\x00\x00" +
          "\x00\x00\x00\x00" +
          "\xde\xad\xbe\xef" +
          "\x00\x00\x0e\x10" +
          "Hello" +
          "World").force_encoding("ASCII-8BIT")
      end
    end
  end

  context "when parsing a response" do
    context "example 4.1.1" do
      before :all do
        s = ("\x81\x00\x00\x00\x00\x00\x00\x01" +
          "\x00\x00\x00\x09\x00\x00\x00\x00" +
          "\x00\x00\x00\x00\x00\x00\x00\x00" +
          "Not found").force_encoding("ASCII-8BIT")
        @pkt = Memcached::Response.parse_header(s[0..23])
        @pkt.parse_body(s[24..-1])
      end

      it "should return the right class according to magic & opcode" do
        @pkt[:magic].should == 0x81
        @pkt[:opcode].should == 0
        @pkt.class.should == Memcached::Response
      end
      it "should return the right data type" do
        @pkt[:data_type].should == 0
      end
      it "should return the right status" do
        @pkt[:status].should == Memcached::Errors::KEY_NOT_FOUND
      end
      it "should return the right opaque" do
        @pkt[:opaque].should == 0
      end
      it "should return the right CAS" do
        @pkt[:cas].should == 0
      end
      it "should parse the body correctly" do
        @pkt[:extras].should be_empty
        @pkt[:key].should == ""
        @pkt[:value].should == "Not found"
      end
    end

    context "example 4.2.1" do
      before :all do
        s =  ("\x81\x00\x00\x00" +
          "\x04\x00\x00\x00" +
          "\x00\x00\x00\x09" +
          "\x00\x00\x00\x00" +
          "\x00\x00\x00\x00" +
          "\x00\x00\x00\x01" +
          "\xde\xad\xbe\xef" +
          "World").force_encoding("ASCII-8BIT")
        @pkt = Memcached::Response.parse_header(s[0..23])
        @pkt.parse_body(s[24..-1])
      end

      it "should return the right class according to magic & opcode" do
        @pkt[:magic].should == 0x81
        @pkt[:opcode].should == 0
        @pkt.class.should == Memcached::Response
      end
      it "should return the right data type" do
        @pkt[:data_type].should == 0
      end
      it "should return the right status" do
        @pkt[:status].should == Memcached::Errors::NO_ERROR
      end
      it "should return the right opaque" do
        @pkt[:opaque].should == 0
      end
      it "should return the right CAS" do
        @pkt[:cas].should == 1
      end
      it "should parse the body correctly" do
        @pkt[:key].should == ""
        @pkt[:value].should == "World"
      end
    end

    describe :parse_body do
      it "should return succeeding bytes" do
        s = "\x81\x01\x00\x00" +
          "\x00\x00\x00\x00" +
          "\x00\x00\x00\x00" +
          "\x00\x00\x00\x00" +
          "\x00\x00\x00\x00" +
          "\x00\x00\x00\x01" +
          "chunky bacon"
        @pkt = Memcached::Response.parse_header(s[0..23])
        s = @pkt.parse_body(s[24..-1])
        @pkt[:status].should == 0
        @pkt[:total_body_length].should == 0
        @pkt[:value].should == ""
        s.should == "chunky bacon"
      end
    end
  end
end