-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunction_test.go
More file actions
180 lines (139 loc) · 2.77 KB
/
function_test.go
File metadata and controls
180 lines (139 loc) · 2.77 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package gouse
import (
"errors"
"sync"
"testing"
"time"
)
func TestParallelizeFunc(t *testing.T) {
done := make(chan struct{})
func1 := func() {
time.Sleep(1 * time.Second)
}
func2 := func() {
time.Sleep(1 * time.Second)
}
go func() {
ParallelizeFunc(func1, func2)
done <- struct{}{}
}()
select {
case <-done:
case <-time.After(2 * time.Second):
t.Error("Parallelize(): timeout waiting to complete, maximum is 2 seconds")
}
}
func TestLockFunc(t *testing.T) {
var result int
callback := func() {
result = 42
}
lockFunc := LockFunc(callback)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
lockFunc.(func())()
}()
wg.Wait()
if result != 42 {
t.Errorf("expected 42, got %d", result)
}
}
func TestRWLockFunc(t *testing.T) {
var result int
callback := func() {
result = 84
}
rwLockFunc := RWLockFunc(callback)
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
rwLockFunc.(func())()
}()
go func() {
defer wg.Done()
rwLockFunc.(func())()
}()
wg.Wait()
if result != 84 {
t.Errorf("expected 84, got %d", result)
}
}
func TestLockHandlerWithNonFunctionCallback(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic, but code did not panic")
}
}()
lockHandler("not a function", func() {
// do nothing
}, func() {
// do nothing
})
}
func TestLockFuncConcurrency(t *testing.T) {
var count int
var mutex sync.Mutex
callback := func() {
mutex.Lock()
defer mutex.Unlock()
count++
}
lockFunc := LockFunc(callback)
var wg sync.WaitGroup
goroutines := 100
wg.Add(goroutines)
for range goroutines {
go func() {
defer wg.Done()
lockFunc.(func())()
}()
}
wg.Wait()
if count != goroutines {
t.Errorf("expected %d, got %d", goroutines, count)
}
}
func TestDeferWrapperSuccess(t *testing.T) {
cleanupCalled := false
cleanupFunc := func() {
cleanupCalled = true
}
mainFunc := func() error {
return nil
}
err := DeferWrapper(mainFunc, cleanupFunc)
if err != nil {
t.Errorf("expected no error, but got %v", err)
}
if !cleanupCalled {
t.Errorf("expected cleanup to be called, but it wasn't")
}
}
func TestDeferWrapperFailure(t *testing.T) {
cleanupCalled := false
cleanupFunc := func() {
cleanupCalled = true
}
mainFunc := func() error {
return errors.New("main function error")
}
err := DeferWrapper(mainFunc, cleanupFunc)
if err == nil || err.Error() != "main function error" {
t.Errorf("expected 'main function error', but got %v", err)
}
if !cleanupCalled {
t.Errorf("expected cleanup to be called, but it wasn't")
}
}
func TestDeferWrapperNilCleanup(t *testing.T) {
mainFunc := func() error {
return nil
}
err := DeferWrapper(mainFunc, nil)
if err != nil {
t.Errorf("expected no error, but got %v", err)
}
}