<commit_before>require 'spec_helper'

RSpec.describe Expeditor::Command do
  describe 'circuit break function' do
    context 'with circuit break' do
      it 'should reject execution' do
        service = Expeditor::Service.new(executor: Concurrent::ThreadPoolExecutor.new(max_queue: 0), threshold: 0.5, non_break_count: 100, sleep: 0, period: 10)
        commands = 100.times.map do
          Expeditor::Command.new(service: service) do
            raise RuntimeError
          end.set_fallback do |e|
            if e === Expeditor::CircuitBreakError
              1
            else
              0
            end
          end
        end
        commands.each(&:start)
        sum = commands.map(&:get).inject(:+)
        expect(sum).to eq(0)
        command = Expeditor::Command.new(service: service) do
          42
        end
        command.start
        expect { command.get }.to raise_error(Expeditor::CircuitBreakError)
        service.shutdown
      end

      it 'should not count circuit break' do
        service = Expeditor::Service.new(threshold: 0, non_break_count: 0)
        commands = 100.times.map do
          Expeditor::Command.new(service: service) do
            raise Expeditor::CircuitBreakError
          end
        end
        commands.map(&:start)
        commands.map(&:wait)
        command = Expeditor::Command.new(service: service) do
          42
        end
        command.start
        expect(command.get).to eq(42)
        service.shutdown
      end
    end

    context 'with circuit break and wait' do
      it 'should reject execution and back' do
        skip 'Need half-open state for circuit breaker'

        sleep_value = 0.03
        config = { threshold: 0.1, non_break_count: 5, sleep: sleep_value, period: 0.1 }
        service = Expeditor::Service.new(config)
        failure_commands = 10.times.map do
          Expeditor::Command.new(service: service) do
            raise RuntimeError
          end
        end
        failure_commands.each(&:start)
        failure_commands.each(&:wait)
        expect(service.open?).to eq(true)

        # Store break count to compare later.
        last_breaked_count = service.status.break

        success_commands = 5.times.map do
          Expeditor::Command.new(service: service) { 0 }<commit_msg>Faster and readable tests<commit_after>require 'spec_helper'

RSpec.describe Expeditor::Command do
  describe 'circuit break function' do
    context 'with circuit break' do
      it 'should reject execution' do
        executor = Concurrent::ThreadPoolExecutor.new(max_queue: 0)
        service = Expeditor::Service.new(executor: executor, threshold: 0.0, non_break_count: 2, sleep: 0, period: 10)

        3.times do
          Expeditor::Command.new(service: service) do
            raise RuntimeError
          end.start.wait
        end
        expect(service.open?).to eq(true)

        command = Expeditor::Command.new(service: service) { 42 }.start
        expect { command.get }.to raise_error(Expeditor::CircuitBreakError)

        service.shutdown
      end

      it 'should not count circuit break' do
        service = Expeditor::Service.new(threshold: 0, non_break_count: 0)
        5.times do
          Expeditor::Command.new(service: service) do
            raise Expeditor::CircuitBreakError
          end.start.wait
        end

        command = Expeditor::Command.new(service: service) { 42 }.start
        expect(command.get).to eq(42)

        service.shutdown
      end
    end

    context 'with circuit break and wait' do
      it 'should reject execution and back' do
        skip 'Need half-open state for circuit breaker'

        sleep_value = 0.03
        config = { threshold: 0.1, non_break_count: 5, sleep: sleep_value, period: 0.1 }
        service = Expeditor::Service.new(config)
        failure_commands = 10.times.map do
          Expeditor::Command.new(service: service) do
            raise RuntimeError
          end
        end
        failure_commands.each(&:start)
        failure_commands.each(&:wait)
        expect(service.open?).to eq(true)

        # Store break count to compare later.
        last_breaked_count = service.status.break

        success_commands = 5.times.map do
          Expeditor::Command.new(service: service) { 0 }
{'fraction_non_alphanumeric': 0.09033613445378151, 'fraction_numerical': 0.015639589169000934, 'mean_word_length': 2.2218045112781954, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2003239', 'n_tokens_mistral': 1274, 'n_tokens_neox': 1169, 'n_words': 320}