File: mutex_stats_spec.rb

package info (click to toggle)
ruby-hitimes 1.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 352 kB
  • sloc: ruby: 1,196; ansic: 418; java: 265; makefile: 15
file content (36 lines) | stat: -rw-r--r-- 951 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
31
32
33
34
35
36
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 { |t| t.join }
    return stats
  end

  if (not defined? RUBY_ENGINE) or (RUBY_ENGINE == "ruby") then
    it "Hitimes::Stats is threadsafe" do
      stats = run_with_scissors( ::Hitimes::Stats.new, @threads, @iters )
      stats.count.must_equal @final_value
    end
  else
    it "Hitimes::Stats is not threadsafe" do
      stats = run_with_scissors( ::Hitimes::Stats.new, @threads, @iters )
      stats.count.wont_equal @final_value
    end
  end

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

end