Repository navigation
Expand file tree
/
Copy pathclient_test.go
More file actions
236 lines (209 loc) · 6.44 KB
/
Copy pathclient_test.go
File metadata and controls
236 lines (209 loc) · 6.44 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package configmapdynconfig
import (
"context"
"sync/atomic"
"testing"
"time"
"go.temporal.io/server/common/dynamicconfig"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/metrics"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes/fake"
k8stesting "k8s.io/client-go/testing"
)
const (
testNamespace = "temporal"
testConfigMapName = "temporal-dynconfig"
testDataKey = "config.yaml"
)
func newTestClient(t *testing.T, fakeK8s *fake.Clientset) Client {
t.Helper()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
c, err := NewClient(ctx, fakeK8s, testNamespace, testConfigMapName, testDataKey,
log.NewNoopLogger(), metrics.NoopMetricsHandler)
if err != nil {
t.Fatalf("NewClient: %v", err)
}
t.Cleanup(c.Stop)
return c
}
func configMapWithData(data string) *corev1.ConfigMap {
return &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: testConfigMapName,
Namespace: testNamespace,
},
Data: map[string]string{
testDataKey: data,
},
}
}
// TestGetValue_Empty — new client with empty ConfigMap returns nil for any key.
func TestGetValue_Empty(t *testing.T) {
fakeK8s := fake.NewSimpleClientset(configMapWithData(""))
c := newTestClient(t, fakeK8s)
key := dynamicconfig.MakeKey("frontend.rps")
got := c.GetValue(key)
if got != nil {
t.Errorf("expected nil, got %v", got)
}
}
// TestGetValue_LoadsFromConfigMap — client loads values from existing ConfigMap.
func TestGetValue_LoadsFromConfigMap(t *testing.T) {
yaml := `
frontend.namespaceRPS.visibility:
- value: 99
constraints: {}
`
fakeK8s := fake.NewSimpleClientset(configMapWithData(yaml))
c := newTestClient(t, fakeK8s)
key := dynamicconfig.MakeKey("frontend.namespaceRPS.visibility")
got := c.GetValue(key)
if len(got) != 1 {
t.Fatalf("expected 1 value, got %d", len(got))
}
if got[0].Value != 99 {
t.Errorf("expected value 99, got %v", got[0].Value)
}
}
// TestGetValue_AfterWrite — WriteConfig updates the ConfigMap, GetValue returns new value.
func TestGetValue_AfterWrite(t *testing.T) {
fakeK8s := fake.NewSimpleClientset(configMapWithData(""))
c := newTestClient(t, fakeK8s)
key := dynamicconfig.MakeKey("matching.numTaskqueueReadPartitions")
values := []dynamicconfig.ConstrainedValue{
{Value: 8, Constraints: dynamicconfig.Constraints{}},
}
if err := c.WriteConfig(context.Background(), key, values); err != nil {
t.Fatalf("WriteConfig: %v", err)
}
got := c.GetValue(key)
if len(got) != 1 {
t.Fatalf("expected 1 value, got %d", len(got))
}
if got[0].Value != 8 {
t.Errorf("expected value 8, got %v", got[0].Value)
}
}
// TestSubscribe_CalledOnChange — Subscribe callback fires when ConfigMap is updated via watch.
func TestSubscribe_CalledOnChange(t *testing.T) {
fakeWatcher := watch.NewFake()
fakeK8s := fake.NewSimpleClientset(configMapWithData(""))
fakeK8s.PrependWatchReactor("configmaps", func(action k8stesting.Action) (bool, watch.Interface, error) {
return true, fakeWatcher, nil
})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c, err := NewClient(ctx, fakeK8s, testNamespace, testConfigMapName, testDataKey,
log.NewNoopLogger(), metrics.NoopMetricsHandler)
if err != nil {
t.Fatalf("NewClient: %v", err)
}
defer c.Stop()
var called atomic.Int32
var foundGlobalRPS atomic.Bool
cancel2 := c.Subscribe(func(changed map[dynamicconfig.Key][]dynamicconfig.ConstrainedValue) {
for k, vs := range changed {
if k.String() == "frontend.globalrps" && len(vs) == 1 {
if v, ok := vs[0].Value.(int); ok && v == 9999 {
foundGlobalRPS.Store(true)
}
}
}
called.Add(1)
})
defer cancel2()
// Simulate a MODIFIED event on the ConfigMap via the fake watcher.
updatedCM := configMapWithData(`
frontend.globalRPS:
- value: 9999
constraints: {}
`)
fakeWatcher.Modify(updatedCM)
// Wait for the callback to fire (up to 3s).
deadline := time.Now().Add(3 * time.Second)
for time.Now().Before(deadline) {
if foundGlobalRPS.Load() {
break
}
time.Sleep(10 * time.Millisecond)
}
if !foundGlobalRPS.Load() {
t.Fatalf("Subscribe callback was not called with frontend.globalrps=9999 (called=%d)", called.Load())
}
}
// TestDumpAll — returns all loaded keys.
func TestDumpAll(t *testing.T) {
yaml := `
frontend.globalRPS:
- value: 6000
constraints: {}
matching.numTaskqueueReadPartitions:
- value: 4
constraints: {}
`
fakeK8s := fake.NewSimpleClientset(configMapWithData(yaml))
c := newTestClient(t, fakeK8s)
dump := c.DumpAll()
if dump == nil {
t.Fatal("DumpAll returned nil")
}
if _, ok := dump["frontend.globalrps"]; !ok {
t.Error("DumpAll missing frontend.globalrps")
}
if _, ok := dump["matching.numtaskqueuereadpartitions"]; !ok {
t.Error("DumpAll missing matching.numtaskqueuereadpartitions")
}
}
// TestSeedDefaults — missing keys from defaults.yaml are added; existing keys are not overwritten.
func TestSeedDefaults(t *testing.T) {
// Provide a ConfigMap that already has one key from defaults.yaml with a
// custom value. After NewClient starts, that value must remain unchanged,
// and other default keys must have been seeded.
initial := `
frontend.namespaceRPS.visibility:
- value: 999
constraints: {}
`
fakeK8s := fake.NewSimpleClientset(configMapWithData(initial))
c := newTestClient(t, fakeK8s)
// The existing key must keep its custom value.
key := dynamicconfig.MakeKey("frontend.namespaceRPS.visibility")
got := c.GetValue(key)
if len(got) != 1 {
t.Fatalf("expected 1 value, got %d", len(got))
}
if got[0].Value != 999 {
t.Errorf("existing key was overwritten; expected 999 got %v", got[0].Value)
}
// At least one other default key should have been seeded.
key2 := dynamicconfig.MakeKey("frontend.globalRPS")
got2 := c.GetValue(key2)
if len(got2) == 0 {
t.Error("expected frontend.globalRPS to be seeded from defaults.yaml but got nothing")
}
}
// TestStop — Stop() can be called without panic.
func TestStop(t *testing.T) {
fakeK8s := fake.NewSimpleClientset(configMapWithData(""))
ctx := context.Background()
c, err := NewClient(ctx, fakeK8s, testNamespace, testConfigMapName, testDataKey,
log.NewNoopLogger(), metrics.NoopMetricsHandler)
if err != nil {
t.Fatalf("NewClient: %v", err)
}
// Should not panic or block.
done := make(chan struct{})
go func() {
defer close(done)
c.Stop()
}()
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("Stop() timed out")
}
}