File: jww_test.go

package info (click to toggle)
golang-github-spf13-jwalterweatherman 0.0~git20170109.0.fa7ca7e.really.git20160311.0.33c24e7-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch, stretch-backports
  • size: 64 kB
  • sloc: makefile: 2
file content (96 lines) | stat: -rw-r--r-- 2,872 bytes parent folder | download
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
// Copyright © 2016 Steve Francia <spf@spf13.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package jwalterweatherman

import (
	"bytes"
	"github.com/stretchr/testify/assert"
	"sync"
	"testing"
)

func TestLevels(t *testing.T) {
	SetStdoutThreshold(LevelError)
	assert.Equal(t, StdoutThreshold(), LevelError)
	SetLogThreshold(LevelCritical)
	assert.Equal(t, LogThreshold(), LevelCritical)
	assert.NotEqual(t, StdoutThreshold(), LevelCritical)
	SetStdoutThreshold(LevelWarn)
	assert.Equal(t, StdoutThreshold(), LevelWarn)
}

func TestDefaultLogging(t *testing.T) {
	outputBuf := new(bytes.Buffer)
	logBuf := new(bytes.Buffer)
	LogHandle = logBuf
	OutHandle = outputBuf

	SetLogThreshold(LevelWarn)
	SetStdoutThreshold(LevelError)

	FATAL.Println("fatal err")
	CRITICAL.Println("critical err")
	ERROR.Println("an error")
	WARN.Println("a warning")
	INFO.Println("information")
	DEBUG.Println("debugging info")
	TRACE.Println("trace")

	assert.Contains(t, logBuf.String(), "fatal err")
	assert.Contains(t, logBuf.String(), "critical err")
	assert.Contains(t, logBuf.String(), "an error")
	assert.Contains(t, logBuf.String(), "a warning")
	assert.NotContains(t, logBuf.String(), "information")
	assert.NotContains(t, logBuf.String(), "debugging info")
	assert.NotContains(t, logBuf.String(), "trace")

	assert.Contains(t, outputBuf.String(), "fatal err")
	assert.Contains(t, outputBuf.String(), "critical err")
	assert.Contains(t, outputBuf.String(), "an error")
	assert.NotContains(t, outputBuf.String(), "a warning")
	assert.NotContains(t, outputBuf.String(), "information")
	assert.NotContains(t, outputBuf.String(), "debugging info")
	assert.NotContains(t, outputBuf.String(), "trace")
}

func TestLogCounter(t *testing.T) {
	ResetLogCounters()

	FATAL.Println("fatal err")
	CRITICAL.Println("critical err")
	WARN.Println("a warning")
	WARN.Println("another warning")
	INFO.Println("information")
	DEBUG.Println("debugging info")
	TRACE.Println("trace")

	wg := &sync.WaitGroup{}

	for i := 0; i < 10; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			for j := 0; j < 10; j++ {
				ERROR.Println("error", j)
				// check for data races
				assert.True(t, LogCountForLevel(LevelError) > uint64(j))
				assert.True(t, LogCountForLevelsGreaterThanorEqualTo(LevelError) > uint64(j))
			}
		}()

	}

	wg.Wait()

	assert.Equal(t, uint64(1), LogCountForLevel(LevelFatal))
	assert.Equal(t, uint64(1), LogCountForLevel(LevelCritical))
	assert.Equal(t, uint64(2), LogCountForLevel(LevelWarn))
	assert.Equal(t, uint64(1), LogCountForLevel(LevelInfo))
	assert.Equal(t, uint64(1), LogCountForLevel(LevelDebug))
	assert.Equal(t, uint64(1), LogCountForLevel(LevelTrace))
	assert.Equal(t, uint64(100), LogCountForLevel(LevelError))
	assert.Equal(t, uint64(102), LogCountForLevelsGreaterThanorEqualTo(LevelError))
}