• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15require 'spec_helper'
16
17include GRPC::Core::StatusCodes
18
19describe GRPC::Core::WriteFlags do
20  it 'should define the known write flag values' do
21    m = GRPC::Core::WriteFlags
22    expect(m.const_get(:BUFFER_HINT)).to_not be_nil
23    expect(m.const_get(:NO_COMPRESS)).to_not be_nil
24  end
25end
26
27describe GRPC::Core::RpcErrors do
28  before(:each) do
29    @known_types = {
30      OK: 0,
31      ERROR: 1,
32      NOT_ON_SERVER: 2,
33      NOT_ON_CLIENT: 3,
34      ALREADY_ACCEPTED: 4,
35      ALREADY_INVOKED: 5,
36      NOT_INVOKED: 6,
37      ALREADY_FINISHED: 7,
38      TOO_MANY_OPERATIONS: 8,
39      INVALID_FLAGS: 9,
40      ErrorMessages: {
41        0 => 'ok',
42        1 => 'unknown error',
43        2 => 'not available on a server',
44        3 => 'not available on a client',
45        4 => 'call is already accepted',
46        5 => 'call is already invoked',
47        6 => 'call is not yet invoked',
48        7 => 'call is already finished',
49        8 => 'outstanding read or write present',
50        9 => 'a bad flag was given'
51      }
52    }
53  end
54
55  it 'should have symbols for all the known error codes' do
56    m = GRPC::Core::RpcErrors
57    syms_and_codes = m.constants.collect { |c| [c, m.const_get(c)] }
58    expect(Hash[syms_and_codes]).to eq(@known_types)
59  end
60end
61
62describe GRPC::Core::CallOps do
63  before(:each) do
64    @known_types = {
65      SEND_INITIAL_METADATA: 0,
66      SEND_MESSAGE: 1,
67      SEND_CLOSE_FROM_CLIENT: 2,
68      SEND_STATUS_FROM_SERVER: 3,
69      RECV_INITIAL_METADATA: 4,
70      RECV_MESSAGE: 5,
71      RECV_STATUS_ON_CLIENT: 6,
72      RECV_CLOSE_ON_SERVER: 7
73    }
74  end
75
76  it 'should have symbols for all the known operation types' do
77    m = GRPC::Core::CallOps
78    syms_and_codes = m.constants.collect { |c| [c, m.const_get(c)] }
79    expect(Hash[syms_and_codes]).to eq(@known_types)
80  end
81end
82
83describe GRPC::Core::Call do
84  let(:test_tag)  { Object.new }
85  let(:fake_host) { 'localhost:10101' }
86
87  before(:each) do
88    @ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
89  end
90
91  describe '#status' do
92    it 'can save the status and read it back' do
93      make_test_call do |call|
94        sts = Struct::Status.new(OK, 'OK')
95        expect { call.status = sts }.not_to raise_error
96        expect(call.status).to eq(sts)
97      end
98    end
99
100    it 'must be set to a status' do
101      make_test_call do |call|
102        bad_sts = Object.new
103        expect { call.status = bad_sts }.to raise_error(TypeError)
104      end
105    end
106
107    it 'can be set to nil' do
108      make_test_call do |call|
109        expect { call.status = nil }.not_to raise_error
110      end
111    end
112  end
113
114  describe '#metadata' do
115    it 'can save the metadata hash and read it back' do
116      make_test_call do |call|
117        md = { 'k1' => 'v1',  'k2' => 'v2' }
118        expect { call.metadata = md }.not_to raise_error
119        expect(call.metadata).to be(md)
120      end
121    end
122
123    it 'must be set with a hash' do
124      make_test_call do |call|
125        bad_md = Object.new
126        expect { call.metadata = bad_md }.to raise_error(TypeError)
127      end
128    end
129
130    it 'can be set to nil' do
131      make_test_call do |call|
132        expect { call.metadata = nil }.not_to raise_error
133      end
134    end
135  end
136
137  describe '#set_credentials!' do
138    it 'can set a valid CallCredentials object' do
139      make_test_call do |call|
140        auth_proc = proc { { 'plugin_key' => 'plugin_value' } }
141        creds = GRPC::Core::CallCredentials.new auth_proc
142        expect { call.set_credentials! creds }.not_to raise_error
143      end
144    end
145  end
146
147  describe '#cancel' do
148    it 'completes ok' do
149      make_test_call do |call|
150        expect { call.cancel }.not_to raise_error
151      end
152    end
153
154    it 'completes ok when the call is closed' do
155      make_test_call do |call|
156        call.close
157        expect { call.cancel }.not_to raise_error
158      end
159    end
160  end
161
162  describe '#cancel_with_status' do
163    it 'completes ok' do
164      make_test_call do |call|
165        expect do
166          call.cancel_with_status(0, 'test status')
167        end.not_to raise_error
168        expect do
169          call.cancel_with_status(0, nil)
170        end.to raise_error(TypeError)
171      end
172    end
173
174    it 'completes ok when the call is closed' do
175      make_test_call do |call|
176        call.close
177        expect do
178          call.cancel_with_status(0, 'test status')
179        end.not_to raise_error
180      end
181    end
182  end
183
184  def make_test_call
185    call = @ch.create_call(nil, nil, 'phony_method', nil, deadline)
186    yield call
187    call.close
188  end
189
190  def deadline
191    Time.now + 2  # in 2 seconds; arbitrary
192  end
193end
194