File: ruby_thread_pool_executor_spec.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 (92 lines) | stat: -rw-r--r-- 2,531 bytes parent folder | download | duplicates (2)
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
require_relative 'thread_pool_executor_shared'

module Concurrent

  RSpec.describe RubyThreadPoolExecutor, :type=>:mrirbx do

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

    subject do
      RubyThreadPoolExecutor.new(
        min_threads: 2,
        max_threads: 5,
        idletime: 60,
        max_queue: 10,
        fallback_policy: :discard
      )
    end

    it_should_behave_like :thread_pool

    it_should_behave_like :thread_pool_executor

    context '#remaining_capacity' do

      let!(:expected_max){ 100 }
      let(:latch) { Concurrent::CountDownLatch.new }

      subject do
        RubyThreadPoolExecutor.new(
          min_threads: 10,
          max_threads: 20,
          idletime: 60,
          max_queue: expected_max,
          fallback_policy: :discard
        )
      end

      it 'returns :max_length when no tasks are enqueued' do
        5.times{ subject.post{ nil } }
        subject.post { latch.count_down }
        latch.wait(0.1)
        expect(subject.remaining_capacity).to eq expected_max
      end

      it 'returns the remaining capacity when tasks are enqueued' do
        block = Concurrent::CountDownLatch.new
        100.times{ subject.post{ block.wait } }
        subject.post { latch.count_down }
        latch.wait(0.1)
        expect(subject.remaining_capacity).to be < expected_max
        block.count_down
      end
    end

    if Concurrent.on_cruby? && Concurrent.ruby_version(:>=, 2, 3, 0)
      context 'threads naming' do
        subject do
          opts = { min_threads: 2 }
          opts[:name] = pool_name if pool_name
          described_class.new(opts)
        end

        let(:names) { Concurrent::Set.new }

        before do
          subject.post(names) { |names| names << Thread.current.name }
          subject.post(names) { |names| names << Thread.current.name }
          subject.shutdown
          subject.wait_for_termination(pool_termination_timeout)
          expect(names.size).to eq 2
        end

        context 'without pool name' do
          let(:pool_name) { }
          it 'sets counted name' do
            expect(names.all? { |name| name =~ /^worker-\d+$/ }).to be true
          end
        end

        context 'with pool name' do
          let(:pool_name) { 'MyExecutor' }
          it 'sets counted name' do
            expect(names.all? { |name| name =~ /^MyExecutor-worker-\d+$/ }).to be true
          end
        end
      end
    end
  end
end