~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/gopkg.in/inconshreveable/log15.v2/ext/ext_test.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package ext
2
 
 
3
 
import (
4
 
        "errors"
5
 
        log "gopkg.in/inconshreveable/log15.v2"
6
 
        "math"
7
 
        "testing"
8
 
)
9
 
 
10
 
func testHandler() (log.Handler, *log.Record) {
11
 
        rec := new(log.Record)
12
 
        return log.FuncHandler(func(r *log.Record) error {
13
 
                *rec = *r
14
 
                return nil
15
 
        }), rec
16
 
}
17
 
 
18
 
func TestHotSwapHandler(t *testing.T) {
19
 
        t.Parallel()
20
 
 
21
 
        h1, r1 := testHandler()
22
 
 
23
 
        l := log.New()
24
 
        h := HotSwapHandler(h1)
25
 
        l.SetHandler(h)
26
 
 
27
 
        l.Info("to h1")
28
 
        if r1.Msg != "to h1" {
29
 
                t.Fatalf("didn't get expected message to h1")
30
 
        }
31
 
 
32
 
        h2, r2 := testHandler()
33
 
        h.Swap(h2)
34
 
        l.Info("to h2")
35
 
        if r2.Msg != "to h2" {
36
 
                t.Fatalf("didn't get expected message to h2")
37
 
        }
38
 
}
39
 
 
40
 
func TestSpeculativeHandler(t *testing.T) {
41
 
        t.Parallel()
42
 
 
43
 
        // test with an even multiple of the buffer size, less than full buffer size
44
 
        // and not a multiple of the buffer size
45
 
        for _, count := range []int{10000, 50, 432} {
46
 
                recs := make(chan *log.Record)
47
 
                done := make(chan int)
48
 
                spec := SpeculativeHandler(100, log.ChannelHandler(recs))
49
 
 
50
 
                go func() {
51
 
                        defer close(done)
52
 
                        expectedCount := int(math.Min(float64(count), float64(100)))
53
 
                        expectedIdx := count - expectedCount
54
 
                        for r := range recs {
55
 
                                if r.Ctx[1] != expectedIdx {
56
 
                                        t.Errorf("Bad ctx 'i', got %d expected %d", r.Ctx[1], expectedIdx)
57
 
                                        return
58
 
                                }
59
 
                                expectedIdx++
60
 
                                expectedCount--
61
 
 
62
 
                                if expectedCount == 0 {
63
 
                                        // got everything we expected
64
 
                                        break
65
 
                                }
66
 
                        }
67
 
 
68
 
                        select {
69
 
                        case <-recs:
70
 
                                t.Errorf("got an extra record we shouldn't have!")
71
 
                        default:
72
 
                        }
73
 
                }()
74
 
 
75
 
                lg := log.New()
76
 
                lg.SetHandler(spec)
77
 
                for i := 0; i < count; i++ {
78
 
                        lg.Debug("test speculative", "i", i)
79
 
                }
80
 
 
81
 
                go spec.Flush()
82
 
 
83
 
                // wait for the go routine to finish
84
 
                <-done
85
 
        }
86
 
}
87
 
 
88
 
func TestErrorHandler(t *testing.T) {
89
 
        t.Parallel()
90
 
 
91
 
        h, r := testHandler()
92
 
        lg := log.New()
93
 
        lg.SetHandler(EscalateErrHandler(
94
 
                log.LvlFilterHandler(log.LvlError, h)))
95
 
 
96
 
        lg.Debug("some function result", "err", nil)
97
 
        if r.Msg != "" {
98
 
                t.Fatalf("Expected debug level message to be filtered")
99
 
        }
100
 
 
101
 
        lg.Debug("some function result", "err", errors.New("failed operation"))
102
 
        if r.Msg != "some function result" {
103
 
                t.Fatalf("Expected debug level message to be escalated and pass lvlfilter")
104
 
        }
105
 
 
106
 
        if r.Lvl != log.LvlError {
107
 
                t.Fatalf("Expected debug level message to be escalated to LvlError")
108
 
        }
109
 
}