File: priority_spec.rb

package info (click to toggle)
ruby2.5 2.5.5-3%2Bdeb10u4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 101,532 kB
  • sloc: ruby: 732,598; ansic: 669,262; xml: 25,363; yacc: 20,963; javascript: 6,680; sh: 3,610; lisp: 2,627; makefile: 596; python: 198; sed: 76; perl: 62; awk: 36; asm: 35
file content (68 lines) | stat: -rw-r--r-- 1,605 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
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
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)

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

  after 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 do
    ThreadSpecs.clear_state
    @thread = Thread.new {}
  end

  after do
    @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.join
    @thread.priority = 3
    @thread.priority.should == 3
  end
end