-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonitor_test.go
126 lines (101 loc) · 2.59 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
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package simplex
import (
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"sync"
"testing"
"time"
)
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 task does not fire", func(t *testing.T) {
panic := func() {
panic("test failed")
}
cancel := mon.WaitUntil(time.Hour, panic)
cancel()
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 task fires", func(t *testing.T) {
finish := make(chan struct{})
mon.WaitUntil(time.Hour, func() {
close(finish)
})
mon.AdvanceTime(start.Add(time.Hour * 2))
<-ticked
<-finish
})
}
func TestMonitorAsyncWaitFor(t *testing.T) {
start := time.Now()
mon := NewMonitor(start, makeLogger(t))
var wg sync.WaitGroup
wg.Add(1)
mon.WaitFor(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.WaitUntil(10*time.Millisecond, wg.Done)
mon.WaitFor(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.WaitFor(func() {
go mon.AdvanceTime(start.Add(10 * time.Millisecond))
mon.WaitUntil(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}
}