test/appender/elasticsearch_test.rb in semantic_logger-4.0.0 vs test/appender/elasticsearch_test.rb in semantic_logger-4.1.0

- old
+ new

@@ -1,73 +1,80 @@ require_relative '../test_helper' # Unit Test for SemanticLogger::Appender::Elasticsearch module Appender class ElasticsearchTest < Minitest::Test - response_mock = Struct.new(:code, :body) - describe SemanticLogger::Appender::Elasticsearch do before do - Net::HTTP.stub_any_instance(:start, true) do + skip('Concurrent::TimerTask issue is preventing the process from stopping.') if defined? JRuby + Elasticsearch::Transport::Client.stub_any_instance(:bulk, true) do @appender = SemanticLogger::Appender::Elasticsearch.new( - url: 'http://localhost:9200' + url: 'http://localhost:9200', + batch_size: 1 # immediate flush ) end @message = 'AppenderElasticsearchTest log message' end + after do + @appender.close if @appender + end + it 'logs to daily indexes' do index = nil - @appender.stub(:post, -> json, ind { index = ind }) do + @appender.stub(:enqueue, ->(ind, json){ index = ind['index']['_index'] } ) do @appender.info @message end - assert_equal "/semantic_logger-#{Time.now.utc.strftime('%Y.%m.%d')}/log", index + assert_equal "semantic_logger-#{Time.now.strftime('%Y.%m.%d')}", index end SemanticLogger::LEVELS.each do |level| it "send #{level}" do request = nil - @appender.http.stub(:request, -> r { request = r; response_mock.new('200', 'ok') }) do + @appender.client.stub(:bulk, -> r { request = r; {"status" => 201 } }) do @appender.send(level, @message) end - message = JSON.parse(request.body) - assert_equal @message, message['message'] - assert_equal level.to_s, message['level'] - refute message['exception'] + + message = request[:body][1] + assert_equal @message, message[:message] + assert_equal level, message[:level] end it "sends #{level} exceptions" do exc = nil begin Uh oh rescue Exception => e exc = e end request = nil - @appender.http.stub(:request, -> r { request = r; response_mock.new('200', 'ok') }) do + @appender.client.stub(:bulk, -> r { request = r; {"status" => 201 } }) do @appender.send(level, 'Reading File', exc) end - hash = JSON.parse(request.body) - assert 'Reading File', hash['message'] - assert exception = hash['exception'] - assert 'NameError', exception['name'] - assert 'undefined local variable or method', exception['message'] - assert_equal level.to_s, hash['level'] - assert exception['stack_trace'].first.include?(__FILE__), exception + + hash = request[:body][1] + + assert 'Reading File', hash[:message] + assert exception = hash[:exception] + assert 'NameError', exception[:name] + assert 'undefined local variable or method', exception[:message] + assert_equal level, hash[:level] + assert exception[:stack_trace].first.include?(__FILE__), exception end it "sends #{level} custom attributes" do request = nil - @appender.http.stub(:request, -> r { request = r; response_mock.new('200', 'ok') }) do + @appender.client.stub(:bulk, -> r { request = r; {"status" => 201 } }) do @appender.send(level, @message, {key1: 1, key2: 'a'}) end - message = JSON.parse(request.body) - assert_equal @message, message['message'] - assert_equal level.to_s, message['level'] - refute message['stack_trace'] - assert payload = message['payload'], message - assert_equal 1, payload['key1'], message - assert_equal 'a', payload['key2'], message + + message = request[:body][1] + assert_equal @message, message[:message] + assert_equal level, message[:level] + refute message[:stack_trace] + assert payload = message[:payload], message + assert_equal 1, payload[:key1], message + assert_equal 'a', payload[:key2], message end end end end