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
|
# 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
# 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
}
# "enabled algorithms bitset" when all compression algorithms are enabled
ALL_ENABLED_BITSET = 0x7
# "enabled algorithms bitset" when all compression algorithms are disabled
ALL_DISABLED_BITSET = 0x0
# 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 everything has been disabled' do
options = GRPC::Core::CompressionOptions.new(
default_algorithm: 'identity',
default_level: 'none',
disabled_algorithms: [:gzip, :deflate]
)
expect(options.to_hash).to(
eql('grpc.default_compression_algorithm' => 0,
'grpc.default_compression_level' => 0,
'grpc.compression_enabled_algorithms_bitset' => 0x1)
)
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']
)
expected_bitset = ALL_ENABLED_BITSET & ~ALGORITHM_BITS[:deflate]
expect(options.to_hash).to(
eql('grpc.default_compression_algorithm' => ALGORITHMS[:gzip],
'grpc.default_compression_level' => COMPRESS_LEVELS[:low],
'grpc.compression_enabled_algorithms_bitset' => expected_bitset)
)
end
it 'gives correct channel args when no algorithms are disabled' do
options = GRPC::Core::CompressionOptions.new(
default_algorithm: 'identity',
default_level: 'high'
)
expect(options.to_hash).to(
eql('grpc.default_compression_algorithm' => ALGORITHMS[:identity],
'grpc.default_compression_level' => COMPRESS_LEVELS[:high],
'grpc.compression_enabled_algorithms_bitset' => ALL_ENABLED_BITSET)
)
end
# Raising an error in when attempting to set the default algorithm
# to something that is also requested to be disabled
it 'gives raises an error when the chosen default algorithm is disabled' do
blk = proc do
GRPC::Core::CompressionOptions.new(
default_algorithm: :gzip,
disabled_algorithms: [:gzip])
end
expect { blk.call }.to raise_error
end
end
# Test the private methods in the C extension that interact with
# the wrapped grpc_compression_options.
#
# Using #send to call private methods.
describe 'private internal methods' do
it 'mutating functions and accessors should be private' do
options = GRPC::Core::CompressionOptions.new
[:disable_algorithm_internal,
:disable_algorithms,
:set_default_algorithm,
:set_default_level,
:default_algorithm_internal_value,
:default_level_internal_value].each do |method_name|
expect(options.private_methods).to include(method_name)
end
end
describe '#disable_algorithms' do
ALGORITHMS.each_pair do |name, internal_value|
it "passes #{internal_value} to internal method for #{name}" do
options = GRPC::Core::CompressionOptions.new
expect(options).to receive(:disable_algorithm_internal)
.with(internal_value)
options.send(:disable_algorithms, name)
end
end
it 'should work with multiple parameters' do
options = GRPC::Core::CompressionOptions.new
ALGORITHMS.values do |internal_value|
expect(options).to receive(:disable_algorithm_internal)
.with(internal_value)
end
# disabled_algorithms is a private, variadic method
options.send(:disable_algorithms, *ALGORITHMS.keys)
end
end
describe '#new default values' do
it 'should start out with all algorithms enabled' do
options = GRPC::Core::CompressionOptions.new
bitset = options.send(:enabled_algorithms_bitset)
expect(bitset).to eql(ALL_ENABLED_BITSET)
end
it 'should start out with no default algorithm' do
options = GRPC::Core::CompressionOptions.new
expect(options.send(:default_algorithm_internal_value)).to be_nil
end
it 'should start out with no default level' do
options = GRPC::Core::CompressionOptions.new
expect(options.send(:default_level_internal_value)).to be_nil
end
end
describe '#enabled_algoritms_bitset' do
it 'should respond to disabling one algorithm' do
options = GRPC::Core::CompressionOptions.new
options.send(:disable_algorithms, :gzip)
current_bitset = options.send(:enabled_algorithms_bitset)
expect(current_bitset & ALGORITHM_BITS[:gzip]).to be_zero
end
it 'should respond to disabling multiple algorithms' do
options = GRPC::Core::CompressionOptions.new
# splitting up algorithms array since #disable_algorithms is variadic
options.send(:disable_algorithms, *ALGORITHMS.keys)
current_bitset = options.send(:enabled_algorithms_bitset)
expect(current_bitset).to eql(ALL_DISABLED_BITSET)
end
end
describe 'setting the default algorithm by name' do
it 'should set the internal value of the default algorithm' do
ALGORITHMS.each_pair do |name, expected_internal_value|
options = GRPC::Core::CompressionOptions.new
options.send(:set_default_algorithm, name)
internal_value = options.send(:default_algorithm_internal_value)
expect(internal_value).to eql(expected_internal_value)
end
end
it 'should fail with invalid algorithm names' do
[:none, :low, :huffman, :unkown, Object.new, 1].each do |name|
blk = proc do
options = GRPC::CoreCompressionOptions.new
options.send(:set_default_algorithm, name)
end
expect { blk.call }.to raise_error
end
end
end
describe 'setting the default level by name' do
it 'should set the internal value of the default compression value' do
COMPRESS_LEVELS.each_pair do |level, expected_internal_value|
options = GRPC::Core::CompressionOptions.new
options.send(:set_default_level, level)
internal_value = options.send(:default_level_internal_value)
expect(internal_value).to eql(expected_internal_value)
end
end
it 'should fail with invalid names' do
[:identity, :gzip, :unkown, :any, Object.new, 1].each do |name|
blk = proc do
GRPC::Core::CompressionOptions.new.send(:set_default_level, name)
end
expect { blk.call }.to raise_error
end
end
end
end
end
|