From 979d3aa6a8ba2199a606637e0be87a98a849b686 Mon Sep 17 00:00:00 2001 From: Alexander Marychev Date: Tue, 15 Oct 2024 14:30:27 +0300 Subject: [PATCH 1/5] Expand functionality: limit group for adapter --- lib/yabeda/dsl/class_methods.rb | 17 +++- spec/yabeda/counter_spec.rb | 127 ++++++++++++++++++++++---- spec/yabeda/dsl/class_methods_spec.rb | 2 +- 3 files changed, 124 insertions(+), 22 deletions(-) diff --git a/lib/yabeda/dsl/class_methods.rb b/lib/yabeda/dsl/class_methods.rb index a79789e..7756d5f 100644 --- a/lib/yabeda/dsl/class_methods.rb +++ b/lib/yabeda/dsl/class_methods.rb @@ -96,10 +96,23 @@ def temporary_tags # # @param adapter_names [Array] Names of adapters to use def adapter(*adapter_names, group: @group) - raise ConfigurationError, "Adapter limitation can't be defined outside of group" unless group + raise ConfigurationError, "Adapter limitation can't be defined without adapter_names" if adapter_names.empty? + + @adapter_names = adapter_names + if group + include_group(group) + else + return yield if block_given? + + raise ConfigurationError, "Should be block passed with call .include_group for example" + end + end + + def include_group(group) + raise ConfigurationError, "Adapter limitation can't be defined without of group name" unless group Yabeda.groups[group] ||= Yabeda::Group.new(group) - Yabeda.groups[group].adapter(*adapter_names) + Yabeda.groups[group].adapter(*@adapter_names) end private diff --git a/spec/yabeda/counter_spec.rb b/spec/yabeda/counter_spec.rb index fdb4b02..baa8017 100644 --- a/spec/yabeda/counter_spec.rb +++ b/spec/yabeda/counter_spec.rb @@ -2,53 +2,142 @@ RSpec.describe Yabeda::Counter do subject(:increment_counter) { counter.increment(tags, by: metric_value) } - let(:tags) { { foo: "bar" } } let(:metric_value) { 10 } - let(:counter) { Yabeda.test_counter } - let(:built_tags) { { built_foo: "built_bar" } } let(:adapter) { instance_double(Yabeda::BaseAdapter, perform_counter_increment!: true, register!: true) } before do - Yabeda.configure do - counter :test_counter - end - Yabeda.configure! unless Yabeda.already_configured? - allow(Yabeda::Tags).to receive(:build).with(tags, anything).and_return(built_tags) - allow(Yabeda::Tags).to receive(:build).with({}, anything).and_return({}) Yabeda.register_adapter(:test_adapter, adapter) end - it { expect(increment_counter).to eq(metric_value) } + context "when config has no group" do + let(:counter) { Yabeda.test_counter } - it "increments counter with empty tags if tags are not provided" do - expect { counter.increment }.to change { counter.values[{}] }.by(1) - end + before do + Yabeda.configure do + counter :test_counter + end + Yabeda.configure! unless Yabeda.already_configured? + end + + it { expect(increment_counter).to eq(metric_value) } + + it "increments counter with empty tags if tags are not provided" do + expect { counter.increment }.to change { counter.values[{}] }.by(1) + end - it "execute perform_counter_increment! method of adapter" do - increment_counter - expect(adapter).to have_received(:perform_counter_increment!).with(counter, built_tags, metric_value) + it "execute perform_counter_increment!" do + increment_counter + expect(adapter).to have_received(:perform_counter_increment!).with(counter, tags, metric_value) + end end context "with adapter option" do - let(:counter) { Yabeda.counter_with_adapter } let(:another_adapter) { instance_double(Yabeda::BaseAdapter, perform_counter_increment!: true, register!: true) } + let(:counter) { Yabeda.counter_with_adapter } before do Yabeda.register_adapter(:another_adapter, another_adapter) + Yabeda.configure do counter :counter_with_adapter, adapter: :test_adapter end Yabeda.configure! unless Yabeda.already_configured? end - it "execute perform_counter_increment! method of adapter with name :test_adapter" do + it "execute perform_counter_increment! with name :test_adapter" do increment_counter aggregate_failures do - expect(adapter).to have_received(:perform_counter_increment!).with(counter, built_tags, metric_value) + expect(adapter).to have_received(:perform_counter_increment!).with(counter, tags, metric_value) expect(another_adapter).not_to have_received(:perform_counter_increment!) end end end + + context "with call .adapter method" do + let(:tags) { { type: "champignon" } } + let(:counter) { Yabeda.mushrooms.champignon_counter } + let(:basket_adapter) { instance_double(Yabeda::BaseAdapter, perform_counter_increment!: true, register!: true) } + + before do + Yabeda.register_adapter(:basket_adapter, basket_adapter) + end + + context "when call .adapter method in outside of group" do + before do + Yabeda.configure do + group :mushrooms do + counter :champignon_counter + end + + adapter :basket_adapter do + include_group :mushrooms + end + end + Yabeda.configure! unless Yabeda.already_configured? + end + + it "execute perform_counter_increment! with name :basket_adapter" do + increment_counter + + aggregate_failures do + expect(basket_adapter).to have_received(:perform_counter_increment!).with(counter, tags, metric_value) + expect(adapter).not_to have_received(:perform_counter_increment!) + end + end + end + + context "when call .adapter_names without adapter_names args" do + it "raises an error" do + expect do + Yabeda.configure do + group :mushrooms do + counter :champignon_counter + end + + adapter do + include_group :mushrooms + end + end + Yabeda.configure! unless Yabeda.already_configured? + end.to raise_error(Yabeda::ConfigurationError, "Adapter limitation can't be defined without adapter_names") + end + end + + context "when call adapter method in inside of group" do + before do + Yabeda.configure do + group :mushrooms do + adapter :basket_adapter + counter :champignon_counter + end + end + Yabeda.configure! unless Yabeda.already_configured? + end + + it "execute perform_counter_increment! with name :basket_adapter" do + increment_counter + + aggregate_failures do + expect(basket_adapter).to have_received(:perform_counter_increment!).with(counter, tags, metric_value) + expect(adapter).not_to have_received(:perform_counter_increment!) + end + end + end + + context "when call .adapter_names without adapter_names args" do + it "raises an error" do + expect do + Yabeda.configure do + group :mushrooms do + adapter + counter :champignon_counter + end + end + Yabeda.configure! unless Yabeda.already_configured? + end.to raise_error(Yabeda::ConfigurationError, "Adapter limitation can't be defined without adapter_names") + end + end + end end diff --git a/spec/yabeda/dsl/class_methods_spec.rb b/spec/yabeda/dsl/class_methods_spec.rb index 7b8159f..079a53a 100644 --- a/spec/yabeda/dsl/class_methods_spec.rb +++ b/spec/yabeda/dsl/class_methods_spec.rb @@ -192,7 +192,7 @@ expect do Yabeda.configure { adapter :test } Yabeda.configure! unless Yabeda.already_configured? - end.to raise_error(Yabeda::ConfigurationError, /can't be defined outside of group/) + end.to raise_error(Yabeda::ConfigurationError, "Should be block passed with call .include_group for example") end end From 9e588b59420d78ff548f3bec24d2b937d3692f67 Mon Sep 17 00:00:00 2001 From: Alexander Marychev Date: Tue, 7 Jan 2025 12:13:01 +0300 Subject: [PATCH 2/5] Make happy rubocop --- spec/yabeda/counter_spec.rb | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/spec/yabeda/counter_spec.rb b/spec/yabeda/counter_spec.rb index baa8017..bcdaa90 100644 --- a/spec/yabeda/counter_spec.rb +++ b/spec/yabeda/counter_spec.rb @@ -2,6 +2,7 @@ RSpec.describe Yabeda::Counter do subject(:increment_counter) { counter.increment(tags, by: metric_value) } + let(:tags) { { foo: "bar" } } let(:metric_value) { 10 } let(:adapter) { instance_double(Yabeda::BaseAdapter, perform_counter_increment!: true, register!: true) } @@ -16,7 +17,7 @@ before do Yabeda.configure do counter :test_counter - end + end Yabeda.configure! unless Yabeda.already_configured? end @@ -80,7 +81,7 @@ it "execute perform_counter_increment! with name :basket_adapter" do increment_counter - + aggregate_failures do expect(basket_adapter).to have_received(:perform_counter_increment!).with(counter, tags, metric_value) expect(adapter).not_to have_received(:perform_counter_increment!) @@ -89,15 +90,25 @@ end context "when call .adapter_names without adapter_names args" do - it "raises an error" do + it "raises an error using include_group construction" do expect do Yabeda.configure do group :mushrooms do counter :champignon_counter end - adapter do - include_group :mushrooms + adapter { include_group :mushrooms } + end + Yabeda.configure! unless Yabeda.already_configured? + end.to raise_error(Yabeda::ConfigurationError, "Adapter limitation can't be defined without adapter_names") + end + + it "raises an error using adapter construction into the group block" do + expect do + Yabeda.configure do + group :mushrooms do + adapter + counter :champignon_counter end end Yabeda.configure! unless Yabeda.already_configured? @@ -118,26 +129,12 @@ it "execute perform_counter_increment! with name :basket_adapter" do increment_counter - + aggregate_failures do expect(basket_adapter).to have_received(:perform_counter_increment!).with(counter, tags, metric_value) expect(adapter).not_to have_received(:perform_counter_increment!) end end end - - context "when call .adapter_names without adapter_names args" do - it "raises an error" do - expect do - Yabeda.configure do - group :mushrooms do - adapter - counter :champignon_counter - end - end - Yabeda.configure! unless Yabeda.already_configured? - end.to raise_error(Yabeda::ConfigurationError, "Adapter limitation can't be defined without adapter_names") - end - end end end From 88c3c3fa124f323a1a65be606f8fa5dcad76b729 Mon Sep 17 00:00:00 2001 From: Alexander Marychev Date: Tue, 7 Jan 2025 12:53:21 +0300 Subject: [PATCH 3/5] Fixed randomly fail tests --- spec/yabeda/counter_spec.rb | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/spec/yabeda/counter_spec.rb b/spec/yabeda/counter_spec.rb index bcdaa90..8971747 100644 --- a/spec/yabeda/counter_spec.rb +++ b/spec/yabeda/counter_spec.rb @@ -6,31 +6,25 @@ let(:tags) { { foo: "bar" } } let(:metric_value) { 10 } let(:adapter) { instance_double(Yabeda::BaseAdapter, perform_counter_increment!: true, register!: true) } + let(:counter) { Yabeda.test_counter } before do Yabeda.register_adapter(:test_adapter, adapter) - end - - context "when config has no group" do - let(:counter) { Yabeda.test_counter } - - before do - Yabeda.configure do - counter :test_counter - end - Yabeda.configure! unless Yabeda.already_configured? + Yabeda.configure do + counter :test_counter end + Yabeda.configure! unless Yabeda.already_configured? + end - it { expect(increment_counter).to eq(metric_value) } + it { expect(increment_counter).to eq(metric_value) } - it "increments counter with empty tags if tags are not provided" do - expect { counter.increment }.to change { counter.values[{}] }.by(1) - end + it "increments counter with empty tags if tags are not provided" do + expect { counter.increment }.to change { counter.values[{}] }.by(1) + end - it "execute perform_counter_increment!" do - increment_counter - expect(adapter).to have_received(:perform_counter_increment!).with(counter, tags, metric_value) - end + it "execute perform_counter_increment!" do + increment_counter + expect(adapter).to have_received(:perform_counter_increment!).with(counter, tags, metric_value) end context "with adapter option" do From 99dc00ec0757d166d65e8b62aad0e5b8e98b8ad2 Mon Sep 17 00:00:00 2001 From: Alexander Marychev Date: Tue, 7 Jan 2025 15:04:53 +0300 Subject: [PATCH 4/5] Remove random running specs; up tests --- spec/spec_helper.rb | 2 -- spec/yabeda/group_spec.rb | 7 +------ 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 377d86d..c2cf986 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -12,8 +12,6 @@ # Disable RSpec exposing methods globally on `Module` and `main` config.disable_monkey_patching! - config.order = :random - config.expect_with :rspec do |c| c.syntax = :expect end diff --git a/spec/yabeda/group_spec.rb b/spec/yabeda/group_spec.rb index b63ba97..4feda1d 100644 --- a/spec/yabeda/group_spec.rb +++ b/spec/yabeda/group_spec.rb @@ -2,14 +2,9 @@ RSpec.describe Yabeda::Group do let(:name) { nil } - let(:group) { described_class.new(name) } - before do - Yabeda.groups[name] = group - end - - after { Yabeda.reset! } + before { Yabeda.groups[name] = group } describe "default tags" do context "when on the top level group" do From 326c86df4a409527896980c8adc40d0eaa03679b Mon Sep 17 00:00:00 2001 From: Alexander Marychev Date: Wed, 8 Jan 2025 17:05:31 +0300 Subject: [PATCH 5/5] Revert random running specs; up tests --- spec/spec_helper.rb | 2 ++ spec/yabeda/counter_spec.rb | 52 ++++++++++++++++++------------------- spec/yabeda_spec.rb | 2 ++ 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c2cf986..377d86d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -12,6 +12,8 @@ # Disable RSpec exposing methods globally on `Module` and `main` config.disable_monkey_patching! + config.order = :random + config.expect_with :rspec do |c| c.syntax = :expect end diff --git a/spec/yabeda/counter_spec.rb b/spec/yabeda/counter_spec.rb index 8971747..1507fbc 100644 --- a/spec/yabeda/counter_spec.rb +++ b/spec/yabeda/counter_spec.rb @@ -59,6 +59,31 @@ Yabeda.register_adapter(:basket_adapter, basket_adapter) end + it "raises an error using include_group construction without adapter_names args" do + expect do + Yabeda.configure do + group :mushrooms do + counter :champignon_counter + end + + adapter { include_group :mushrooms } + end + Yabeda.configure! unless Yabeda.already_configured? + end.to raise_error(Yabeda::ConfigurationError, "Adapter limitation can't be defined without adapter_names") + end + + it "raises an error using adapter construction into the group block without adapter_names args" do + expect do + Yabeda.configure do + group :mushrooms do + adapter + counter :champignon_counter + end + end + Yabeda.configure! unless Yabeda.already_configured? + end.to raise_error(Yabeda::ConfigurationError, "Adapter limitation can't be defined without adapter_names") + end + context "when call .adapter method in outside of group" do before do Yabeda.configure do @@ -83,33 +108,6 @@ end end - context "when call .adapter_names without adapter_names args" do - it "raises an error using include_group construction" do - expect do - Yabeda.configure do - group :mushrooms do - counter :champignon_counter - end - - adapter { include_group :mushrooms } - end - Yabeda.configure! unless Yabeda.already_configured? - end.to raise_error(Yabeda::ConfigurationError, "Adapter limitation can't be defined without adapter_names") - end - - it "raises an error using adapter construction into the group block" do - expect do - Yabeda.configure do - group :mushrooms do - adapter - counter :champignon_counter - end - end - Yabeda.configure! unless Yabeda.already_configured? - end.to raise_error(Yabeda::ConfigurationError, "Adapter limitation can't be defined without adapter_names") - end - end - context "when call adapter method in inside of group" do before do Yabeda.configure do diff --git a/spec/yabeda_spec.rb b/spec/yabeda_spec.rb index 9464ab7..d83931a 100644 --- a/spec/yabeda_spec.rb +++ b/spec/yabeda_spec.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true RSpec.describe Yabeda do + before { described_class.reset! } + it "has a version number" do expect(Yabeda::VERSION).not_to be_nil end