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