File: thread_pool_shared.rb

package info (click to toggle)
ruby-concurrent 1.1.6%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 30,284 kB
  • sloc: ruby: 30,875; java: 6,117; javascript: 1,114; ansic: 288; makefile: 10; sh: 6
file content (113 lines) | stat: -rw-r--r-- 3,157 bytes parent folder | download | duplicates (3)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
require_relative 'executor_service_shared'

RSpec.shared_examples :thread_pool do

  after(:each) do
    subject.shutdown
    expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
  end

  it_should_behave_like :executor_service

  let(:latch) { Concurrent::CountDownLatch.new }

  context '#auto_terminate?' do

    it 'returns true by default' do
      expect(subject.auto_terminate?).to be true
    end

    it 'returns true when :enable_at_exit_handler is true' do
      if described_class.to_s =~ /FixedThreadPool$/
        subject = described_class.new(1, auto_terminate: true)
      else
        subject = described_class.new(auto_terminate: true)
      end
      expect(subject.auto_terminate?).to be true

      subject.shutdown
      expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
    end

    it 'returns false when :enable_at_exit_handler is false' do
      if described_class.to_s =~ /FixedThreadPool$/
        subject = described_class.new(1, auto_terminate: false)
      else
        subject = described_class.new(auto_terminate: false)
      end
      expect(subject.auto_terminate?).to be false

      subject.shutdown
      expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
    end
  end

  context '#length' do

    it 'returns zero on creation' do
      expect(subject.length).to eq 0
    end

    it 'returns zero once shut down' do
      5.times{ subject.post{ sleep(0.1) } }
      subject.post { latch.count_down }
      latch.wait(0.1)
      subject.shutdown
      expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
      expect(subject.length).to eq 0
    end
  end

  context '#scheduled_task_count' do

    it 'returns zero on creation' do
      expect(subject.scheduled_task_count).to eq 0
    end

    it 'returns the approximate number of tasks that have been post thus far' do
      10.times{ subject.post{ nil } }
      subject.post { latch.count_down }
      latch.wait(0.1)
      expect(subject.scheduled_task_count).to be > 0
    end

    it 'returns the approximate number of tasks that were post' do
      10.times{ subject.post{ nil } }
      subject.post { latch.count_down }
      latch.wait(0.1)
      subject.shutdown
      expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
      expect(subject.scheduled_task_count).to be > 0
    end
  end

  context '#completed_task_count' do

    it 'returns zero on creation' do
      expect(subject.completed_task_count).to eq 0
    end

    unless Concurrent.on_jruby?

      it 'returns the approximate number of tasks that have been completed thus far' do
        5.times{ subject.post{ raise StandardError } }
        5.times{ subject.post{ nil } }
        subject.post { latch.count_down }
        latch.wait(1)
        expect(subject.completed_task_count).to be > 0
      end
    end
  end

  context '#shutdown' do

    it 'allows threads to exit normally' do
      10.times{ subject << proc{ nil } }
      expect(subject.length).to be > 0
      sleep(0.1)
      subject.shutdown
      sleep(1)
      expect(subject.length).to eq(0)
    end
  end
end