Back to Repositories

Testing Monotonic Time Implementation in concurrent-ruby

This test suite validates the monotonic time functionality in the concurrent-ruby library, ensuring accurate and consistent time measurements across different units and formats. The tests verify both floating-point and integer time representations while maintaining strict type checking.

Test Coverage Overview

The test suite provides comprehensive coverage of the monotonic_time utility, examining various time unit conversions and type validations.

  • Tests float returns for second, millisecond, and microsecond units
  • Validates integer returns for second, millisecond, microsecond, and nanosecond units
  • Verifies error handling for invalid time units
  • Ensures type consistency across all supported time formats

Implementation Analysis

The testing approach employs RSpec’s context-based structure to organize related test cases systematically.

The implementation uses iterative testing patterns with shared examples for similar unit types, reducing code duplication while maintaining thorough coverage. RSpec’s expect syntax is used consistently for assertions and error checking.

Technical Details

Testing Infrastructure:
  • RSpec testing framework
  • Concurrent::Utility module integration
  • Type checking using be_a and be_an matchers
  • Error validation using raise_error matcher

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Ruby development.

  • DRY principle through enumerated test cases
  • Clear test organization using context blocks
  • Explicit error case handling
  • Consistent assertion style
  • Comprehensive type checking

ruby-concurrency/concurrent-ruby

spec/concurrent/monotonic_time_spec.rb

            
require 'concurrent/utility/monotonic_time'

module Concurrent

  RSpec.describe :monotonic_time do
    context 'behavior' do

      it 'returns seconds as float' do
        expect(Concurrent.monotonic_time).to be_a(Float)
      end

      [:float_second, :float_millisecond, :float_microsecond].each do |unit|
        it "returns a Float when unit = #{unit.inspect}" do
          expect(Concurrent.monotonic_time(unit)).to be_a(Float)
        end
      end

      [:second, :millisecond, :microsecond, :nanosecond].each do |unit|
        it "returns an Integer when unit = #{unit.inspect}" do
          expect(Concurrent.monotonic_time(unit)).to be_an(Integer)
        end
      end

      it 'raises ArgumentError on unknown units' do
        expect {
          Concurrent.monotonic_time(:foo)
        }.to raise_error(ArgumentError)
      end

    end
  end
end