nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame^] | 1 | # Copyright 2014, Google Inc. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # Redistribution and use in source and binary forms, with or without |
| 5 | # modification, are permitted provided that the following conditions are |
| 6 | # met: |
| 7 | # |
| 8 | # * Redistributions of source code must retain the above copyright |
| 9 | # notice, this list of conditions and the following disclaimer. |
| 10 | # * Redistributions in binary form must reproduce the above |
| 11 | # copyright notice, this list of conditions and the following disclaimer |
| 12 | # in the documentation and/or other materials provided with the |
| 13 | # distribution. |
| 14 | # * Neither the name of Google Inc. nor the names of its |
| 15 | # contributors may be used to endorse or promote products derived from |
| 16 | # this software without specific prior written permission. |
| 17 | # |
| 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | require 'grpc' |
| 31 | require 'port_picker' |
| 32 | |
| 33 | module GRPC |
| 34 | |
| 35 | describe Channel do |
| 36 | |
| 37 | before(:each) do |
| 38 | @cq = CompletionQueue.new |
| 39 | end |
| 40 | |
| 41 | describe '#new' do |
| 42 | |
| 43 | it 'take a host name without channel args' do |
| 44 | expect { Channel.new('dummy_host', nil) }.not_to raise_error |
| 45 | end |
| 46 | |
| 47 | it 'does not take a hash with bad keys as channel args' do |
| 48 | blk = construct_with_args(Object.new => 1) |
| 49 | expect(&blk).to raise_error TypeError |
| 50 | blk = construct_with_args(1 => 1) |
| 51 | expect(&blk).to raise_error TypeError |
| 52 | end |
| 53 | |
| 54 | it 'does not take a hash with bad values as channel args' do |
| 55 | blk = construct_with_args(:symbol => Object.new) |
| 56 | expect(&blk).to raise_error TypeError |
| 57 | blk = construct_with_args('1' => Hash.new) |
| 58 | expect(&blk).to raise_error TypeError |
| 59 | end |
| 60 | |
| 61 | it 'can take a hash with a symbol key as channel args' do |
| 62 | blk = construct_with_args(:a_symbol => 1) |
| 63 | expect(&blk).to_not raise_error |
| 64 | end |
| 65 | |
| 66 | it 'can take a hash with a string key as channel args' do |
| 67 | blk = construct_with_args('a_symbol' => 1) |
| 68 | expect(&blk).to_not raise_error |
| 69 | end |
| 70 | |
| 71 | it 'can take a hash with a string value as channel args' do |
| 72 | blk = construct_with_args(:a_symbol => '1') |
| 73 | expect(&blk).to_not raise_error |
| 74 | end |
| 75 | |
| 76 | it 'can take a hash with a symbol value as channel args' do |
| 77 | blk = construct_with_args(:a_symbol => :another_symbol) |
| 78 | expect(&blk).to_not raise_error |
| 79 | end |
| 80 | |
| 81 | it 'can take a hash with a numeric value as channel args' do |
| 82 | blk = construct_with_args(:a_symbol => 1) |
| 83 | expect(&blk).to_not raise_error |
| 84 | end |
| 85 | |
| 86 | it 'can take a hash with many args as channel args' do |
| 87 | args = Hash[127.times.collect { |x| [x.to_s, x] } ] |
| 88 | blk = construct_with_args(args) |
| 89 | expect(&blk).to_not raise_error |
| 90 | end |
| 91 | |
| 92 | end |
| 93 | |
| 94 | describe '#create_call' do |
| 95 | it 'creates a call OK' do |
| 96 | port = find_unused_tcp_port |
| 97 | host = "localhost:#{port}" |
| 98 | ch = Channel.new(host, nil) |
| 99 | |
| 100 | deadline = Time.now + 5 |
| 101 | expect(ch.create_call('dummy_method', 'dummy_host', deadline)) |
| 102 | .not_to be(nil) |
| 103 | end |
| 104 | |
| 105 | it 'raises an error if called on a closed channel' do |
| 106 | port = find_unused_tcp_port |
| 107 | host = "localhost:#{port}" |
| 108 | ch = Channel.new(host, nil) |
| 109 | ch.close |
| 110 | |
| 111 | deadline = Time.now + 5 |
| 112 | blk = Proc.new do |
| 113 | ch.create_call('dummy_method', 'dummy_host', deadline) |
| 114 | end |
| 115 | expect(&blk).to raise_error(RuntimeError) |
| 116 | end |
| 117 | |
| 118 | end |
| 119 | |
| 120 | describe '#destroy' do |
| 121 | it 'destroys a channel ok' do |
| 122 | port = find_unused_tcp_port |
| 123 | host = "localhost:#{port}" |
| 124 | ch = Channel.new(host, nil) |
| 125 | blk = Proc.new { ch.destroy } |
| 126 | expect(&blk).to_not raise_error |
| 127 | end |
| 128 | |
| 129 | it 'can be called more than once without error' do |
| 130 | port = find_unused_tcp_port |
| 131 | host = "localhost:#{port}" |
| 132 | ch = Channel.new(host, nil) |
| 133 | blk = Proc.new { ch.destroy } |
| 134 | blk.call |
| 135 | expect(&blk).to_not raise_error |
| 136 | end |
| 137 | end |
| 138 | |
| 139 | describe '#close' do |
| 140 | it 'closes a channel ok' do |
| 141 | port = find_unused_tcp_port |
| 142 | host = "localhost:#{port}" |
| 143 | ch = Channel.new(host, nil) |
| 144 | blk = Proc.new { ch.close } |
| 145 | expect(&blk).to_not raise_error |
| 146 | end |
| 147 | |
| 148 | it 'can be called more than once without error' do |
| 149 | port = find_unused_tcp_port |
| 150 | host = "localhost:#{port}" |
| 151 | ch = Channel.new(host, nil) |
| 152 | blk = Proc.new { ch.close } |
| 153 | blk.call |
| 154 | expect(&blk).to_not raise_error |
| 155 | end |
| 156 | end |
| 157 | |
| 158 | def construct_with_args(a) |
| 159 | Proc.new {Channel.new('dummy_host', a)} |
| 160 | end |
| 161 | |
| 162 | end |
| 163 | |
| 164 | end |