From 869c33fc4402b6e4e3c1df73c83b118cbe08ab4f Mon Sep 17 00:00:00 2001 From: aryascripts Date: Thu, 11 Jul 2024 10:19:38 -0400 Subject: [PATCH] fix: added fix and tests for usage_reporter --- lib/graphql-hive/printer.rb | 1 - lib/graphql-hive/usage_reporter.rb | 4 +- .../graphql-hive/usage_reporter_spec.rb | 117 ++++++++++++++++++ 3 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 spec/graphql/graphql-hive/usage_reporter_spec.rb diff --git a/lib/graphql-hive/printer.rb b/lib/graphql-hive/printer.rb index 5d9065e..a96c016 100644 --- a/lib/graphql-hive/printer.rb +++ b/lib/graphql-hive/printer.rb @@ -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 diff --git a/lib/graphql-hive/usage_reporter.rb b/lib/graphql-hive/usage_reporter.rb index 395842d..a6b0b50 100644 --- a/lib/graphql-hive/usage_reporter.rb +++ b/lib/graphql-hive/usage_reporter.rb @@ -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 diff --git a/spec/graphql/graphql-hive/usage_reporter_spec.rb b/spec/graphql/graphql-hive/usage_reporter_spec.rb new file mode 100644 index 0000000..0a8c3e4 --- /dev/null +++ b/spec/graphql/graphql-hive/usage_reporter_spec.rb @@ -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