<commit_before>require 'thwait'
require 'capybara'

require 'net/http'
require 'uri'
require 'find_a_port'

module Pact
  module Consumer

    class AppManager

      include Singleton

      attr_accessor :mock_port

      def initialize
        @spawned_app_pids = []
        @registered_apps = {}
        @max_wait = 10
        @apps_spawned = false
      end

      def register(app, port = FindAPort.available_port)
        @registered_apps ||= {}
        existing = @registered_apps[port]
        raise "Port #{port} is already being used by #{existing}" if existing and not existing == app
        @registered_apps[port] = app
        spawn(app, port) if @apps_spawned
        port
      end

      def kill_all
        @spawned_app_pids.each do |pid| 
          Process.kill(9, pid) 
          Process.wait(pid)
        end
        @apps_spawned = false
      end

      def clear_all
        kill_all
        @registered_apps = []
      end

      def spawn_all
        @registered_apps.each do |port, app|
          spawn(app, port)
        end
        @apps_spawned = true
      end

      def wait_until
        waited = 0
        wait_time = 0.1
        while waited < @max_wait do
          break if yield
          sleep wait_time
          waited += wait_time
          raise "Waited longer than #{@max_wait} seconds" if waited >= @max_wait
        end
      end

      def spawn app, port
        # following stolen from https://github.com/jwilger/kookaburra
        @spawned_app_pids ||= []
        @spawned_app_pids << fork do
          Capybara.server_port = port
          Capybara::Server.new(app).boot

          # This ensures that this forked process keeps running, because the
          # actual server is started in a thread by Capybara.
          ThreadsWait.all_waits(Thread.list)
        end

        wait_until do
          begin
            Net::HTTP.get_response(URI.parse("http://localhost:#{port}/index.html"))
          rescue Errno::ECONNREFUSED
            false
          end
        end
      end

    end
  end
end
<commit_msg>WIP - Latop is dying<commit_after>require 'thwait'
require 'capybara'

require 'net/http'
require 'uri'
require 'find_a_port'

module Pact
  module Consumer

    class AppManager

      include Singleton

      attr_accessor :mock_port

      def initialize
        @spawned_app_pids = []
        @registered_apps = {}
        @max_wait = 10
        @apps_spawned = false
      end

      def register(app, port = FindAPort.available_port)
        @registered_apps ||= {}
        existing = @registered_apps[port]
        raise "Port #{port} is already being used by #{existing}" if existing and not existing == app
        @registered_apps[port] = app
        spawn(app, port) if @apps_spawned
        port
      end

      def kill_all
        @spawned_app_pids.each do |pid| 
          Process.kill(9, pid) 
          Process.wait(pid)
        end
        @spawned_app_pids = []
        @apps_spawned = false
      end

      def clear_all
        kill_all
        @registered_apps = []
      end

      def spawn_all
        @registered_apps.each do |port, app|
          spawn(app, port)
        end
        @apps_spawned = true
      end

      def wait_until
        waited = 0
        wait_time = 0.1
        while waited < @max_wait do
          break if yield
          sleep wait_time
          waited += wait_time
          raise "Waited longer than #{@max_wait} seconds" if waited >= @max_wait
        end
      end

      def spawn app, port
        # following stolen from https://github.com/jwilger/kookaburra
        @spawned_app_pids ||= []
        @spawned_app_pids << fork do
          Capybara.server_port = port
          Capybara::Server.new(app).boot

          # This ensures that this forked process keeps running, because the
          # actual server is started in a thread by Capybara.
          ThreadsWait.all_waits(Thread.list)
        end

        wait_until do
          begin
            Net::HTTP.get_response(URI.parse("http://localhost:#{port}/index.html"))
          rescue Errno::ECONNREFUSED
            false
          end
        end
      end

    end
  end
end

{'fraction_non_alphanumeric': 0.07642549113560135, 'fraction_numerical': 0.0028749401054144704, 'mean_word_length': 1.7889111556446227, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '5503581', 'n_tokens_mistral': 1280, 'n_tokens_neox': 1235, 'n_words': 334}