<commit_before>$: << File.dirname(__FILE__) + '/../lib' unless $:.include?(File.dirname(__FILE__) + '/../lib/')
require 'rubygems' if RUBY_VERSION < '1.9.0'
gem 'minitest'
require 'minitest/autorun'
require 'em-ventually'
require 'sensu/config'
require 'sensu/server'
require 'sensu/client'

class TestSensu < MiniTest::Unit::TestCase
  include EM::Ventually
  EM::Ventually.total_default = 0.5

  def setup
    @options = { :config_file => File.join(File.dirname(__FILE__), 'config.json') }
    config = Sensu::Config.new(@options)
    config.create_working_directory
    config.purge_working_directory
    @settings = config.settings
  end

  def test_read_config_file
    config = Sensu::Config.new(@options)
    settings = config.settings
    eventually(true) { settings.has_key?('client') }
  end

  def test_create_working_directory
    config = Sensu::Config.new(@options)
    config.create_working_directory
    eventually(true) { File.exists?('/tmp/sensu') }
  end

  def test_keep_alives
    server = Sensu::Server.new(@options)
    client = Sensu::Client.new(@options)
    server.setup_logging
    server.setup_redis
    server.setup_amqp
    server.setup_keep_alives
    client.setup_amqp
    client.setup_keep_alives
    test_client = ''
    EM.add_timer(1) do
      server.redis_connection.get('client:' + @settings['client']['name']).callback do |client_json|
        test_client = JSON.parse(client_json).reject { |key, value| key == 'timestamp' }
      end
    end
    eventually(@settings['client'], :total => 1.5) { test_client }
  end

  def test_handlers
    server = Sensu::Server.new(@options)
    server.setup_logging
    server.setup_handlers
    event = {
      'client' => @settings['client'],
      'check' => {
        'handler' => 'default'
      },
      'status' => 1,
      'output' => 'WARNING\n'
    }
    server.handle_event(event)
    eventually(true, :total => 1.5) do
      JSON.parse(File.open('/tmp/sensu/test_handlers', 'rb').read) == event
    end
  end

  def test_execute_checks
    server = Sensu::Server.new(@options)
    client = Sensu::Client.new(@options)
    server.setup_logging
    server.setup_redis
    server.setup_amqp
    server.setup_keep_alives
    server.setup_handlers
    server.setup_results
    client.setup_amqp
    client.setup_keep_alives
    @settings['checks'].each_key do |check_name|
      client.execute_check({'name' => check_name})
    end
    client_events = Hash.new
    EM.add_timer(1) do
      server.redis_connection.hgetall('events:' + @settings['client']['name']).callback do |events|
        client_events = Hash[*events]
        client_events.each do |key, value|
          client_events[key] = JSON.parse(value)
        end
      end
    end
    parallel do
      @settings['checks'].each_with_index do |(name, info), index|
        next if index == 0
        eventually({'status' => index, 'output' => @settings['client']['name'] + "\n"}, :total => 1.5) { client_events[name] }
      end
    end
  end
end
<commit_msg>[test-coverage] no ordered hash in ruby 1.8.x w/o activesupport<commit_after>$: << File.dirname(__FILE__) + '/../lib' unless $:.include?(File.dirname(__FILE__) + '/../lib/')
require 'rubygems' if RUBY_VERSION < '1.9.0'
gem 'minitest'
require 'minitest/autorun'
require 'em-ventually'
require 'sensu/config'
require 'sensu/server'
require 'sensu/client'

class TestSensu < MiniTest::Unit::TestCase
  include EM::Ventually
  EM::Ventually.total_default = 0.5

  def setup
    @options = { :config_file => File.join(File.dirname(__FILE__), 'config.json') }
    config = Sensu::Config.new(@options)
    config.create_working_directory
    config.purge_working_directory
    @settings = config.settings
  end

  def test_read_config_file
    config = Sensu::Config.new(@options)
    settings = config.settings
    eventually(true) { settings.has_key?('client') }
  end

  def test_create_working_directory
    config = Sensu::Config.new(@options)
    config.create_working_directory
    eventually(true) { File.exists?('/tmp/sensu') }
  end

  def test_keep_alives
    server = Sensu::Server.new(@options)
    client = Sensu::Client.new(@options)
    server.setup_logging
    server.setup_redis
    server.setup_amqp
    server.setup_keep_alives
    client.setup_amqp
    client.setup_keep_alives
    test_client = ''
    EM.add_timer(1) do
      server.redis_connection.get('client:' + @settings['client']['name']).callback do |client_json|
        test_client = JSON.parse(client_json).reject { |key, value| key == 'timestamp' }
      end
    end
    eventually(@settings['client'], :total => 1.5) { test_client }
  end

  def test_handlers
    server = Sensu::Server.new(@options)
    server.setup_logging
    server.setup_handlers
    event = {
      'client' => @settings['client'],
      'check' => {
        'handler' => 'default'
      },
      'status' => 1,
      'output' => 'WARNING\n'
    }
    server.handle_event(event)
    eventually(true, :total => 1.5) do
      JSON.parse(File.open('/tmp/sensu/test_handlers', 'rb').read) == event
    end
  end

  def test_execute_checks
    server = Sensu::Server.new(@options)
    client = Sensu::Client.new(@options)
    server.setup_logging
    server.setup_redis
    server.setup_amqp
    server.setup_keep_alives
    server.setup_handlers
    server.setup_results
    client.setup_amqp
    client.setup_keep_alives
    @settings['checks'].each_key do |check_name|
      client.execute_check({'name' => check_name})
    end
    client_events = Hash.new
    EM.add_timer(1) do
      server.redis_connection.hgetall('events:' + @settings['client']['name']).callback do |events|
        client_events = Hash[*events]
        client_events.each do |key, value|
          client_events[key] = JSON.parse(value)
        end
      end
    end
    parallel do
      %w[bar baz qux].each_with_index do |name, index|
        eventually({'status' => index + 1, 'output' => @settings['client']['name'] + "\n"}, :total => 1.5) { client_events[name] }
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.13714095965465714, 'fraction_numerical': 0.005312966959986717, 'mean_word_length': 3.3306973400431343, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 27, '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': '5920300', 'n_tokens_mistral': 2031, 'n_tokens_neox': 1999, 'n_words': 366}