Sha256: de0ddf46a5ecefe8e2417ff484b64770568c8dd8c1a6a8be20958cc36134cb20

Contents?: true

Size: 2 KB

Versions: 4

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

4 entries across 4 versions & 1 rubygems

Version Path
semantic_logger-4.3.1 test/appender/udp_test.rb
semantic_logger-4.3.0 test/appender/udp_test.rb
semantic_logger-4.2.2 test/appender/udp_test.rb
semantic_logger-4.2.1 test/appender/udp_test.rb