-
Notifications
You must be signed in to change notification settings - Fork 0
/
trigger_test.go
156 lines (137 loc) · 3.86 KB
/
trigger_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package slate
import (
"errors"
"fmt"
"testing"
"time"
)
func Test_TriggerPulse(t *testing.T) {
t.Run("NewTriggerPulse", func(t *testing.T) {
t.Run("nil callback", func(t *testing.T) {
if _, e := NewTriggerPulse(20*time.Millisecond, nil); e == nil {
t.Error("didn't returned the expected error")
} else if !errors.Is(e, ErrNilPointer) {
t.Errorf("(%v) when expected (%v)", e, ErrNilPointer)
}
})
t.Run("new TriggerPulse trigger", func(t *testing.T) {
if _, e := NewTriggerPulse(20*time.Millisecond, func() error {
return nil
}); e != nil {
t.Errorf("unexpected (%v) error", e)
}
})
})
t.Run("Close", func(t *testing.T) {
t.Run("is the same as stopping it", func(t *testing.T) {
called := false
sut, _ := NewTriggerPulse(20*time.Millisecond, func() error {
called = true
return nil
})
_ = sut.Close()
time.Sleep(40 * time.Millisecond)
if called {
t.Error("didn't prevented the TriggerPulse to execute")
}
})
})
t.Run("Delay", func(t *testing.T) {
t.Run("retrieves the trigger delay time", func(t *testing.T) {
duration := 20 * time.Millisecond
sut, _ := NewTriggerPulse(duration, func() error {
return nil
})
defer func() { _ = sut.Close() }()
if check := sut.Delay(); check != duration {
t.Errorf("returned (%v) wait duration", check)
}
})
})
t.Run("Execution", func(t *testing.T) {
t.Run("only trigger execution once", func(t *testing.T) {
count := 0
sut, _ := NewTriggerPulse(20*time.Millisecond, func() error {
count++
return nil
})
defer func() { _ = sut.Close() }()
time.Sleep(100 * time.Millisecond)
if count == 0 {
t.Error("didn't called the callback function once")
} else if count > 1 {
t.Error("recurrently called the callback function")
}
})
})
}
func Test_TriggerRecurring(t *testing.T) {
t.Run("NewTriggerRecurring", func(t *testing.T) {
t.Run("nil callback", func(t *testing.T) {
if _, e := NewTriggerRecurring(20*time.Millisecond, nil); e == nil {
t.Error("didn't returned the expected error")
} else if !errors.Is(e, ErrNilPointer) {
t.Errorf("(%v) when expected (%v)", e, ErrNilPointer)
}
})
t.Run("new TriggerRecurring trigger", func(t *testing.T) {
if _, e := NewTriggerRecurring(20*time.Millisecond, func() error {
return nil
}); e != nil {
t.Errorf("unexpected (%v) error", e)
}
})
})
t.Run("Close", func(t *testing.T) {
t.Run("is the same as stopping it", func(t *testing.T) {
called := false
sut, _ := NewTriggerRecurring(20*time.Millisecond, func() error {
called = true
return nil
})
_ = sut.Close()
time.Sleep(40 * time.Millisecond)
if called {
t.Error("didn't stop the TriggerRecurring to be executed")
}
})
})
t.Run("Delay", func(t *testing.T) {
t.Run("retrieves the trigger interval duration", func(t *testing.T) {
duration := 20 * time.Millisecond
sut, _ := NewTriggerRecurring(duration, func() error {
return nil
})
defer func() { _ = sut.Close() }()
if check := sut.Delay(); check != duration {
t.Errorf("returned (%v) interval duration", check)
}
})
})
t.Run("Execution", func(t *testing.T) {
t.Run("run trigger multiple times", func(t *testing.T) {
count := 0
sut, _ := NewTriggerRecurring(20*time.Millisecond, func() error {
count++
return nil
})
defer func() { _ = sut.Close() }()
time.Sleep(100 * time.Millisecond)
if count <= 2 {
t.Error("didn't recurrently called the callback function")
}
})
t.Run("stop the trigger on callback error", func(t *testing.T) {
count := 0
sut, _ := NewTriggerRecurring(20*time.Millisecond, func() error {
count++
return fmt.Errorf("__dummy_error__")
})
defer func() { _ = sut.Close() }()
time.Sleep(100 * time.Millisecond)
if count != 1 {
t.Error("didn't stop recursion calls after the first error")
}
})
})
}