aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/ruby/spec/compression_options_spec.rb
blob: 7fa416ef095ba9ccdc97a7790c92595fdc6418eb (plain)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# Copyright 2015, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

require 'grpc'

describe GRPC::Core::CompressionOptions do
  # Note these constants should be updated according to what the core lib does.

  # Names of supported compression algorithms and their internal enum values
  ALGORITHMS = {
    identity: 0,
    deflate: 1,
    gzip: 2
  }

  # Compression algorithms and their corresponding bits in the internal
  # enabled algorithms bitset for GRPC core channel args.
  ALGORITHM_BITS = {
    identity: 0x1,
    deflate: 0x2,
    gzip: 0x4
  }

  # Names of valid supported compression levels and their internal enum values
  COMPRESS_LEVELS = {
    none: 0,
    low: 1,
    medium: 2,
    high: 3
  }

  it 'implements to_s' do
    expect { GRPC::Core::CompressionOptions.new.to_s }.to_not raise_error
  end

  it '#to_channel_arg_hash gives the same result as #to_hash' do
    options = GRPC::Core::CompressionOptions.new
    expect(options.to_channel_arg_hash).to eql(options.to_hash)
  end

  # Test the normal call sequence of creating an instance
  # and then obtaining the resulting channel-arg hash that
  # corresponds to the compression settings of the instance
  describe 'creating and converting to channel args hash' do
    it 'gives the correct channel args when nothing has been adjusted yet' do
      expect(GRPC::Core::CompressionOptions.new.to_hash).to(
        eql('grpc.compression_enabled_algorithms_bitset' => 0x7))
    end

    it 'gives the correct channel args after disabling multiple algorithms' do
      options = GRPC::Core::CompressionOptions.new(
        default_algorithm: :identity,
        default_level: :none,
        disabled_algorithms: [:gzip, :deflate]
      )

      channel_arg_hash = options.to_hash
      expect(channel_arg_hash['grpc.default_compression_algorithm']).to eq(0)
      expect(channel_arg_hash['grpc.default_compression_level']).to eq(0)

      bitset = channel_arg_hash['grpc.compression_enabled_algorithms_bitset']
      expect(bitset & ALGORITHM_BITS[:gzip]).to eq(0)
      expect(bitset & ALGORITHM_BITS[:deflate]).to eq(0)
    end

    it 'gives correct channel args with all args set' do
      options = GRPC::Core::CompressionOptions.new(
        default_algorithm: :gzip,
        default_level: :low,
        disabled_algorithms: [:deflate]
      )

      channel_arg_hash = options.to_hash

      actual_bitset = channel_arg_hash[
        'grpc.compression_enabled_algorithms_bitset']
      default_algorithm = channel_arg_hash['grpc.default_compression_algorithm']
      default_level = channel_arg_hash['grpc.default_compression_level']

      expect(actual_bitset & ALGORITHM_BITS[:deflate]).to eq(0)
      expect(default_algorithm).to eq(ALGORITHMS[:gzip])
      expect(default_level).to eq(COMPRESS_LEVELS[:low])
    end

    it 'gives correct channel args when no algorithms are disabled' do
      options = GRPC::Core::CompressionOptions.new(
        default_algorithm: :identity,
        default_level: :high
      )

      channel_arg_hash = options.to_hash

      actual_bitset = channel_arg_hash[
        'grpc.compression_enabled_algorithms_bitset']
      default_algorithm = channel_arg_hash['grpc.default_compression_algorithm']
      default_level = channel_arg_hash['grpc.default_compression_level']

      expect(actual_bitset & ALGORITHM_BITS[:deflate]).to_not eq(0)
      expect(actual_bitset & ALGORITHM_BITS[:gzip]).to_not eq(0)
      expect(default_algorithm).to eq(ALGORITHMS[:identity])
      expect(default_level).to eq(COMPRESS_LEVELS[:high])
    end
  end

  describe '#new with bad parameters' do
    it 'should fail with more than one parameter' do
      blk = proc { GRPC::Core::CompressionOptions.new(:gzip, :none) }
      expect { blk.call }.to raise_error
    end

    it 'should fail with a non-hash parameter' do
      blk = proc { GRPC::Core::CompressionOptions.new(:gzip) }
      expect { blk.call }.to raise_error
    end
  end

  describe '#level_name_to_value' do
    COMPRESS_LEVELS.each_pair do |name, internal_value|
      it "should return value #{internal_value} for level #{name}" do
        actual_value = GRPC::Core::CompressionOptions.level_name_to_value(name)
        expect(actual_value).to eq(internal_value)
      end
    end

    [:gzip, :deflate, :any, Object.new, 'none', 'low', 1].each do |name|
      it "should fail for parameter #{name} of class #{name.class}" do
        blk = proc do
          GRPC::Core::CompressionOptions.level_name_to_value(name)
        end
        expect { blk.call }.to raise_error
      end
    end
  end

  describe '#level_value_to_name' do
    COMPRESS_LEVELS.each_pair do |name, internal_value|
      it "should return level name #{name} for value #{internal_value}" do
        actual_name = GRPC::Core::CompressionOptions.level_value_to_name(
          internal_value)
        expect(actual_name).to eq(name)
      end
    end
    it 'should give the correct internal values from compression level names' do
      cls = GRPC::Core::CompressionOptions
      COMPRESS_LEVELS.each_pair do |name, internal_value|
        expect(cls.level_value_to_name(internal_value)).to eq(name)
      end
    end

    [:gzip, :any, Object.new, '1', :low].each do |name|
      it "should fail for parameter #{name} of class #{name.class}" do
        blk = proc do
          GRPC::Core::CompressionOptions.level_value_to_name(name)
        end
        expect { blk.call }.to raise_error
      end
    end
  end

  describe '#algorithm_name_to_value' do
    it 'should give the correct internal values from algorithm names' do
      cls = GRPC::Core::CompressionOptions
      ALGORITHMS.each_pair do |name, internal_value|
        expect(cls.algorithm_name_to_value(name)).to eq(internal_value)
      end
    end

    ['gzip', 'deflate', :any, Object.new, :none, :low, 1].each do |name|
      it "should fail for parameter #{name} of class #{name.class}" do
        blk = proc do
          GRPC::Core::CompressionOptions.algorithm_name_to_value(name)
        end
        expect { blk.call }.to raise_error
      end
    end
  end

  describe '#algorithm_value_to_name' do
    it 'should give the correct internal values from algorithm names' do
      cls = GRPC::Core::CompressionOptions
      ALGORITHMS.each_pair do |name, internal_value|
        expect(cls.algorithm_value_to_name(internal_value)).to eq(name)
      end
    end

    ['gzip', :deflate, :any, Object.new, :low, '1'].each do |value|
      it "should fail for parameter #{value} of class #{value.class}" do
        blk = proc do
          GRPC::Core::CompressionOptions.algorithm_value_to_name(value)
        end
        expect { blk.call }.to raise_error
      end
    end
  end

  describe '#default_algorithm and #default_algorithm_internal_value' do
    it 'can set the default algorithm and then read it back out' do
      ALGORITHMS.each_pair do |name, internal_value|
        options = GRPC::Core::CompressionOptions.new(default_algorithm: name)
        expect(options.default_algorithm).to eq(name)
        expect(options.default_algorithm_internal_value).to eq(internal_value)
      end
    end

    it 'returns nil if unset' do
      options = GRPC::Core::CompressionOptions.new
      expect(options.default_algorithm).to be_nil
      expect(options.default_algorithm_internal_value).to be_nil
    end
  end

  describe '#default_level and #default_level_internal_value' do
    it 'can set the default level and read it back out' do
      COMPRESS_LEVELS.each_pair do |name, internal_value|
        options = GRPC::Core::CompressionOptions.new(default_level: name)
        expect(options.default_level).to eq(name)
        expect(options.default_level_internal_value).to eq(internal_value)
      end
    end

    it 'returns nil if unset' do
      options = GRPC::Core::CompressionOptions.new
      expect(options.default_level).to be_nil
      expect(options.default_level_internal_value).to be_nil
    end
  end

  describe '#disabled_algorithms' do
    it 'can set the disabled algorithms and read them back out' do
      options = GRPC::Core::CompressionOptions.new(
        disabled_algorithms: [:gzip, :deflate])

      [:gzip, :deflate].each do |name|
        expect(options.disabled_algorithms.include?(name)).to eq(true)
      end
      expect(options.disabled_algorithms.size).to eq(2)
    end

    it 'returns an empty list if no algorithms were disabled' do
      options = GRPC::Core::CompressionOptions.new
      expect(options.disabled_algorithms).to eq([])
    end
  end

  describe '#is_algorithm_enabled' do
    it 'returns true if the algorithm is valid and not disabled' do
      options = GRPC::Core::CompressionOptions.new(disabled_algorithms: [:gzip])
      expect(options.is_algorithm_enabled(:deflate)).to eq(true)
    end

    it 'returns false if the algorithm is valid and disabled' do
      options = GRPC::Core::CompressionOptions.new(disabled_algorithms: [:gzip])
      expect(options.is_algorithm_enabled(:gzip)).to eq(false)
    end

    [:none, :any, 'gzip', Object.new, 1].each do |name|
      it "should fail for parameter ${name} of class #{name.class}" do
        options = GRPC::Core::CompressionOptions.new(
          disabled_algorithms: [:gzip])

        blk = proc do
          options.is_algorithm_enabled(name)
        end
        expect { blk.call }.to raise_error
      end
    end
  end

  describe '#enabled_algoritms_bitset' do
    it 'should respond to not disabling any algorithms' do
      options = GRPC::Core::CompressionOptions.new
      actual_bitset = options.enabled_algorithms_bitset
      expect(actual_bitset & ALGORITHM_BITS[:gzip]).to_not eq(0)
      expect(actual_bitset & ALGORITHM_BITS[:deflate]).to_not eq(0)
    end

    it 'should respond to disabling one algorithm' do
      options = GRPC::Core::CompressionOptions.new(
        disabled_algorithms: [:gzip])
      expect(options.enabled_algorithms_bitset & ALGORITHM_BITS[:gzip]).to eq(0)
    end

    it 'should respond to disabling multiple algorithms' do
      options = GRPC::Core::CompressionOptions.new(
        disabled_algorithms: [:gzip, :deflate])

      actual_bitset = options.enabled_algorithms_bitset
      expect(actual_bitset & ALGORITHM_BITS[:gzip]).to eq(0)
      expect(actual_bitset & ALGORITHM_BITS[:deflate]).to eq(0)
    end
  end
end