Sha256: d986774946aaec3e954da7a3ef68107f2fc18b6ecb7d1275d7324df84421415e

Contents?: true

Size: 1.88 KB

Versions: 3

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

describe DDMetrics::Stopwatch do
  subject(:stopwatch) { described_class.new }

  after { Timecop.return }

  it 'is zero by default' do
    expect(stopwatch.duration).to eq(0.0)
  end

  it 'records correct duration after start+stop' do
    Timecop.freeze(Time.local(2008, 9, 1, 10, 5, 0))
    stopwatch.start

    Timecop.freeze(Time.local(2008, 9, 1, 10, 5, 1))
    stopwatch.stop

    expect(stopwatch.duration).to eq(1.0)
  end

  it 'records correct duration after start+stop+start+stop' do
    Timecop.freeze(Time.local(2008, 9, 1, 10, 5, 0))
    stopwatch.start

    Timecop.freeze(Time.local(2008, 9, 1, 10, 5, 1))
    stopwatch.stop

    Timecop.freeze(Time.local(2008, 9, 1, 10, 5, 3))
    stopwatch.start

    Timecop.freeze(Time.local(2008, 9, 1, 10, 5, 6))
    stopwatch.stop

    expect(stopwatch.duration).to eq(1.0 + 3.0)
  end

  it 'errors when stopping when not started' do
    expect { stopwatch.stop }
      .to raise_error(
        DDMetrics::Stopwatch::NotRunningError,
        'Cannot stop, because stopwatch is not running',
      )
  end

  it 'errors when starting when already started' do
    stopwatch.start
    expect { stopwatch.start }
      .to raise_error(
        DDMetrics::Stopwatch::AlreadyRunningError,
        'Cannot start, because stopwatch is already running',
      )
  end

  it 'reports running status' do
    expect(stopwatch).not_to be_running
    expect(stopwatch).to be_stopped

    stopwatch.start

    expect(stopwatch).to be_running
    expect(stopwatch).not_to be_stopped

    stopwatch.stop

    expect(stopwatch).not_to be_running
    expect(stopwatch).to be_stopped
  end

  it 'errors when getting duration while running' do
    stopwatch.start
    expect { stopwatch.duration }
      .to raise_error(
        DDMetrics::Stopwatch::StillRunningError,
        'Cannot get duration, because stopwatch is still running',
      )
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ddmetrics-1.0.1 spec/ddmetrics/stopwatch_spec.rb
ddmetrics-1.0.0 spec/ddmetrics/stopwatch_spec.rb
ddmetrics-1.0.0rc1 spec/ddmetrics/stopwatch_spec.rb