File: test_value_marshaller.rb

package info (click to toggle)
ruby-dalli 3.2.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 684 kB
  • sloc: ruby: 6,552; sh: 20; makefile: 4
file content (229 lines) | stat: -rw-r--r-- 8,361 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# frozen_string_literal: true

require_relative '../helper'

describe Dalli::Protocol::ValueMarshaller do
  describe 'options' do
    subject { Dalli::Protocol::ValueMarshaller.new(options) }

    describe 'value_max_bytes' do
      describe 'by default' do
        let(:options) { {} }

        it 'sets value_max_bytes to 1MB by default' do
          assert_equal(1024 * 1024, subject.value_max_bytes)
        end
      end

      describe 'with a user specified value' do
        let(:value_max_bytes) { rand(4 * 1024 * 1024) + 1 }
        let(:options) { { value_max_bytes: value_max_bytes } }

        it 'sets value_max_bytes to the user specified value' do
          assert_equal subject.value_max_bytes, value_max_bytes
        end
      end
    end
  end

  describe 'store' do
    let(:marshaller) { Dalli::Protocol::ValueMarshaller.new(client_options) }
    let(:client_options) { {} }
    let(:val) { SecureRandom.hex(4096) }
    let(:serialized_value) { Marshal.dump(val) }
    let(:compressed_serialized_value) { Dalli::Compressor.compress(serialized_value) }
    let(:compressed_raw_value) { Dalli::Compressor.compress(val) }
    let(:key) { SecureRandom.hex(5) }

    describe 'when the bytesize is under value_max_bytes' do
      describe 'when the raw option is not specified' do
        let(:req_options) { {} }

        describe 'when the serialized value is above the minimum compression size' do
          let(:val) { SecureRandom.hex(4096) }

          it 'return the expected value and flags' do
            assert_equal [compressed_serialized_value, 0x3], marshaller.store(key, val, req_options)
          end
        end

        describe 'when the value is below the minimum compression size' do
          let(:val) { SecureRandom.hex(128) }

          it 'return the expected value and flags' do
            assert_equal [serialized_value, 0x1], marshaller.store(key, val, req_options)
          end
        end
      end

      describe 'when the raw option is specified' do
        let(:req_options) { { raw: true } }

        describe 'when the value is above the minimum compression size' do
          let(:val) { SecureRandom.hex(4096) }

          it 'return the expected value and flags' do
            assert_equal [compressed_raw_value, 0x2], marshaller.store(key, val, req_options)
          end
        end

        describe 'when the value is below the minimum compression size' do
          let(:val) { SecureRandom.hex(128) }

          it 'return the expected value and flags' do
            assert_equal [val, 0x0], marshaller.store(key, val, req_options)
          end
        end
      end
    end

    describe 'when the value_max_bytes is the default 1MB' do
      let(:client_options) { {} }

      describe 'when the raw option is not specified' do
        let(:req_options) { {} }

        describe 'when the compressed, serialized value is above the value_max_bytes size' do
          let(:val) { SecureRandom.hex(4 * 1024 * 1024) }

          it 'raises an error with the expected message' do
            exception = assert_raises Dalli::ValueOverMaxSize do
              marshaller.store(key, val, req_options)
            end

            assert_equal "Value for #{key} over max size: #{1024 * 1024} <= #{compressed_serialized_value.size}",
                         exception.message
          end
        end

        describe 'when the serialized value is below the value_max_bytes size' do
          let(:val) { SecureRandom.hex(4096) }

          it 'return the expected value and flags' do
            assert_equal [compressed_serialized_value, 0x3], marshaller.store(key, val, req_options)
          end
        end

        describe 'when the serialized value is below the value_max_bytes size and min compression size' do
          let(:val) { SecureRandom.hex(128) }

          it 'return the expected value and flags' do
            assert_equal [serialized_value, 0x1], marshaller.store(key, val, req_options)
          end
        end
      end

      describe 'when the raw option is specified' do
        let(:req_options) { { raw: true } }

        describe 'when the raw compressed value is above the value_max_bytes size' do
          let(:val) { SecureRandom.hex(4 * 1024 * 1024) }

          it 'raises an error with the expected message' do
            exception = assert_raises Dalli::ValueOverMaxSize do
              marshaller.store(key, val, req_options)
            end

            assert_equal "Value for #{key} over max size: #{1024 * 1024} <= #{compressed_raw_value.size}",
                         exception.message
          end
        end

        describe 'when the value is below the value_max_bytes size and above the minimum compression size' do
          let(:val) { SecureRandom.hex(4096) }

          it 'return the expected value and flags' do
            assert_equal [compressed_raw_value, 0x2], marshaller.store(key, val, req_options)
          end
        end

        describe 'when the raw value is below the value_max_bytes size and min compression size' do
          let(:val) { SecureRandom.hex(128) }

          it 'return the expected value and flags' do
            assert_equal [val, 0x0], marshaller.store(key, val, req_options)
          end
        end
      end
    end

    describe 'when the value_max_bytes is customized' do
      let(:value_max_bytes) { 512 }
      let(:client_options) { { value_max_bytes: value_max_bytes } }

      describe 'when the raw option is not specified' do
        let(:req_options) { {} }

        describe 'when the compressed, serialized value is above the value_max_bytes size' do
          let(:val) { SecureRandom.hex(4096) }

          it 'raises an error with the expected message' do
            exception = assert_raises Dalli::ValueOverMaxSize do
              marshaller.store(key, val, req_options)
            end

            assert_equal "Value for #{key} over max size: #{value_max_bytes} <= #{compressed_serialized_value.size}",
                         exception.message
          end
        end

        describe 'when the serialized value is below the value_max_bytes size and min compression size' do
          let(:val) { SecureRandom.hex(128) }

          it 'return the expected value and flags' do
            assert_equal [serialized_value, 0x1], marshaller.store(key, val, req_options)
          end
        end
      end

      describe 'when the raw option is specified' do
        let(:req_options) { { raw: true } }

        describe 'when the raw compressed value is above the value_max_bytes size' do
          let(:val) { SecureRandom.hex(4096) }

          it 'raises an error with the expected message' do
            exception = assert_raises Dalli::ValueOverMaxSize do
              marshaller.store(key, val, req_options)
            end

            assert_equal "Value for #{key} over max size: #{value_max_bytes} <= #{compressed_raw_value.size}",
                         exception.message
          end
        end

        describe 'when the raw value is below the value_max_bytes size and min compression size' do
          let(:val) { SecureRandom.hex(128) }

          it 'return the expected value and flags' do
            assert_equal [val, 0x0], marshaller.store(key, val, req_options)
          end
        end
      end
    end
  end

  describe 'retrieve' do
    let(:marshaller) { Dalli::Protocol::ValueMarshaller.new({}) }
    let(:val) { SecureRandom.hex(4096) }
    let(:serialized_value) { Marshal.dump(val) }
    let(:compressed_serialized_value) { Dalli::Compressor.compress(serialized_value) }
    let(:compressed_raw_value) { Dalli::Compressor.compress(val) }

    it 'retrieves the value when the flags indicate the value is both compressed and serialized' do
      assert_equal val, marshaller.retrieve(compressed_serialized_value, 0x3)
    end

    it 'retrieves the value when the flags indicate the value is just compressed' do
      assert_equal val, marshaller.retrieve(compressed_raw_value, 0x2)
    end

    it 'retrieves the value when the flags indicate the value is just serialized' do
      assert_equal val, marshaller.retrieve(serialized_value, 0x1)
    end

    it 'retrieves the value when the flags indicate the value is neither compressed nor serialized' do
      assert_equal val, marshaller.retrieve(val, 0x0)
    end
  end
end