Sha256: 8a0b8e5bf3d0d3583acb23465b87026fab2f30e6b0ff817e326899293ca3da12

Contents?: true

Size: 1.72 KB

Versions: 4

Compression:

Stored size: 1.72 KB

Contents

#--
#
# Author:: Tsutomu Katsube.
# Copyright:: Copyright (c) 2024 Tsutomu Katsube. All rights reserved.
# License:: Ruby license.

require_relative "sub-test-result"
require_relative "test-suite-runner"

module Test
  module Unit
    class TestSuiteThreadRunner < TestSuiteRunner
      @task_queue = Thread::Queue.new
      class << self
        def task_queue
          @task_queue
        end

        def run_all_tests
          n_workers = TestSuiteRunner.n_workers

          workers = []
          sub_exceptions = []
          n_workers.times do |i|
            workers << Thread.new(i) do |worker_id|
              begin
                loop do
                  task = @task_queue.pop
                  break if task.nil?
                  catch do |stop_tag|
                    task.call(stop_tag)
                  end
                end
              rescue Exception => exception
                sub_exceptions << exception
              end
            end
          end

          yield

          n_workers.times do
            @task_queue << nil
          end
          workers.each(&:join)
          sub_exceptions.each do |exception|
            raise exception
          end
        end
      end

      private
      def run_tests(result, &progress_block)
        @test_suite.tests.each do |test|
          if test.is_a?(TestSuite) or not @test_suite.parallel_safe?
            run_test(test, result, &progress_block)
          else
            task = lambda do |stop_tag|
              sub_result = SubTestResult.new(result)
              sub_result.stop_tag = stop_tag
              run_test(test, sub_result, &progress_block)
            end
            self.class.task_queue << task
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
es_cli-0.1.0 vendor/bundle/ruby/3.1.0/gems/test-unit-3.6.7/lib/test/unit/test-suite-thread-runner.rb
test-unit-3.6.7 lib/test/unit/test-suite-thread-runner.rb
test-unit-3.6.6 lib/test/unit/test-suite-thread-runner.rb
test-unit-3.6.5 lib/test/unit/test-suite-thread-runner.rb