Skip to content

Commit

Permalink
a better approach towards client testing
Browse files Browse the repository at this point in the history
  • Loading branch information
michal-kazmierczak committed Dec 17, 2024
1 parent 65ec89e commit 371d748
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

require_relative '../../../../test_helper'
require_relative '../../../../support/grpc_server_runner'

describe OpenTelemetry::Instrumentation::Grpc::Interceptors::ClientTracer do
let(:instrumentation) { OpenTelemetry::Instrumentation::Grpc::Instrumentation.instance }
let(:exporter) { EXPORTER }
let(:request) { Support::Proto::PingRequest.new(value: 'Ping!') }
let(:server_runner) { Support::GrpcServerRunner.new }

before do
instrumentation.install
exporter.reset

# use a real gRPC server to avoid non-trivial mocks
server_port = server_runner.start
# create a client stub to interact with the server
@stub = Support::Proto::PingServer::Stub.new(
"localhost:#{server_port}",
:this_channel_is_insecure
)
end
after do
server_runner.stop
end

describe '#request_response' do
it 'registers a span' do
_(exporter.finished_spans.size).must_equal 0

@stub.request_response_ping(request)

_(exporter.finished_spans.size).must_equal 1
end

it 'sets expected kind' do
@stub.request_response_ping(request)

span = exporter.finished_spans.first

_(span).wont_be_nil
_(span.kind).must_equal(:client)
end

it 'sets expected name' do
@stub.request_response_ping(request)

span = exporter.finished_spans.first

_(span).wont_be_nil
_(span.name).must_equal('support.proto.PingServer/RequestResponsePing')
end

it 'sets expected attributes' do
@stub.request_response_ping(request)

span = exporter.finished_spans.first

_(span).wont_be_nil
_(span.attributes['rpc.system']).must_equal('grpc')
_(span.attributes['rpc.service']).must_equal('support.proto.PingServer')
_(span.attributes['rpc.method']).must_equal('RequestResponsePing')
_(span.attributes['rpc.type']).must_equal('request_response')
_(span.attributes['net.sock.peer.addr']).wont_be_empty
_(span.attributes['rpc.grpc.status_code']).must_equal(0)
end
end
end

This file was deleted.

0 comments on commit 371d748

Please sign in to comment.