Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 1 | # Copyright 2015, Google Inc. |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 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' |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 31 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 32 | include GRPC::Core::StatusCodes |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 33 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 34 | describe GRPC::Core::RpcErrors do |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 35 | before(:each) do |
| 36 | @known_types = { |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 37 | OK: 0, |
| 38 | ERROR: 1, |
| 39 | NOT_ON_SERVER: 2, |
| 40 | NOT_ON_CLIENT: 3, |
| 41 | ALREADY_ACCEPTED: 4, |
| 42 | ALREADY_INVOKED: 5, |
| 43 | NOT_INVOKED: 6, |
| 44 | ALREADY_FINISHED: 7, |
| 45 | TOO_MANY_OPERATIONS: 8, |
| 46 | INVALID_FLAGS: 9, |
| 47 | ErrorMessages: { |
| 48 | 0 => 'ok', |
| 49 | 1 => 'unknown error', |
| 50 | 2 => 'not available on a server', |
| 51 | 3 => 'not available on a client', |
| 52 | 4 => 'call is already accepted', |
| 53 | 5 => 'call is already invoked', |
| 54 | 6 => 'call is not yet invoked', |
| 55 | 7 => 'call is already finished', |
| 56 | 8 => 'outstanding read or write present', |
| 57 | 9 => 'a bad flag was given' |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | end |
| 61 | |
| 62 | it 'should have symbols for all the known error codes' do |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 63 | m = GRPC::Core::RpcErrors |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 64 | syms_and_codes = m.constants.collect { |c| [c, m.const_get(c)] } |
| 65 | expect(Hash[syms_and_codes]).to eq(@known_types) |
| 66 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 67 | end |
| 68 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 69 | describe GRPC::Core::Call do |
Tim Emiola | 564719d | 2015-03-27 13:07:34 -0700 | [diff] [blame^] | 70 | let (:client_queue) { GRPC::Core::CompletionQueue.new } |
| 71 | let (:test_tag) { Object.new } |
| 72 | let (:fake_host) { 'localhost:10101' } |
| 73 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 74 | before(:each) do |
Tim Emiola | d02d1d5 | 2015-01-26 18:36:17 -0800 | [diff] [blame] | 75 | @ch = GRPC::Core::Channel.new(fake_host, nil) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 76 | end |
| 77 | |
| 78 | describe '#start_read' do |
Tim Emiola | b24054a | 2015-02-09 12:34:16 -0800 | [diff] [blame] | 79 | xit 'should fail if called immediately' do |
Tim Emiola | 564719d | 2015-03-27 13:07:34 -0700 | [diff] [blame^] | 80 | blk = proc { make_test_call.start_read(test_tag) } |
temiola | 5832791 | 2014-12-15 17:51:16 -0800 | [diff] [blame] | 81 | expect(&blk).to raise_error GRPC::Core::CallError |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 82 | end |
| 83 | end |
| 84 | |
| 85 | describe '#start_write' do |
Tim Emiola | b24054a | 2015-02-09 12:34:16 -0800 | [diff] [blame] | 86 | xit 'should fail if called immediately' do |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 87 | bytes = GRPC::Core::ByteBuffer.new('test string') |
Tim Emiola | 564719d | 2015-03-27 13:07:34 -0700 | [diff] [blame^] | 88 | blk = proc { make_test_call.start_write(bytes, test_tag) } |
temiola | 5832791 | 2014-12-15 17:51:16 -0800 | [diff] [blame] | 89 | expect(&blk).to raise_error GRPC::Core::CallError |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 90 | end |
| 91 | end |
| 92 | |
| 93 | describe '#start_write_status' do |
Tim Emiola | b24054a | 2015-02-09 12:34:16 -0800 | [diff] [blame] | 94 | xit 'should fail if called immediately' do |
Tim Emiola | 564719d | 2015-03-27 13:07:34 -0700 | [diff] [blame^] | 95 | blk = proc { make_test_call.start_write_status(153, 'x', test_tag) } |
temiola | 5832791 | 2014-12-15 17:51:16 -0800 | [diff] [blame] | 96 | expect(&blk).to raise_error GRPC::Core::CallError |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 97 | end |
| 98 | end |
| 99 | |
| 100 | describe '#writes_done' do |
Tim Emiola | b24054a | 2015-02-09 12:34:16 -0800 | [diff] [blame] | 101 | xit 'should fail if called immediately' do |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 102 | blk = proc { make_test_call.writes_done(Object.new) } |
temiola | 5832791 | 2014-12-15 17:51:16 -0800 | [diff] [blame] | 103 | expect(&blk).to raise_error GRPC::Core::CallError |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 104 | end |
| 105 | end |
| 106 | |
| 107 | describe '#add_metadata' do |
| 108 | it 'adds metadata to a call without fail' do |
| 109 | call = make_test_call |
| 110 | n = 37 |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 111 | one_md = proc { |x| [sprintf('key%d', x), sprintf('value%d', x)] } |
| 112 | metadata = Hash[n.times.collect { |i| one_md.call i }] |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 113 | expect { call.add_metadata(metadata) }.to_not raise_error |
| 114 | end |
| 115 | end |
| 116 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 117 | describe '#status' do |
| 118 | it 'can save the status and read it back' do |
| 119 | call = make_test_call |
temiola | 5832791 | 2014-12-15 17:51:16 -0800 | [diff] [blame] | 120 | sts = Struct::Status.new(OK, 'OK') |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 121 | expect { call.status = sts }.not_to raise_error |
temiola | 5832791 | 2014-12-15 17:51:16 -0800 | [diff] [blame] | 122 | expect(call.status).to eq(sts) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 123 | end |
| 124 | |
| 125 | it 'must be set to a status' do |
| 126 | call = make_test_call |
| 127 | bad_sts = Object.new |
| 128 | expect { call.status = bad_sts }.to raise_error(TypeError) |
| 129 | end |
| 130 | |
| 131 | it 'can be set to nil' do |
| 132 | call = make_test_call |
| 133 | expect { call.status = nil }.not_to raise_error |
| 134 | end |
| 135 | end |
| 136 | |
| 137 | describe '#metadata' do |
| 138 | it 'can save the metadata hash and read it back' do |
| 139 | call = make_test_call |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 140 | md = { 'k1' => 'v1', 'k2' => 'v2' } |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 141 | expect { call.metadata = md }.not_to raise_error |
| 142 | expect(call.metadata).to be(md) |
| 143 | end |
| 144 | |
| 145 | it 'must be set with a hash' do |
| 146 | call = make_test_call |
| 147 | bad_md = Object.new |
| 148 | expect { call.metadata = bad_md }.to raise_error(TypeError) |
| 149 | end |
| 150 | |
| 151 | it 'can be set to nil' do |
| 152 | call = make_test_call |
| 153 | expect { call.metadata = nil }.not_to raise_error |
| 154 | end |
| 155 | end |
| 156 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 157 | def make_test_call |
Tim Emiola | 564719d | 2015-03-27 13:07:34 -0700 | [diff] [blame^] | 158 | @ch.create_call(client_queue, 'dummy_method', 'dummy_host', deadline) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 159 | end |
| 160 | |
| 161 | def deadline |
| 162 | Time.now + 2 # in 2 seconds; arbitrary |
| 163 | end |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 164 | end |