File: test_nested_diagnostic_context.rb

package info (click to toggle)
ruby-logging 2.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 660 kB
  • sloc: ruby: 6,139; sh: 11; makefile: 2
file content (103 lines) | stat: -rw-r--r-- 2,327 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

require File.expand_path('../setup', __FILE__)

module TestLogging

  class TestNestedDiagnosticContext < Test::Unit::TestCase
    include LoggingTestCase

    def test_push_pop
      ary = Logging.ndc.context
      assert ary.empty?

      assert_nil Logging.ndc.peek

      Logging.ndc.push 'first context'
      assert_equal 'first context', Logging.ndc.peek

      Logging.ndc << 'second'
      Logging.ndc << 'third'
      assert_equal 'third', Logging.ndc.peek
      assert_equal 3, ary.length

      assert_equal 'third', Logging.ndc.pop
      assert_equal 2, ary.length

      assert_equal 'second', Logging.ndc.pop
      assert_equal 1, ary.length

      assert_equal 'first context', Logging.ndc.pop
      assert ary.empty?
    end

    def test_push_block
      ary = Logging.ndc.context

      Logging.ndc.push('first context') do
        assert_equal 'first context', Logging.ndc.peek
      end
      assert ary.empty?

      Logging.ndc.push('first context') do
        assert_raise(ZeroDivisionError) do
          Logging.ndc.push('first context') { 1/0 }
        end
      end
      assert ary.empty?
    end

    def test_clear
      ary = Logging.ndc.context
      assert ary.empty?

      Logging.ndc << 'a' << 'b' << 'c' << 'd'
      assert_equal 'd', Logging.ndc.peek
      assert_equal 4, ary.length

      Logging.ndc.clear
      assert_nil Logging.ndc.peek
    end

    def test_thread_uniqueness
      Logging.ndc << 'first' << 'second'

      t = Thread.new {
        sleep

        Logging.ndc.clear
        assert_nil Logging.ndc.peek

        Logging.ndc << 42
        assert_equal 42, Logging.ndc.peek
      }

      Thread.pass until t.status == 'sleep'
      t.run
      t.join

      assert_equal 'second', Logging.ndc.peek
    end

    def test_thread_inheritance
      Logging.ndc << 'first' << 'second'

      t = Thread.new(Logging.ndc.context) { |ary|
        sleep

        assert_not_equal ary.object_id, Logging.ndc.context.object_id

        if Logging::INHERIT_CONTEXT
          assert_equal %w[first second], Logging.ndc.context
        else
          assert_empty Logging.ndc.context
        end
      }

      Thread.pass until t.status == 'sleep'
      Logging.ndc << 'third'

      t.run
      t.join
    end
  end  # class TestNestedDiagnosticContext
end  # module TestLogging