Skip to content

Commit 9c12f0e

Browse files
committed
fix: circuitbreaker ut
1 parent 8893e8f commit 9c12f0e

File tree

2 files changed

+32
-42
lines changed

2 files changed

+32
-42
lines changed

pkg/manager/controllers/circuitbreaker/circuitbreaker_controller_suit_test.go

-26
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package circuitbreaker
1818

1919
import (
2020
"context"
21-
"fmt"
2221
"os"
2322
"path/filepath"
2423
"sync"
@@ -42,8 +41,6 @@ var (
4241
mgr manager.Manager
4342
c client.Client
4443

45-
request chan struct{}
46-
4744
ctx context.Context
4845
cancel context.CancelFunc
4946
wg sync.WaitGroup
@@ -70,11 +67,9 @@ func TestMain(m *testing.M) {
7067
MetricsBindAddress: "0",
7168
})
7269

73-
request = make(chan struct{}, 5)
7470
if err = (&CircuitBreakerReconciler{
7571
Client: &warpClient{
7672
Client: mgr.GetClient(),
77-
ch: request,
7873
},
7974
}).SetupWithManager(mgr); err != nil {
8075
panic(err)
@@ -100,29 +95,8 @@ func Stop() {
10095

10196
type warpClient struct {
10297
client.Client
103-
ch chan struct{}
10498
}
10599

106100
func (w *warpClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
107-
w.ch <- struct{}{}
108101
return w.Client.Get(ctx, key, obj, opts...)
109102
}
110-
111-
func waitProcess() {
112-
waitProcessFinished(request)
113-
}
114-
115-
func waitProcessFinished(reqChan chan struct{}) {
116-
timeout := time.After(10 * time.Second)
117-
for {
118-
select {
119-
case <-time.After(5 * time.Second):
120-
return
121-
case <-timeout:
122-
fmt.Println("timeout!")
123-
return
124-
case <-reqChan:
125-
continue
126-
}
127-
}
128-
}

pkg/manager/controllers/circuitbreaker/circuitbreaker_controller_test.go

+32-16
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,25 @@ func TestCircuitBreaker(t *testing.T) {
117117
defer func() {
118118
c.Delete(ctx, testPod)
119119
}()
120-
waitProcess()
121120
cb := &ctrlmeshv1alpha1.CircuitBreaker{}
122-
g.Expect(c.Get(ctx, types.NamespacedName{Name: "testcb", Namespace: "default"}, cb)).Should(gomega.BeNil())
123-
g.Expect(cb.Status.TargetStatus).ShouldNot(gomega.BeNil())
121+
g.Eventually(func() bool {
122+
if err := c.Get(ctx, types.NamespacedName{Name: "testcb", Namespace: "default"}, cb); err != nil {
123+
return false
124+
}
125+
return cb.Status.TargetStatus != nil
126+
}, 5*time.Second, 1*time.Second).Should(gomega.BeTrue())
124127
// pod is not available
125128
g.Expect(strings.Contains(cb.Status.TargetStatus[0].Message, "not available")).Should(gomega.BeTrue())
126129
testPod.Status = *mockPod.Status.DeepCopy()
130+
127131
g.Expect(c.Status().Update(ctx, testPod)).Should(gomega.BeNil())
128-
waitProcess()
129-
g.Expect(c.Get(ctx, types.NamespacedName{Name: "testcb", Namespace: "default"}, cb)).Should(gomega.BeNil())
130-
g.Expect(cb.Status.TargetStatus).ShouldNot(gomega.BeNil())
131-
// pod is available
132-
g.Expect(cb.Status.TargetStatus[0].PodIP).Should(gomega.BeEquivalentTo("127.0.0.1"))
132+
g.Eventually(func() string {
133+
g.Expect(c.Get(ctx, types.NamespacedName{Name: "testcb", Namespace: "default"}, cb)).Should(gomega.BeNil())
134+
if cb.Status.TargetStatus != nil {
135+
return cb.Status.TargetStatus[0].PodIP
136+
}
137+
return ""
138+
}, 5*time.Second, 1*time.Second).Should(gomega.Equal("127.0.0.1"))
133139
g.Expect(len(cb.Finalizers) > 0).Should(gomega.BeTrue())
134140
cb.Spec.TrafficInterceptRules = append(cb.Spec.TrafficInterceptRules, &ctrlmeshv1alpha1.TrafficInterceptRule{
135141
Name: "testIntercept",
@@ -142,26 +148,35 @@ func TestCircuitBreaker(t *testing.T) {
142148
"GET",
143149
},
144150
})
151+
localCount := syncCount
145152
g.Expect(c.Update(ctx, cb)).Should(gomega.BeNil())
146-
waitProcess()
147-
g.Expect(c.Get(ctx, types.NamespacedName{Name: "testcb", Namespace: "default"}, cb)).Should(gomega.BeNil())
153+
g.Eventually(func() bool {
154+
g.Expect(c.Get(ctx, types.NamespacedName{Name: "testcb", Namespace: "default"}, cb)).Should(gomega.BeNil())
155+
if len(cb.Spec.TrafficInterceptRules) != 0 && cb.Spec.TrafficInterceptRules[0].Name == "testIntercept" {
156+
return cb.Status.ObservedGeneration == cb.Generation && syncCount != localCount
157+
}
158+
return false
159+
}, 5*time.Second, 1*time.Second).Should(gomega.BeTrue())
148160
g.Expect(breakerManager.ValidateTrafficIntercept("aaa.aaa.aaa", "GET").Allowed).Should(gomega.BeFalse())
149161
if cb.Labels == nil {
150162
cb.Labels = map[string]string{}
151163
}
152164
cb.Labels[ctrlmesh.CtrlmeshCircuitBreakerDisableKey] = "true"
165+
localCount = syncCount
153166
g.Expect(c.Update(ctx, cb)).Should(gomega.BeNil())
154-
waitProcess()
155-
g.Expect(c.Get(ctx, types.NamespacedName{Name: "testcb", Namespace: "default"}, cb)).Should(gomega.BeNil())
156-
g.Expect(len(cb.Status.TargetStatus) == 0).Should(gomega.BeTrue())
157-
g.Expect(len(cb.Finalizers) == 0).Should(gomega.BeTrue())
167+
g.Eventually(func() bool {
168+
g.Expect(c.Get(ctx, types.NamespacedName{Name: "testcb", Namespace: "default"}, cb)).Should(gomega.BeNil())
169+
return len(cb.Status.TargetStatus) == 0 && len(cb.Finalizers) == 0 && syncCount != localCount
170+
}, 5*time.Second, 1*time.Second).Should(gomega.BeTrue())
158171
g.Expect(c.Delete(ctx, testBreaker)).Should(gomega.BeNil())
159-
waitProcess()
160-
g.Expect(c.Get(ctx, types.NamespacedName{Name: "testcb", Namespace: "default"}, cb)).Should(gomega.HaveOccurred())
172+
g.Eventually(func() error {
173+
return c.Get(ctx, types.NamespacedName{Name: "testcb", Namespace: "default"}, cb)
174+
}, 5*time.Second, 1*time.Second).Should(gomega.HaveOccurred())
161175
fmt.Println("test finished")
162176
}
163177

164178
var breakerManager circuitbreaker.ManagerInterface
179+
var syncCount int
165180

166181
func RunMockServer() {
167182
breakerMgr := circuitbreaker.NewManager(ctx)
@@ -179,6 +194,7 @@ func (m *mockBreakerManager) Sync(config *ctrlmeshproto.CircuitBreaker) (*ctrlme
179194
resp, err := m.ManagerInterface.Sync(config)
180195
utilruntime.Must(err)
181196
printJson(resp)
197+
syncCount++
182198
return resp, err
183199
}
184200

0 commit comments

Comments
 (0)