Skip to content

Commit

Permalink
fix: added fix and tests for usage_reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
aryascripts committed Jul 11, 2024
1 parent 7f2341b commit 869c33f
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 3 deletions.
1 change: 0 additions & 1 deletion lib/graphql-hive/printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class Hive < GraphQL::Tracing::PlatformTracing
# - removes aliases
# - sort nodes and directives (files, arguments, variables)
class Printer < GraphQL::Language::Printer

def print_string(str)
@out.append(str)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/graphql-hive/usage_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ def add_operation_to_report(report, operation)
analyzers: [analyzer]
)

visitor.visit
result = visitor.visit

fields.merge(analyzer.result)

operation += "\n" unless operation.empty?
operation += GraphQL::Hive::Printer.new.print(visitor.result)
operation += GraphQL::Hive::Printer.new.print(result)
end

md5 = Digest::MD5.new
Expand Down
117 changes: 117 additions & 0 deletions spec/graphql/graphql-hive/usage_reporter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe GraphQL::Hive::UsageReporter do
let(:subject) { described_class.instance }
let(:client) { instance_double('Hive::Client') }
let(:options) do
{
logger: logger,
buffer_size: 1
}
end
let(:logger) { instance_double('Logger') }

before do
allow(logger).to receive(:warn)
allow(logger).to receive(:debug)
end

describe '#initialize' do
it 'sets the instance' do
expect(described_class.instance).to eq(nil)
described_class.new(options, client)
expect(described_class.instance).to_not eq(nil)
end
end

describe '#add_operation' do
it 'adds an operation to the queue' do
operation = { operation: 'test' }
described_class.new(options, client)
subject.add_operation(operation)
expect(subject.instance_variable_get(:@queue).pop).to eq(operation)
end
end

describe '#on_exit' do
it 'closes the queue and joins the thread' do
described_class.new(options, client)
expect(subject.instance_variable_get(:@queue)).to receive(:close)
expect(subject.instance_variable_get(:@thread)).to receive(:join)
subject.on_exit
end
end

describe '#on_start' do
it 'starts the thread' do
described_class.new(options, client)
expect(subject).to receive(:start_thread)
subject.on_start
end
end

describe '#start_thread' do
it 'logs a warning if the thread is already alive' do
described_class.new(options, client)
subject.instance_variable_set(:@thread, Thread.new { p 'test' })
expect(logger).to receive(:warn)
subject.on_start
end
end

describe '#add_operation' do
let(:timestamp) { 1_720_705_946_333 }
let(:queries) { [] }
let(:results) { [] }
let(:duration) { 100_000 }
let(:operation) do
[timestamp, queries, results, duration]
end
let(:schema) do
GraphQL::Schema.from_definition('type Query { test: String }')
end
let(:query_string) { 'query TestingHive { test }' }

it 'adds an operation to the buffer' do
described_class.new(options, client)
subject.add_operation(operation)
expect(subject.instance_variable_get(:@queue).pop).to eq(operation)
end

context 'successful operaiton' do
let(:options) do
{ logger: logger, buffer_size: 2 }
end
let(:queries) do
[GraphQL::Query.new(schema, query_string, variables: {})]
end
let(:results) do
[GraphQL::Query::Result.new(query: queries.first, values: { 'data' => { 'test' => 'test' } })]
end

before do
allow(client).to receive(:send)
end

it 'processes the operations if the buffer is full' do
described_class.new(options, client)
subject.add_operation(operation)

# call process_operation with send
subject.send(:process_operations, [operation])
expect(client).to have_received(:send).with('/usage',
{ map: { '8b8412ce86f3ea7accb931b1a5de335d' =>
{ fields: ['Query', 'Query.test'],
operation: "query TestingHive {\n test\n}",
operationName: 'TestingHive' } },
operations: [{ execution: { duration: 100_000, errors: [], errorsTotal: 0, ok: true },
operationMapKey: '8b8412ce86f3ea7accb931b1a5de335d',
timestamp: 1_720_705_946_333 }],
size: 1 },
:usage)
end
end
end
end

0 comments on commit 869c33f

Please sign in to comment.