summaryrefslogtreecommitdiff
path: root/spec/ruby/core/thread/priority_spec.rb
blob: 40022da80d9f749131d298d3c12e48ad25a701cc (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)

describe "Thread#priority" do
  before :each do
    @current_priority = Thread.current.priority
    ThreadSpecs.clear_state
    @thread = Thread.new { Thread.pass until ThreadSpecs.state == :exit }
    Thread.pass until @thread.alive?
  end

  after :each do
    ThreadSpecs.state = :exit
    @thread.join
  end

  it "inherits the priority of the current thread while running" do
    @thread.alive?.should be_true
    @thread.priority.should == @current_priority
  end

  it "maintain the priority of the current thread after death" do
    ThreadSpecs.state = :exit
    @thread.join
    @thread.alive?.should be_false
    @thread.priority.should == @current_priority
  end

  it "returns an integer" do
    @thread.priority.should be_kind_of(Integer)
  end
end

describe "Thread#priority=" do
  before :each do
    ThreadSpecs.clear_state
    @thread = Thread.new { Thread.pass until ThreadSpecs.state == :exit }
    Thread.pass until @thread.alive?
  end

  after :each do
    ThreadSpecs.state = :exit
    @thread.join
  end

  describe "when set with an integer" do
    it "returns an integer" do
      value = (@thread.priority = 3)
      value.should == 3
    end

    it "clamps the priority to -3..3" do
      @thread.priority = 42
      @thread.priority.should == 3
      @thread.priority = -42
      @thread.priority.should == -3
    end
  end

  describe "when set with a non-integer" do
    it "raises a type error" do
      lambda{ @thread.priority = Object.new }.should raise_error(TypeError)
    end
  end

  it "sets priority even when the thread has died" do
    thread = Thread.new {}
    thread.join
    thread.priority = 3
    thread.priority.should == 3
  end
end