...

Source file src/github.com/spf13/jwalterweatherman/default_notepad_test.go

Documentation: github.com/spf13/jwalterweatherman

     1  // Copyright © 2016 Steve Francia <spf@spf13.com>.
     2  //
     3  // Use of this source code is governed by an MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package jwalterweatherman
     7  
     8  import (
     9  	"bytes"
    10  	"io/ioutil"
    11  	"sync"
    12  	"testing"
    13  
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestThresholds(t *testing.T) {
    18  	SetStdoutThreshold(LevelError)
    19  	require.Equal(t, StdoutThreshold(), LevelError)
    20  	SetLogThreshold(LevelCritical)
    21  	require.Equal(t, LogThreshold(), LevelCritical)
    22  	require.NotEqual(t, StdoutThreshold(), LevelCritical)
    23  	SetStdoutThreshold(LevelWarn)
    24  	require.Equal(t, StdoutThreshold(), LevelWarn)
    25  }
    26  
    27  func TestDefaultLogging(t *testing.T) {
    28  	var outputBuf, logBuf bytes.Buffer
    29  
    30  	defaultNotepad.logHandle = &logBuf
    31  	defaultNotepad.outHandle = &outputBuf
    32  
    33  	SetLogThreshold(LevelWarn)
    34  	SetStdoutThreshold(LevelError)
    35  
    36  	FATAL.Println("fatal err")
    37  	CRITICAL.Println("critical err")
    38  	ERROR.Println("an error")
    39  	WARN.Println("a warning")
    40  	INFO.Println("information")
    41  	DEBUG.Println("debugging info")
    42  	TRACE.Println("trace")
    43  
    44  	require.Contains(t, logBuf.String(), "fatal err")
    45  	require.Contains(t, logBuf.String(), "critical err")
    46  	require.Contains(t, logBuf.String(), "an error")
    47  	require.Contains(t, logBuf.String(), "a warning")
    48  	require.NotContains(t, logBuf.String(), "information")
    49  	require.NotContains(t, logBuf.String(), "debugging info")
    50  	require.NotContains(t, logBuf.String(), "trace")
    51  
    52  	require.Contains(t, outputBuf.String(), "fatal err")
    53  	require.Contains(t, outputBuf.String(), "critical err")
    54  	require.Contains(t, outputBuf.String(), "an error")
    55  	require.NotContains(t, outputBuf.String(), "a warning")
    56  	require.NotContains(t, outputBuf.String(), "information")
    57  	require.NotContains(t, outputBuf.String(), "debugging info")
    58  	require.NotContains(t, outputBuf.String(), "trace")
    59  }
    60  
    61  func TestLogCounter(t *testing.T) {
    62  	assert := require.New(t)
    63  
    64  	defaultNotepad.logHandle = ioutil.Discard
    65  	defaultNotepad.outHandle = ioutil.Discard
    66  
    67  	errorCounter := &Counter{}
    68  
    69  	SetLogThreshold(LevelTrace)
    70  	SetStdoutThreshold(LevelTrace)
    71  	SetLogListeners(LogCounter(errorCounter, LevelError))
    72  
    73  	FATAL.Println("fatal err")
    74  	CRITICAL.Println("critical err")
    75  	WARN.Println("a warning")
    76  	WARN.Println("another warning")
    77  	INFO.Println("information")
    78  	DEBUG.Println("debugging info")
    79  	TRACE.Println("trace")
    80  
    81  	wg := &sync.WaitGroup{}
    82  
    83  	for i := 0; i < 10; i++ {
    84  		wg.Add(1)
    85  		go func() {
    86  			defer wg.Done()
    87  			for j := 0; j < 10; j++ {
    88  				ERROR.Println("error", j)
    89  				// check for data races
    90  				assert.True(errorCounter.Count() > uint64(j))
    91  			}
    92  		}()
    93  
    94  	}
    95  
    96  	wg.Wait()
    97  
    98  	assert.Equal(uint64(102), errorCounter.Count())
    99  }
   100  

View as plain text