|
235 | 235 | end |
236 | 236 | end |
237 | 237 | end |
| 238 | + |
| 239 | + describe 'validate_instrument_handler' do |
| 240 | + let(:expected_flush_timeout) { 30_000 } |
| 241 | + let(:expected_handler) { 'Handler.process' } |
| 242 | + let(:method_name) { :process } |
| 243 | + |
| 244 | + before do |
| 245 | + Handler = Class.new do |
| 246 | + extend OpenTelemetry::Instrumentation::AwsLambda::Wrap |
| 247 | + |
| 248 | + def self.process(event:, context:) |
| 249 | + { 'statusCode' => 200 } |
| 250 | + end |
| 251 | + end |
| 252 | + end |
| 253 | + |
| 254 | + after do |
| 255 | + Object.send(:remove_const, :Handler) |
| 256 | + end |
| 257 | + |
| 258 | + describe 'when handler method is defined' do |
| 259 | + describe 'when a flush_timeout is not provided' do |
| 260 | + before do |
| 261 | + Handler.instrument_handler(method_name) |
| 262 | + end |
| 263 | + |
| 264 | + it 'calls wrap_lambda with correct arguments' do |
| 265 | + args_checker = proc do |event:, context:, handler:, flush_timeout:| |
| 266 | + _(event).must_equal event_v1 |
| 267 | + _(context).must_equal context |
| 268 | + _(handler).must_equal expected_handler |
| 269 | + _(flush_timeout).must_equal expected_flush_timeout |
| 270 | + end |
| 271 | + |
| 272 | + Handler.stub(:wrap_lambda, args_checker) do |
| 273 | + Handler.process(event: event_v1, context: context) |
| 274 | + end |
| 275 | + end |
| 276 | + |
| 277 | + it 'calls the original method with correct arguments' do |
| 278 | + args_checker = proc do |event:, context:| |
| 279 | + _(event).must_equal event_v1 |
| 280 | + _(context).must_equal context |
| 281 | + end |
| 282 | + |
| 283 | + Handler.stub(:process_without_instrumentation, args_checker) do |
| 284 | + Handler.process(event: event_v1, context: context) |
| 285 | + end |
| 286 | + end |
| 287 | + end |
| 288 | + |
| 289 | + describe 'when a flush_timeout is provided' do |
| 290 | + let(:expected_flush_timeout) { 10_000 } |
| 291 | + |
| 292 | + before do |
| 293 | + Handler.instrument_handler(:process, flush_timeout: expected_flush_timeout) |
| 294 | + end |
| 295 | + |
| 296 | + it 'calls wrap_lambda with correct arguments' do |
| 297 | + args_checker = proc do |event:, context:, handler:, flush_timeout:| |
| 298 | + _(event).must_equal event_v1 |
| 299 | + _(context).must_equal context |
| 300 | + _(handler).must_equal expected_handler |
| 301 | + _(flush_timeout).must_equal expected_flush_timeout |
| 302 | + end |
| 303 | + |
| 304 | + Handler.stub(:wrap_lambda, args_checker) do |
| 305 | + Handler.process(event: event_v1, context: context) |
| 306 | + end |
| 307 | + end |
| 308 | + |
| 309 | + it 'calls the original method with correct arguments' do |
| 310 | + args_checker = proc do |event:, context:| |
| 311 | + _(event).must_equal event_v1 |
| 312 | + _(context).must_equal context |
| 313 | + end |
| 314 | + |
| 315 | + Handler.stub(:process_without_instrumentation, args_checker) do |
| 316 | + Handler.process(event: event_v1, context: context) |
| 317 | + end |
| 318 | + end |
| 319 | + end |
| 320 | + end |
| 321 | + |
| 322 | + describe 'when handler method is not defined' do |
| 323 | + let(:method_name) { :dummy } |
| 324 | + |
| 325 | + it 'raises ArgumentError' do |
| 326 | + assert_raises ArgumentError, "#{method_name} is not a method of Handler" do |
| 327 | + Handler.instrument_handler(method_name) |
| 328 | + end |
| 329 | + end |
| 330 | + end |
| 331 | + end |
238 | 332 | end |
0 commit comments