<commit_before>require File.dirname(__FILE__) + '/../lib/sensu/client.rb'
require File.dirname(__FILE__) + '/helpers.rb'

describe "Sensu::Client" do
  include Helpers

  before do
    @client = Sensu::Client.new(options)
  end

  it "can connect to rabbitmq" do
    async_wrapper do
      @client.setup_rabbitmq
      async_done
    end
  end

  it "can send a keepalive" do
    async_wrapper do
      @client.setup_rabbitmq
      @client.publish_keepalive
      amq.queue('keepalives').subscribe do |headers, payload|
        keepalive = JSON.parse(payload, :symbolize_names => true)
        keepalive[:name].should eq('i-424242')
        async_done
      end
    end
  end

  it "can schedule keepalive publishing" do
    async_wrapper do
      @client.setup_rabbitmq
      @client.setup_keepalives
      amq.queue('keepalives').subscribe do |headers, payload|
        keepalive = JSON.parse(payload, :symbolize_names => true)
        keepalive[:name].should eq('i-424242')
        async_done
      end
    end
  end

  it "can send a check result" do
    async_wrapper do
      @client.setup_rabbitmq
      check = result_template[:check]
      @client.publish_result(check)
      result_queue = amq.queue('results')
      result_queue.subscribe do |headers, payload|
        result = JSON.parse(payload, :symbolize_names => true)
        result[:client].should eq('i-424242')
        result[:check][:name].should eq('foobar')
        async_done
      end
    end
  end

  it "can execute a check" do
    async_wrapper do
      @client.setup_rabbitmq
      check = {
        :name => 'foo',
        :command => 'echo -n foobar',
        :issued => epoch
      }
      @client.execute_check(check)
      amq.queue('results').subscribe do |headers, payload|
        result = JSON.parse(payload, :symbolize_names => true)
        result[:client].should eq('i-424242')
        result[:check][:output].should eq('foobar')
        async_done
      end
    end
  end

  it "can setup subscriptions" do
    async_wrapper do
      @client.setup_rabbitmq
      @client.setup_subscriptions
      amq.fanout('test', :passive => true) do |exchange, declare_ok|
        declare_ok.should be_an_instance_of(AMQ::Protocol::Exchange::DeclareOk)
        exchange.status.should eq(:opening)
        async_done
      end
    end
  end

  it "can receive a check request and execute the check (with tokens)" do
    async_wrapper do
      @client.setup_rabbitmq
      @client.setup_subscriptions
      timer(1) do
        check_request = {
          :name => 'tokens',
          :issued => epoch
        }
        amq.queue('results').subscribe do |headers, payload|
          result = JSON.parse(payload, :symbolize_names => true)
          result[:client].should eq('i-424242')
          result[:check][:output].should eq('i-424242 true')
          result[:check][:status].should eq(2)
          async_done
        end
        amq.fanout('test').publish(check_request.to_json)<commit_msg>[rspec] move subscribe outside of timer, 4th times the charm<commit_after>require File.dirname(__FILE__) + '/../lib/sensu/client.rb'
require File.dirname(__FILE__) + '/helpers.rb'

describe "Sensu::Client" do
  include Helpers

  before do
    @client = Sensu::Client.new(options)
  end

  it "can connect to rabbitmq" do
    async_wrapper do
      @client.setup_rabbitmq
      async_done
    end
  end

  it "can send a keepalive" do
    async_wrapper do
      @client.setup_rabbitmq
      @client.publish_keepalive
      amq.queue('keepalives').subscribe do |headers, payload|
        keepalive = JSON.parse(payload, :symbolize_names => true)
        keepalive[:name].should eq('i-424242')
        async_done
      end
    end
  end

  it "can schedule keepalive publishing" do
    async_wrapper do
      @client.setup_rabbitmq
      @client.setup_keepalives
      amq.queue('keepalives').subscribe do |headers, payload|
        keepalive = JSON.parse(payload, :symbolize_names => true)
        keepalive[:name].should eq('i-424242')
        async_done
      end
    end
  end

  it "can send a check result" do
    async_wrapper do
      @client.setup_rabbitmq
      check = result_template[:check]
      @client.publish_result(check)
      result_queue = amq.queue('results')
      result_queue.subscribe do |headers, payload|
        result = JSON.parse(payload, :symbolize_names => true)
        result[:client].should eq('i-424242')
        result[:check][:name].should eq('foobar')
        async_done
      end
    end
  end

  it "can execute a check" do
    async_wrapper do
      @client.setup_rabbitmq
      check = {
        :name => 'foo',
        :command => 'echo -n foobar',
        :issued => epoch
      }
      @client.execute_check(check)
      amq.queue('results').subscribe do |headers, payload|
        result = JSON.parse(payload, :symbolize_names => true)
        result[:client].should eq('i-424242')
        result[:check][:output].should eq('foobar')
        async_done
      end
    end
  end

  it "can setup subscriptions" do
    async_wrapper do
      @client.setup_rabbitmq
      @client.setup_subscriptions
      amq.fanout('test', :passive => true) do |exchange, declare_ok|
        declare_ok.should be_an_instance_of(AMQ::Protocol::Exchange::DeclareOk)
        exchange.status.should eq(:opening)
        async_done
      end
    end
  end

  it "can receive a check request and execute the check (with tokens)" do
    async_wrapper do
      @client.setup_rabbitmq
      @client.setup_subscriptions
      timer(1) do
        check_request = {
          :name => 'tokens',
          :issued => epoch
        }
        amq.fanout('test').publish(check_request.to_json)
      end
      amq.queue('results').subscribe do |headers, payload|
        result = JSON.parse(payload, :symbolize_names => true)
        result[:client].should eq('i-424242')
        result[:check][:output].should eq('i-424242 true')
        result[:check][:status].should eq(2)
        async_done
{'fraction_non_alphanumeric': 0.10992136523339469, 'fraction_numerical': 0.012882717082148235, 'mean_word_length': 2.5796407185628745, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '4375582', 'n_tokens_mistral': 1911, 'n_tokens_neox': 1808, 'n_words': 422}