Home | History | Annotate | Download | only in spec
      1 # Copyright 2015 gRPC authors.
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
      4 # you may not use this file except in compliance with the License.
      5 # You may obtain a copy of the License at
      6 #
      7 #     http://www.apache.org/licenses/LICENSE-2.0
      8 #
      9 # Unless required by applicable law or agreed to in writing, software
     10 # distributed under the License is distributed on an "AS IS" BASIS,
     11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
     14 
     15 require 'spec_helper'
     16 
     17 describe GRPC::Core::CompressionOptions do
     18   # Note these constants should be updated
     19   # according to what the core lib provides.
     20 
     21   # Names of supported compression algorithms
     22   ALGORITHMS = [:identity, :deflate, :gzip]
     23 
     24   # Names of valid supported compression levels
     25   COMPRESS_LEVELS = [:none, :low, :medium, :high]
     26 
     27   it 'implements to_s' do
     28     expect { GRPC::Core::CompressionOptions.new.to_s }.to_not raise_error
     29   end
     30 
     31   it '#to_channel_arg_hash gives the same result as #to_hash' do
     32     options = GRPC::Core::CompressionOptions.new
     33     expect(options.to_channel_arg_hash).to eq(options.to_hash)
     34   end
     35 
     36   # Test the normal call sequence of creating an instance
     37   # and then obtaining the resulting channel-arg hash that
     38   # corresponds to the compression settings of the instance
     39   describe 'creating, reading, and converting to channel args hash' do
     40     it 'works when no optional args were provided' do
     41       options = GRPC::Core::CompressionOptions.new
     42 
     43       ALGORITHMS.each do |algorithm|
     44         expect(options.algorithm_enabled?(algorithm)).to be true
     45       end
     46 
     47       expect(options.disabled_algorithms).to be_empty
     48       expect(options.default_algorithm).to be nil
     49       expect(options.default_level).to be nil
     50       expect(options.to_hash).to be_instance_of(Hash)
     51     end
     52 
     53     it 'works when disabling multiple algorithms' do
     54       options = GRPC::Core::CompressionOptions.new(
     55         default_algorithm: :identity,
     56         default_level: :none,
     57         disabled_algorithms: [:gzip, :deflate]
     58       )
     59 
     60       [:gzip, :deflate].each do |algorithm|
     61         expect(options.algorithm_enabled?(algorithm)).to be false
     62         expect(options.disabled_algorithms.include?(algorithm)).to be true
     63       end
     64 
     65       expect(options.default_algorithm).to be(:identity)
     66       expect(options.default_level).to be(:none)
     67       expect(options.to_hash).to be_instance_of(Hash)
     68     end
     69 
     70     it 'works when all optional args have been set' do
     71       options = GRPC::Core::CompressionOptions.new(
     72         default_algorithm: :gzip,
     73         default_level: :low,
     74         disabled_algorithms: [:deflate]
     75       )
     76 
     77       expect(options.algorithm_enabled?(:deflate)).to be false
     78       expect(options.algorithm_enabled?(:gzip)).to be true
     79       expect(options.disabled_algorithms).to eq([:deflate])
     80 
     81       expect(options.default_algorithm).to be(:gzip)
     82       expect(options.default_level).to be(:low)
     83       expect(options.to_hash).to be_instance_of(Hash)
     84     end
     85 
     86     it 'doesnt fail when no algorithms are disabled' do
     87       options = GRPC::Core::CompressionOptions.new(
     88         default_algorithm: :identity,
     89         default_level: :high
     90       )
     91 
     92       ALGORITHMS.each do |algorithm|
     93         expect(options.algorithm_enabled?(algorithm)).to be(true)
     94       end
     95 
     96       expect(options.disabled_algorithms).to be_empty
     97       expect(options.default_algorithm).to be(:identity)
     98       expect(options.default_level).to be(:high)
     99       expect(options.to_hash).to be_instance_of(Hash)
    100     end
    101   end
    102 
    103   describe '#new with bad parameters' do
    104     it 'should fail with more than one parameter' do
    105       blk = proc { GRPC::Core::CompressionOptions.new(:gzip, :none) }
    106       expect { blk.call }.to raise_error
    107     end
    108 
    109     it 'should fail with a non-hash parameter' do
    110       blk = proc { GRPC::Core::CompressionOptions.new(:gzip) }
    111       expect { blk.call }.to raise_error
    112     end
    113   end
    114 
    115   describe '#default_algorithm' do
    116     it 'returns nil if unset' do
    117       options = GRPC::Core::CompressionOptions.new
    118       expect(options.default_algorithm).to be(nil)
    119     end
    120   end
    121 
    122   describe '#default_level' do
    123     it 'returns nil if unset' do
    124       options = GRPC::Core::CompressionOptions.new
    125       expect(options.default_level).to be(nil)
    126     end
    127   end
    128 
    129   describe '#disabled_algorithms' do
    130     it 'returns an empty list if no algorithms were disabled' do
    131       options = GRPC::Core::CompressionOptions.new
    132       expect(options.disabled_algorithms).to be_empty
    133     end
    134   end
    135 
    136   describe '#algorithm_enabled?' do
    137     [:none, :any, 'gzip', Object.new, 1].each do |name|
    138       it "should fail for parameter ${name} of class #{name.class}" do
    139         options = GRPC::Core::CompressionOptions.new(
    140           disabled_algorithms: [:gzip])
    141 
    142         blk = proc do
    143           options.algorithm_enabled?(name)
    144         end
    145         expect { blk.call }.to raise_error
    146       end
    147     end
    148   end
    149 end
    150