Sha256: 5b9fe6326669bca6662daec83724a17ddf4e26e2d7ec2fc5f8218c61d03788b5

Contents?: true

Size: 2 KB

Versions: 14

Compression:

Stored size: 2 KB

Contents

require 'flipper/adapters/instrumented'
require 'flipper/instrumentation/statsd'
require 'statsd'

RSpec.describe Flipper::Instrumentation::StatsdSubscriber do
  let(:statsd_client) { Statsd.new }
  let(:socket) { FakeUDPSocket.new }
  let(:adapter) do
    memory = Flipper::Adapters::Memory.new
    Flipper::Adapters::Instrumented.new(memory, instrumenter: ActiveSupport::Notifications)
  end
  let(:flipper) do
    Flipper.new(adapter, instrumenter: ActiveSupport::Notifications)
  end

  let(:user) { user = Flipper::Actor.new('1') }

  before do
    described_class.client = statsd_client
    Thread.current[:statsd_socket] = socket
  end

  after do
    described_class.client = nil
    Thread.current[:statsd_socket] = nil
  end

  def assert_timer(metric)
    regex = /#{Regexp.escape metric}\:\d+\|ms/
    result = socket.buffer.detect { |op| op.first =~ regex }
    expect(result).not_to be_nil
  end

  def assert_counter(metric)
    result = socket.buffer.detect { |op| op.first == "#{metric}:1|c" }
    expect(result).not_to be_nil
  end

  context 'for enabled feature' do
    it 'updates feature metrics when calls happen' do
      flipper[:stats].enable(user)
      assert_timer 'flipper.feature_operation.enable'

      flipper[:stats].enabled?(user)
      assert_timer 'flipper.feature_operation.enabled'
      assert_counter 'flipper.feature.stats.enabled'
    end
  end

  context 'for disabled feature' do
    it 'updates feature metrics when calls happen' do
      flipper[:stats].disable(user)
      assert_timer 'flipper.feature_operation.disable'

      flipper[:stats].enabled?(user)
      assert_timer 'flipper.feature_operation.enabled'
      assert_counter 'flipper.feature.stats.disabled'
    end
  end

  it 'updates adapter metrics when calls happen' do
    flipper[:stats].enable(user)
    assert_timer 'flipper.adapter.memory.enable'

    flipper[:stats].enabled?(user)
    assert_timer 'flipper.adapter.memory.get'

    flipper[:stats].disable(user)
    assert_timer 'flipper.adapter.memory.disable'
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
flipper-0.26.2 spec/flipper/instrumentation/statsd_subscriber_spec.rb
flipper-0.26.1 spec/flipper/instrumentation/statsd_subscriber_spec.rb
flipper-0.26.0 spec/flipper/instrumentation/statsd_subscriber_spec.rb
flipper-0.26.0.rc2 spec/flipper/instrumentation/statsd_subscriber_spec.rb
flipper-0.26.0.rc1 spec/flipper/instrumentation/statsd_subscriber_spec.rb
flipper-0.25.4 spec/flipper/instrumentation/statsd_subscriber_spec.rb
flipper-0.25.3 spec/flipper/instrumentation/statsd_subscriber_spec.rb
flipper-0.25.2 spec/flipper/instrumentation/statsd_subscriber_spec.rb
flipper-0.25.1 spec/flipper/instrumentation/statsd_subscriber_spec.rb
flipper-0.25.0 spec/flipper/instrumentation/statsd_subscriber_spec.rb
flipper-0.24.1 spec/flipper/instrumentation/statsd_subscriber_spec.rb
flipper-0.24.0 spec/flipper/instrumentation/statsd_subscriber_spec.rb
flipper-0.23.1 spec/flipper/instrumentation/statsd_subscriber_spec.rb
flipper-0.23.0 spec/flipper/instrumentation/statsd_subscriber_spec.rb