Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing mode with RSpec matchers #25

Merged
merged 6 commits into from
Sep 25, 2021
Merged

Testing mode with RSpec matchers #25

merged 6 commits into from
Sep 25, 2021

Conversation

Envek
Copy link
Member

@Envek Envek commented Aug 17, 2021

Goal: provide tools to make testing that Yabeda metrics are being changed as expected to be convenient and allow users not to rely on Yabeda internals that are going to change

Usage with RSpec

Add the following to your rails_helper.rb (or spec_helper.rb):

require "yabeda/rspec"

Now you can use increment_yabeda_counter, update_yabeda_gauge, and measure_yabeda_histogram matchers:

it "increments counters" do
  expect { subject }.to increment_yabeda_counter(Yabeda.myapp.foo_count).by(3)
end

You can scope metrics by used tags with with_tags:

it "updates gauges" do
  expect { subject }.to \
    update_yabeda_gauge("some_gauge_name").
    with_tags(method: "command", command: "subscribe")
end

Note that tags you specified doesn't need to be exact, but can be a subset of tags used on metric update. In this example updates with following sets of tags { method: "command", command: "subscribe", status: "SUCCESS" } and { method: "command", command: "subscribe", status: "FAILURE" } will make test example to pass.

And check for values with by for counters, to for gauges, and with for gauges and histograms (and you can use other matchers here):

expect { subject }.to \
  measure_yabeda_histogram(Yabeda.something.anything_runtime).
  with(be_between(0.005, 0.05))

@Envek Envek self-assigned this Aug 17, 2021
@dsalahutdinov
Copy link
Member

Great job! Testing yabeda is now a little bit tricky and not standardized:

  • someone uses expect to receive(:increment)
  • someone uses a result value stored in the yabeda itself, like this one

Having rspec matchers simplifies testing. WDYT if we remove internal yabeda values in the same release?

@Envek Envek marked this pull request as ready for review August 18, 2021 08:22
Copy link

@palkan palkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Awesome!

module Yabeda
# Fake monitoring system adapter that collects latest metric values for later inspection
class TestAdapter < BaseAdapter
include Singleton
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we plan to add Mintiest support in the future, it's probably worth considering some kind of per-thread storage (for multithread tests).

@Envek Envek force-pushed the chore/test-helpers branch from 4e44256 to 138a471 Compare September 25, 2021 11:45
@Envek Envek merged commit a74531d into master Sep 25, 2021
@Envek Envek deleted the chore/test-helpers branch September 25, 2021 11:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants