-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshared_test.go
155 lines (138 loc) · 3.25 KB
/
shared_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
/*
* Copyright 2023 RisingWave Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ctrlkit
import (
"context"
"errors"
"fmt"
"sync"
"sync/atomic"
"testing"
"time"
ctrl "sigs.k8s.io/controller-runtime"
)
func Test_Shared_Decorator(t *testing.T) {
testDecorator[sharedAction](t, "Shared")
}
func newAtomicCounter(cnt *int32, resultErr resultErr) Action {
return NewAction("AtomicCounter", func(ctx context.Context) (ctrl.Result, error) {
atomic.AddInt32(cnt, 1)
return resultErr.result, resultErr.err
})
}
func Test_Shared_Optimize(t *testing.T) {
if Shared(Shared(Nop)).Description() != Shared(Nop).Description() {
t.Fail()
}
}
func Test_Shared(t *testing.T) {
testcases := map[string]struct {
resultErr resultErr
extraRun int
}{
"extra-run-0": {
extraRun: 0,
},
"extra-run-1": {
extraRun: 1,
},
"extra-run-3": {
resultErr: resultErr{
result: ctrl.Result{RequeueAfter: time.Second},
err: errors.New("some error"),
},
extraRun: 3,
},
"extra-run-10": {
resultErr: resultErr{
result: ctrl.Result{Requeue: true},
err: nil,
},
extraRun: 10,
},
}
for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
cnt := int32(0)
shared := Shared(newAtomicCounter(&cnt, tc.resultErr))
var r ctrl.Result
var err error
firstRunDone := make(chan bool)
fatal := false
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
r, err = shared.Run(context.Background())
close(firstRunDone)
}()
for i := 0; i < tc.extraRun; i++ {
wg.Add(1)
go func() {
defer wg.Done()
extraR, extraErr := shared.Run(context.Background())
// Wait for the first result and compare.
<-firstRunDone
if r != extraR || err != extraErr {
fatal = true
}
}()
}
wg.Wait()
if fatal {
t.Fatal("result not the same")
}
if cnt != 1 {
t.Fatal("run count is not 1")
}
})
}
}
func Test_Shared_Run_Panic(t *testing.T) {
defer func() {
if r := recover(); r != nil {
fmt.Println("Shared Panic:", r)
} else {
t.Fail()
}
}()
x := NewAction("panic", func(ctx context.Context) (ctrl.Result, error) {
panic("Aaa panic!!")
})
Shared(x).Run(context.Background())
}
func Test_Shared_Run_Panic_2(t *testing.T) {
s := Shared(NewAction("panic", func(ctx context.Context) (ctrl.Result, error) {
panic("Aaa panic!!")
}))
runPanickingActionOnceAndReturnPanic := func(s Action) (p any) {
defer func() {
if r := recover(); r != nil {
fmt.Println("Panic:", r)
p = r
} else {
t.Fail()
}
}()
s.Run(context.Background())
return nil
}
firstPanic := runPanickingActionOnceAndReturnPanic(s)
secondPanic := runPanickingActionOnceAndReturnPanic(s)
if firstPanic != secondPanic {
t.Fail()
}
}