forked from bigcommerce/gruf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
226 lines (207 loc) · 7.45 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# frozen_string_literal: true
# Copyright (c) 2017-present, BigCommerce Pty. Ltd. All rights reserved
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
require 'ffaker'
RSpec::Core::RakeTask.new(:spec)
task default: :spec
require 'gruf'
def gruf_rake_configure_rpc!
require 'pry'
$LOAD_PATH.unshift File.expand_path('spec/pb', __dir__)
root = File.dirname(__FILE__)
require File.realpath("#{root}/spec/support/grpc.rb")
require File.realpath("#{root}/spec/support/serializers/proto.rb")
require File.realpath("#{root}/spec/support/interceptors.rb")
Gruf.configure do |c|
c.error_serializer = Serializers::Proto
c.health_check_enabled = true
end
end
# Test enumerator
class ThingCreatorEnumerator
def initialize(num: 10)
@num = num.to_i
end
def each
return enum_for(:each) unless block_given?
@num.times do
sleep rand(0..1)
item = Rpc::Thing.new(id: rand(1..1000), name: FFaker::Lorem.word)
Gruf.logger.info "Sending: #{item.inspect}"
yield item
end
end
end
# demo server tests
namespace :gruf do
namespace :demo do
desc 'Get a single Thing to illustrate a unary gRPC call'
task :get_thing do
gruf_rake_configure_rpc!
begin
rpc_client = gruf_demo_build_client
op = rpc_client.call(:GetThing, id: rand(100_000))
Gruf.logger.info op.message.inspect
rescue Gruf::Client::Error => e
Gruf.logger.info e.error.to_h
end
end
desc 'Get a bunch of Things to illustrate a server streamer call'
task :get_things do
gruf_rake_configure_rpc!
begin
rpc_client = gruf_demo_build_client
resp = rpc_client.call(:GetThings)
resp.message.each do |thing|
Gruf.logger.info thing.inspect
end
rescue Gruf::Client::Error => e
Gruf.logger.error e.error.to_h
end
end
desc 'Create Things using a client streamer call'
task :create_things do
gruf_rake_configure_rpc!
begin
rpc_client = gruf_demo_build_client
enumerator = ThingCreatorEnumerator.new
resp = rpc_client.call(:CreateThings, enumerator.each)
Gruf.logger.info resp.message.inspect
rescue Gruf::Client::Error => e
Gruf.logger.error e.error.to_h
end
end
desc 'Create Things in a bidi stream'
task :create_things_in_stream do
gruf_rake_configure_rpc!
begin
rpc_client = gruf_demo_build_client
enumerator = ThingCreatorEnumerator.new
resp = rpc_client.call(:CreateThingsInStream, enumerator.each)
resp.message.each do |thing|
Gruf.logger.info "Received: #{thing.inspect}"
end
rescue Gruf::Client::Error => e
Gruf.logger.error e.error.to_h
end
end
desc 'Demonstrate a failure response'
task :get_fail do
gruf_rake_configure_rpc!
begin
rpc_client = gruf_demo_build_client
op = rpc_client.call(:GetFail, id: rand(100_000))
Gruf.logger.info op.message.inspect
rescue Gruf::Client::Errors::NotFound => e
Gruf.logger.error "Client got a not found error: #{e.error.to_h}"
rescue Gruf::Client::Errors::Validation => e
Gruf.logger.error "Client got a validation error: #{e.error.to_h}"
rescue Gruf::Client::Error => e
Gruf.logger.error "Unknown error: #{e.error.to_h}"
end
end
desc 'Demonstrate a field error in a failure response'
task :get_field_error_fail do
gruf_rake_configure_rpc!
begin
rpc_client = gruf_demo_build_client
op = rpc_client.call(:GetFieldErrorFail, id: rand(100_000))
Gruf.logger.info op.message.inspect
rescue Gruf::Client::Error => e
Gruf.logger.error e.error.to_h
end
end
desc 'Demonstrate an exception in a failure response'
task :get_exception do
gruf_rake_configure_rpc!
begin
rpc_client = gruf_demo_build_client
op = rpc_client.call(:GetException, id: rand(100_000))
Gruf.logger.info op.message.inspect
rescue Gruf::Client::Error => e
Gruf.logger.error e.error.to_h
end
end
desc 'Show an GRPC::Unauthorized response for a unary call'
task :get_unauthorized do
gruf_rake_configure_rpc!
begin
rpc_client = gruf_demo_build_client(options: { password: 'no' })
op = rpc_client.call(:GetThing, id: rand(100_000))
Gruf.logger.info op.message.inspect
rescue Gruf::Client::Error => e
Gruf.logger.error e.error.to_h
end
end
desc 'Perform a performance test against a unary endpoint'
task :performance, [:times, :sleep] do |_t, args|
args.with_defaults(
times: 3000,
sleep: ENV.fetch('SERVER_SLEEP_TIME', 1).to_i
)
gruf_rake_configure_rpc!
Gruf.logger.level = Logger::Severity::INFO
Gruf.logger.info "Running GetThing #{args[:times]} times"
threads = []
call_divisor = ENV.fetch('CLIENT_CALL_SLEEP_DIVISOR', 1000).to_i
args[:times].to_i.times do |idx|
threads << Thread.new do
sleep((idx / call_divisor).ceil)
rpc_client = gruf_demo_build_client
Gruf.logger.info "- #{idx}: Making call"
op = rpc_client.call(:GetThing, id: idx, sleep: args[:sleep].to_i)
Gruf.logger.info "-- #{idx}: #{op.message.inspect}"
rescue Gruf::Client::Error => e
Gruf.logger.error e.error.to_h
end
end
threads.each(&:join)
Gruf.logger.info 'Done.'
end
desc 'Call the health check'
task :health_check do
require 'gruf/controllers/health_controller'
gruf_rake_configure_rpc!
begin
rpc_client = gruf_demo_build_client(service: ::Grpc::Health::V1::Health)
op = rpc_client.call(:Check)
Gruf.logger.info op.message.inspect
rescue Gruf::Client::Error => e
Gruf.logger.info e.error.to_h
end
end
##
# @return [Gruf::Client]
#
def gruf_demo_build_client(service: nil, options: {}, client_options: {})
service ||= Rpc::ThingService
Gruf::Client.new(
service: service,
options: {
hostname: ENV.fetch('HOST', 'localhost:8001'),
username: ENV.fetch('USERNAME', 'grpc'),
password: ENV.fetch('PASSWORD', 'magic')
}.merge(options),
client_options: {
timeout: ENV.fetch('CLIENT_TIMEOUT', 30).to_i,
interceptors: [TestClientInterceptor.new]
}.merge(client_options)
)
end
end
end