-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmonitor_test.go
140 lines (110 loc) · 2.85 KB
/
monitor_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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package simplex
import (
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func TestMonitorDoubleClose(t *testing.T) {
start := time.Now()
mon := NewMonitor(start, makeLogger(t))
require.True(t, mon.shouldRun())
mon.Close()
require.False(t, mon.shouldRun())
mon.Close()
require.False(t, mon.shouldRun())
}
func TestMonitorPrematureCancelTask(t *testing.T) {
start := time.Now()
ticked := make(chan struct{})
mon := NewMonitor(start, makeLogger(t))
mon.logger.(*testLogger).intercept(func(entry zapcore.Entry) error {
if entry.Message == "Ticked" {
ticked <- struct{}{}
}
return nil
})
t.Run("Cancelled future task does not fire", func(t *testing.T) {
panic := func() {
panic("test failed")
}
mon.FutureTask(time.Hour, panic)
mon.CancelFutureTask()
mon.AdvanceTime(start.Add(time.Hour))
select {
case <-ticked:
case <-time.After(time.Minute):
require.FailNow(t, "timed out waiting on tick")
}
})
t.Run("Non-Cancelled future task fires", func(t *testing.T) {
finish := make(chan struct{})
mon.FutureTask(time.Hour, func() {
close(finish)
})
mon.AdvanceTime(start.Add(time.Hour * 2))
<-ticked
<-finish
})
t.Run("Cancelled task does not fire", func(t *testing.T) {
finish := make(chan struct{})
mon.RunTask(func() {
<-finish
close(finish) // Test should panic if we have a double close
})
mon.CancelTask()
close(finish)
})
}
func TestMonitorAsyncWaitFor(t *testing.T) {
start := time.Now()
mon := NewMonitor(start, makeLogger(t))
var wg sync.WaitGroup
wg.Add(1)
mon.RunTask(wg.Done)
wg.Wait()
}
func TestMonitorAsyncWaitUntilWithWaitFor(t *testing.T) {
start := time.Now()
mon := NewMonitor(start, makeLogger(t))
var wg sync.WaitGroup
wg.Add(1)
mon.FutureTask(10*time.Millisecond, wg.Done)
mon.RunTask(func() {
mon.AdvanceTime(start.Add(10 * time.Millisecond))
})
wg.Wait()
}
func TestMonitorAsyncWaitForWithNestedWaitUntil(t *testing.T) {
start := time.Now()
mon := NewMonitor(start, makeLogger(t))
var wg sync.WaitGroup
wg.Add(1)
mon.RunTask(func() {
go mon.AdvanceTime(start.Add(10 * time.Millisecond))
mon.FutureTask(10*time.Millisecond, wg.Done)
})
wg.Wait()
}
type testLogger struct {
*zap.Logger
}
func (tl *testLogger) Trace(msg string, fields ...zap.Field) {
tl.Log(zapcore.DebugLevel, msg, fields...)
}
func (tl *testLogger) Verbo(msg string, fields ...zap.Field) {
tl.Log(zapcore.DebugLevel, msg, fields...)
}
func (t *testLogger) intercept(hook func(entry zapcore.Entry) error) {
logger := t.Logger.WithOptions(zap.Hooks(hook))
t.Logger = logger
}
func makeLogger(t *testing.T) *testLogger {
logger, err := zap.NewDevelopment()
require.NoError(t, err)
return &testLogger{Logger: logger}
}