<commit_before># encoding: utf-8

require "childprocess"

RACKUP_FILE = File.expand_path("../example_backend_server.ru", __FILE__)

BACKEND_CALLED_FILE = File.expand_path("../../tmp/backend_called.txt", __FILE__)
FileUtils.mkdir_p(File.dirname(BACKEND_CALLED_FILE))

process = ChildProcess.build("unicorn", "--listen", "127.0.0.1:9444", RACKUP_FILE)
process.io.inherit!
process.start
sleep 1 # Wait for unicorn to spin up

at_exit do
  process.stop
end

module ExampleBackendServer
  module Helpers
    def make_request(method, path, options = {})
      pre_request
      url = "http://127.0.0.1:9333#{path}"

      EM.run do
        ApiUmbrella::Gatekeeper::Server.run(:config => File.expand_path("../../config/gatekeeper.yml", __FILE__))

        EventMachine.add_timer(0.05) do
          http = EventMachine::HttpRequest.new(url, :connect_timeout => 1.5, :inactivity_timeout => 1.5).send(method, options)
          http.errback { @last_response = http.response; @last_header = http.response_header; EM.stop }
          http.callback { @last_response = http.response; @last_header = http.response_header; EM.stop }
        end
      end

      @backend_called = File.exists?(BACKEND_CALLED_FILE)
    end

    def make_multiple_requests(count, method, path, options = {})
      pre_request
      url = "http://127.0.0.1:9333#{path}"

      EM.run do
        ApiUmbrella::Gatekeeper::Server.run(:config => File.expand_path("../../config/gatekeeper.yml", __FILE__))

        EventMachine.add_timer(0.05) do
          http = nil
          EventMachine::Iterator.new(0...count, 5).map(
            proc { |index, iter|
              http = EventMachine::HttpRequest.new(url, :connect_timeout => 0.5, :inactivity_timeout => 0.5).send(method, options)
              http.errback { |h| iter.return(h) }
              http.callback { |h| iter.return(h) }
            },
            proc { |results|
               @last_response = results.last.response
               @last_header = results.last.response_header
               EM.stop
            }
          )
        end
      end

      @backend_called = File.exists?(BACKEND_CALLED_FILE)
    end

    def send_chunks(chunks, delay = 0.0)
      pre_request

      EM.run do
        ApiUmbrella::Gatekeeper::Server.run(:config => File.expand_path("../../config/gatekeeper.yml", __FILE__))

        EventMachine.add_timer(0.05) do
          SendHttpChunks.chunks = chunks
          SendHttpChunks.delay = delay
          conn = EventMachine.connect("127.0.0.1", 9333, SendHttpChunks)
          conn.errback { @last_response = conn.response; @last_headers_hash = conn.headers; EM.stop }
          conn.callback { @last_response = conn.response; @last_headers_hash = conn.headers; EM.stop }
        end
      end
    end

    private

    def pre_request
      FileUtils.rm_f(BACKEND_CALLED_FILE)
      @last_response = nil
      @last_header = nil
      @last_headers_hash = nil
    end
  end

  class SendHttpChunks < EventMachine::Connection
    include EventMachine::Deferrable

    mattr_accessor :chunks
    mattr_accessor :delay

    attr_reader :response
    attr_reader :headers
<commit_msg>Make gatekeeper yaml configurable for tests.<commit_after># encoding: utf-8

require "childprocess"

RACKUP_FILE = File.expand_path("../example_backend_server.ru", __FILE__)

BACKEND_CALLED_FILE = File.expand_path("../../tmp/backend_called.txt", __FILE__)
FileUtils.mkdir_p(File.dirname(BACKEND_CALLED_FILE))

process = ChildProcess.build("unicorn", "--listen", "127.0.0.1:9444", RACKUP_FILE)
process.io.inherit!
process.start
sleep 1 # Wait for unicorn to spin up

at_exit do
  process.stop
end

module ExampleBackendServer
  module Helpers
    def make_request(method, path, options = {})
      pre_request
      url = "http://127.0.0.1:9333#{path}"

      EM.run do
        config_name = @gatekeeper_config || "gatekeeper.yml"
        ApiUmbrella::Gatekeeper::Server.run(:config => File.expand_path("../../config/#{config_name}", __FILE__))

        EventMachine.add_timer(0.05) do
          http = EventMachine::HttpRequest.new(url, :connect_timeout => 1.5, :inactivity_timeout => 1.5).send(method, options)
          http.errback { @last_response = http.response; @last_header = http.response_header; EM.stop }
          http.callback { @last_response = http.response; @last_header = http.response_header; EM.stop }
        end
      end

      @backend_called = File.exists?(BACKEND_CALLED_FILE)
    end

    def make_multiple_requests(count, method, path, options = {})
      pre_request
      url = "http://127.0.0.1:9333#{path}"

      EM.run do
        config_name = @gatekeeper_config || "gatekeeper.yml"
        ApiUmbrella::Gatekeeper::Server.run(:config => File.expand_path("../../config/#{config_name}", __FILE__))

        EventMachine.add_timer(0.05) do
          http = nil
          EventMachine::Iterator.new(0...count, 5).map(
            proc { |index, iter|
              http = EventMachine::HttpRequest.new(url, :connect_timeout => 0.5, :inactivity_timeout => 0.5).send(method, options)
              http.errback { |h| iter.return(h) }
              http.callback { |h| iter.return(h) }
            },
            proc { |results|
               @last_response = results.last.response
               @last_header = results.last.response_header
               EM.stop
            }
          )
        end
      end

      @backend_called = File.exists?(BACKEND_CALLED_FILE)
    end

    def send_chunks(chunks, delay = 0.0)
      pre_request

      EM.run do
        config_name = @gatekeeper_config || "gatekeeper.yml"
        ApiUmbrella::Gatekeeper::Server.run(:config => File.expand_path("../../config/#{config_name}", __FILE__))

        EventMachine.add_timer(0.05) do
          SendHttpChunks.chunks = chunks
          SendHttpChunks.delay = delay
          conn = EventMachine.connect("127.0.0.1", 9333, SendHttpChunks)
          conn.errback { @last_response = conn.response; @last_headers_hash = conn.headers; EM.stop }
          conn.callback { @last_response = conn.response; @last_headers_hash = conn.headers; EM.stop }
        end
      end
    end

    private

    def pre_request
      FileUtils.rm_f(BACKEND_CALLED_FILE)
      @last_response = nil
      @last_header = nil
      @last_headers_hash = nil
    end
  end

  class SendHttpChunks < EventMachine::Connection
    include EventMachine::Deferrable

    mattr_accessor :chunks
    mattr_accessor :delay

    attr_reader :response
    attr_reader :headers

{'fraction_non_alphanumeric': 0.12096029547553093, 'fraction_numerical': 0.019390581717451522, 'mean_word_length': 2.6593468468468466, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, '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': '3817230', 'n_tokens_mistral': 2109, 'n_tokens_neox': 2025, 'n_words': 374}