File: mutex_stats_spec.rb

package info (click to toggle)
ruby-hitimes 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 372 kB
  • sloc: ruby: 1,385; makefile: 7
file content (30 lines) | stat: -rw-r--r-- 707 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# frozen_string_literal: true

require "spec_helper"

describe Hitimes::MutexedStats do
  before(:each) do
    @threads = 5
    @iters   = 10_000
    @final_value = @threads * @iters
  end

  def run_with_scissors(stats, threads, iters)
    spool = []
    threads.times do |_t|
      spool << Thread.new { iters.times { stats.update(1) } }
    end
    spool.each(&:join)
    stats
  end

  it "Hitimes::Stats is threadsafe" do
    stats = run_with_scissors(Hitimes::Stats.new, @threads, @iters)
    _(stats.count).must_equal @final_value
  end

  it "has a threadsafe update" do
    stats = run_with_scissors(Hitimes::MutexedStats.new, @threads, @iters)
    _(stats.count).must_equal @final_value
  end
end