Sha256: d9f7f44bc767bebe144b670ad851da3264ed18bf86cd57a93316b3db304df276

Contents?: true

Size: 2 KB

Versions: 7

Compression:

Stored size: 2 KB

Contents

require_relative '../test_helper'

# Unit Test for SemanticLogger::Appender::Tcp
module Appender
  class UdpTest < Minitest::Test
    describe SemanticLogger::Appender::Udp do
      before do
        @appender = SemanticLogger::Appender::Udp.new(server: 'localhost:8088')
        @message  = 'AppenderUdpTest log message'
      end

      SemanticLogger::LEVELS.each do |level|
        it "send #{level}" do
          data = nil
          @appender.socket.stub(:send, -> d, flags { data = d }) do
            @appender.send(level, @message)
          end
          hash = JSON.parse(data)
          assert_equal @message, hash['message']
          assert_equal level.to_s, hash['level']
          refute hash['stack_trace']
        end

        it "send #{level} exceptions" do
          exc = nil
          begin
            Uh oh
          rescue Exception => e
            exc = e
          end
          data = nil
          @appender.socket.stub(:send, -> d, flags { data = d }) do
            @appender.send(level, 'Reading File', exc)
          end
          hash = JSON.parse(data)
          assert 'Reading File', hash['message']
          assert 'NameError', hash['exception']['name']
          assert 'undefined local variable or method', hash['exception']['message']
          assert_equal level.to_s, hash['level'], 'Should be error level (3)'
          assert hash['exception']['stack_trace'].first.include?(__FILE__), hash['exception']
        end

        it "send #{level} custom attributes" do
          data = nil
          @appender.socket.stub(:send, -> d, flags { data = d }) do
            @appender.send(level, @message, {key1: 1, key2: 'a'})
          end
          hash = JSON.parse(data)
          assert_equal @message, hash['message']
          assert_equal level.to_s, hash['level']
          refute hash['stack_trace']
          assert payload = hash['payload'], hash
          assert_equal 1, payload['key1'], payload
          assert_equal 'a', payload['key2'], payload
        end

      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
semantic_logger-4.2.0 test/appender/udp_test.rb
semantic_logger-4.1.1 test/appender/udp_test.rb
semantic_logger-4.1.0 test/appender/udp_test.rb
semantic_logger-4.0.0 test/appender/udp_test.rb
semantic_logger-4.0.0.rc1 test/appender/udp_test.rb
semantic_logger-4.0.0.beta2 test/appender/udp_test.rb
semantic_logger-4.0.0.beta1 test/appender/udp_test.rb