Back to Repositories

Testing JavaThreadPoolExecutor Policy Mappings in concurrent-ruby

This test suite validates the JavaThreadPoolExecutor implementation in JRuby environments, focusing on thread pool behavior and overload policy mappings. It ensures proper initialization, shutdown, and policy handling for concurrent operations in the Ruby-Concurrency framework.

Test Coverage Overview

The test suite provides comprehensive coverage of the JavaThreadPoolExecutor functionality in JRuby environments.

  • Core thread pool behaviors and executor operations
  • Overload policy mappings (abort, discard, caller_runs)
  • Pool pruning behavior verification
  • Thread pool initialization and shutdown sequences

Implementation Analysis

The testing approach utilizes RSpec shared examples to validate common thread pool behaviors while adding JRuby-specific test cases.

  • Shared behavior verification through it_should_behave_like
  • Java class integration testing with explicit policy mapping checks
  • Mock expectations for Java class instantiation
  • Platform-specific conditional testing

Technical Details

  • RSpec testing framework with JRuby integration
  • Concurrent::JavaThreadPoolExecutor implementation
  • Java ThreadPoolExecutor policy classes
  • Custom thread pool configuration parameters
  • Conditional test execution based on Ruby engine

Best Practices Demonstrated

The test suite exemplifies robust concurrent testing practices with clear separation of concerns and thorough verification.

  • Platform-specific test isolation
  • Proper resource cleanup in after hooks
  • Comprehensive policy mapping verification
  • Shared example usage for common behaviors
  • Clear test organization and naming

ruby-concurrency/concurrent-ruby

spec/concurrent/executor/java_thread_pool_executor_spec.rb

            
require 'concurrent/utility/engine'

if Concurrent.on_jruby?
  require_relative 'thread_pool_executor_shared'

  module Concurrent

    RSpec.describe JavaThreadPoolExecutor, :type => :jruby do

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

      subject do
        JavaThreadPoolExecutor.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 :prune do
        it "is a no-op, pruning is handled by the JVM" do
          executor = JavaThreadPoolExecutor.new
          executor.prune_pool
        end
      end

      context '#overload_policy' do

        specify ':abort maps to AbortPolicy' do
          clazz = java.util.concurrent.ThreadPoolExecutor::AbortPolicy
          policy = clazz.new
          expect(clazz).to receive(:new).at_least(:once).with(any_args).and_return(policy)
          JavaThreadPoolExecutor.new(
            min_threads: 2,
            max_threads: 5,
            idletime: 60,
            max_queue: 10,
            fallback_policy: :abort
          )
        end

        specify ':discard maps to DiscardPolicy' do
          clazz = java.util.concurrent.ThreadPoolExecutor::DiscardPolicy
          policy = clazz.new
          expect(clazz).to receive(:new).at_least(:once).with(any_args).and_return(policy)
          JavaThreadPoolExecutor.new(
            min_threads: 2,
            max_threads: 5,
            idletime: 60,
            max_queue: 10,
            fallback_policy: :discard
          )
        end

        specify ':caller_runs maps to CallerRunsPolicy' do
          clazz = java.util.concurrent.ThreadPoolExecutor::CallerRunsPolicy
          policy = clazz.new
          expect(clazz).to receive(:new).at_least(:once).with(any_args).and_return(policy)
          JavaThreadPoolExecutor.new(
            min_threads: 2,
            max_threads: 5,
            idletime: 60,
            max_queue: 10,
            fallback_policy: :caller_runs
          )
        end
      end
    end
  end
end