<commit_before>require 'rubygems'
require 'test/unit'
require 'qmf'
require 'timeout'
require 'thread'

$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))

require 'spqr/spqr'
require 'spqr/app'
require 'rhubarb/rhubarb'

module QmfTestHelpers
  DEBUG = false
  
  class AgentNotifyHandler < Qmf::ConsoleHandler
    def initialize
      @q = Queue.new
    end
    
    def queue
      @q
    end

    def agent_added(agent)
      # puts "GOT AN AGENT:  #{agent}"
      @q << agent
    end
  end

  def app_setup(*classes)
    unless $broker
      $notify_handler = AgentNotifyHandler.new
      $connection = Qmf::Connection.new(Qmf::ConnectionSettings.new)
      $console = Qmf::Console.new($notify_handler)
      $broker = $console.add_connection($connection)
    end
      
    @app = SPQR::App.new(:loglevel => DEBUG ? :debug : :fatal)
    @app.register *classes
    @child_pid = fork do 
      unless DEBUG
        # replace stdin/stdout/stderr
        $stdin.reopen("/dev/null", "r")
        $stdout.reopen("/dev/null", "w")
        $stderr.reopen("/dev/null", "w")
      end

      @app.main
    end
    
    $broker.wait_for_stable

    Timeout.timeout(3) do
      k = ""
      begin
        ag = $notify_handler.queue.pop
        k = ag.key
        # puts "GOT A KEY:  #{k}"
      end until k != "1.0"
    end


  end

  def teardown
    Process.kill(9, @child_pid) if @child_pid
  end
end

class Test::Unit::TestCase
end<commit_msg>debug messages for agent/console sync<commit_after>require 'rubygems'
require 'test/unit'
require 'qmf'
require 'timeout'
require 'thread'

$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))

require 'spqr/spqr'
require 'spqr/app'
require 'rhubarb/rhubarb'

module QmfTestHelpers
  DEBUG = false
  
  class AgentNotifyHandler < Qmf::ConsoleHandler
    def initialize
      @q = Queue.new
    end
    
    def queue
      @q
    end

    def agent_added(agent)
      puts "GOT AN AGENT:  #{agent}" if DEBUG
      @q << agent
    end
  end

  def app_setup(*classes)
    unless $broker
      $notify_handler = AgentNotifyHandler.new
      $connection = Qmf::Connection.new(Qmf::ConnectionSettings.new)
      $console = Qmf::Console.new($notify_handler)
      $broker = $console.add_connection($connection)
    end
      
    @app = SPQR::App.new(:loglevel => (DEBUG ? :debug : :fatal))
    @app.register *classes
    @child_pid = fork do 
      unless DEBUG
        # replace stdin/stdout/stderr
        $stdin.reopen("/dev/null", "r")
        $stdout.reopen("/dev/null", "w")
        $stderr.reopen("/dev/null", "w")
      end

      @app.main
    end
    
    $broker.wait_for_stable

    Timeout.timeout(3) do
      k = ""
      begin
        ag = $notify_handler.queue.pop
        k = ag.key
        puts "GOT A KEY:  #{k}" if DEBUG
      end until k != "1.0"
      puts "ESCAPING FROM TIMEOUT" if DEBUG
    end


  end

  def teardown
    Process.kill(9, @child_pid) if @child_pid
  end
end

class Test::Unit::TestCase
end
{'fraction_non_alphanumeric': 0.1261348897535668, 'fraction_numerical': 0.0025940337224383916, 'mean_word_length': 2.554147465437788, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '6223528', 'n_tokens_mistral': 1079, 'n_tokens_neox': 1059, 'n_words': 239}