forked from fluxio/logging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cancellable_logger_test.go
56 lines (48 loc) · 2.13 KB
/
cancellable_logger_test.go
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
package logging
import (
"os"
"runtime"
"sync/atomic"
"testing"
"github.com/fluxio/sync_testing"
)
// A logger safe for concurrent usage.
type atomicLogger int64
func (l *atomicLogger) Trace(vals ...interface{}) { atomic.AddInt64((*int64)(l), 1) }
func (l *atomicLogger) Tracef(fmt string, args ...interface{}) { atomic.AddInt64((*int64)(l), 1) }
func (l *atomicLogger) Debug(vals ...interface{}) { atomic.AddInt64((*int64)(l), 1) }
func (l *atomicLogger) Debugf(fmt string, args ...interface{}) { atomic.AddInt64((*int64)(l), 1) }
func (l *atomicLogger) Info(vals ...interface{}) { atomic.AddInt64((*int64)(l), 1) }
func (l *atomicLogger) Infof(fmt string, args ...interface{}) { atomic.AddInt64((*int64)(l), 1) }
func (l *atomicLogger) Error(vals ...interface{}) { atomic.AddInt64((*int64)(l), 1) }
func (l *atomicLogger) Errorf(fmt string, args ...interface{}) { atomic.AddInt64((*int64)(l), 1) }
func (l *atomicLogger) LogLevel() Level { return ErrorLevel }
func (l *atomicLogger) SetLogLevel(lev Level) {}
func TestCancellableLogger(t *testing.T) {
runtime.GOMAXPROCS(10)
var cl CancellableLogger
var logger atomicLogger
cl.Logger = &logger
sync_testing.MaximizeContention(50,
func() { cl.Info("x") },
func() { cl.Info("x") },
func() { cl.Info("x") },
func() { cl.Info("x") },
func() { cl.Info("x") },
func() { cl.Info("x") },
func() { cl.Info("x") },
func() { cl.Info("x") },
func() { cl.Info("x") },
func() { cl.Cancel() },
)
// For some reason, running this test under the race detector works just
// fine (even if in the regression scenario of a race actually existing in
// the code) unless the test explicitly fails. Weird. So, if you set the
// "FAIL_FOR_RACE" env var, this test will artificially fail which seems to
// trigger the race detector's output. Example command line:
// FAIL_FOR_RACE=1 go test -race -run CancellableLogger genie/flow
// Try running that without the write-lock in CancellableLogger.Cancel().
if os.Getenv("FAIL_FOR_RACE") != "" {
t.Error("Artificially failing test to poke race detector.")
}
}