Back to Repositories

Testing Statistical Counter Operations in Resque

This test suite validates the functionality of Resque::Stat class, which handles statistical operations in the Resque job queue system. The tests ensure proper handling of statistical counters, including increment, decrement, and data store management.

Test Coverage Overview

The test suite provides comprehensive coverage of Resque’s statistical operations functionality.

  • Core statistical operations testing including get, increment, and decrement
  • Data store management and interaction verification
  • Deprecation warning validation for legacy methods
  • Counter manipulation using both standard and operator methods

Implementation Analysis

The testing approach utilizes RSpec’s describe/it blocks with a DummyStatStore implementation for isolated testing. The suite employs before/after hooks for test environment setup and cleanup, ensuring consistent test state.

  • Mock data store implementation for controlled testing
  • State management through setup/teardown hooks
  • Assertion-based verification of counter operations

Technical Details

  • Testing Framework: RSpec
  • Test Dependencies: test_helper
  • Mock Objects: DummyStatStore class
  • Assertion Methods: assert_equal, assert_output
  • Setup/Teardown: before/after blocks for state management

Best Practices Demonstrated

The test suite demonstrates several testing best practices for Ruby unit testing.

  • Isolation of tests using mock objects
  • Proper test setup and teardown
  • Comprehensive coverage of edge cases
  • Clear test naming and organization
  • Verification of deprecation warnings

resque/resque

test/stat_test.rb

            
require 'test_helper'

describe "Resque::Stat" do
  class DummyStatStore
    def initialize
      @stat = Hash.new(0)
    end

    def stat(stat)
      @stat[stat]
    end

    def increment_stat(stat, by = 1, redis: nil)
      @stat[stat] += by
    end

    def decrement_stat(stat, by)
      @stat[stat] -= by
    end

    def clear_stat(stat, redis: nil)
      @stat[stat] = 0
    end
  end

  before do
    @original_data_store = Resque::Stat.data_store
    @dummy_data_store = DummyStatStore.new
    Resque::Stat.data_store = @dummy_data_store
  end

  after do
    Resque::Stat.data_store = @original_data_store
  end

  it '#redis show deprecation warning' do
    assert_output(nil, /\[Resque\] \[Deprecation\]/ ) do
      assert_equal @dummy_data_store, Resque::Stat.redis
    end
  end

  it '#redis returns data_store' do
    assert_equal @dummy_data_store, Resque::Stat.data_store
  end

  it "#get" do
    assert_equal 0, Resque::Stat.get('hello')
  end

  it "#[]" do
    assert_equal 0, Resque::Stat['hello']
  end

  it "#incr" do
    assert_equal 2, Resque::Stat.incr('hello', 2)
    assert_equal 2, Resque::Stat['hello']
  end

  it "#<<" do
    assert_equal 1, Resque::Stat << 'hello'
    assert_equal 1, Resque::Stat['hello']
  end

  it "#decr" do
    assert_equal 2, Resque::Stat.incr('hello', 2)
    assert_equal 2, Resque::Stat['hello']

    assert_equal 0, Resque::Stat.decr('hello', 2)
    assert_equal 0, Resque::Stat['hello']
  end

  it "#>>" do
    assert_equal 2, Resque::Stat.incr('hello', 2)
    assert_equal 2, Resque::Stat['hello']

    assert_equal 1, Resque::Stat >> 'hello'
    assert_equal 1, Resque::Stat['hello']
  end

  it '#clear' do
    assert_equal 2, Resque::Stat.incr('hello', 2)
    assert_equal 2, Resque::Stat['hello']

    Resque::Stat.clear('hello')

    assert_equal 0, Resque::Stat['hello']
  end
end