@@ -117,19 +117,25 @@ func TestCircuitBreaker(t *testing.T) {
117
117
defer func () {
118
118
c .Delete (ctx , testPod )
119
119
}()
120
- waitProcess ()
121
120
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 ())
124
127
// pod is not available
125
128
g .Expect (strings .Contains (cb .Status .TargetStatus [0 ].Message , "not available" )).Should (gomega .BeTrue ())
126
129
testPod .Status = * mockPod .Status .DeepCopy ()
130
+
127
131
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" ))
133
139
g .Expect (len (cb .Finalizers ) > 0 ).Should (gomega .BeTrue ())
134
140
cb .Spec .TrafficInterceptRules = append (cb .Spec .TrafficInterceptRules , & ctrlmeshv1alpha1.TrafficInterceptRule {
135
141
Name : "testIntercept" ,
@@ -142,26 +148,35 @@ func TestCircuitBreaker(t *testing.T) {
142
148
"GET" ,
143
149
},
144
150
})
151
+ localCount := syncCount
145
152
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 ())
148
160
g .Expect (breakerManager .ValidateTrafficIntercept ("aaa.aaa.aaa" , "GET" ).Allowed ).Should (gomega .BeFalse ())
149
161
if cb .Labels == nil {
150
162
cb .Labels = map [string ]string {}
151
163
}
152
164
cb .Labels [ctrlmesh .CtrlmeshCircuitBreakerDisableKey ] = "true"
165
+ localCount = syncCount
153
166
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 ())
158
171
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 ())
161
175
fmt .Println ("test finished" )
162
176
}
163
177
164
178
var breakerManager circuitbreaker.ManagerInterface
179
+ var syncCount int
165
180
166
181
func RunMockServer () {
167
182
breakerMgr := circuitbreaker .NewManager (ctx )
@@ -179,6 +194,7 @@ func (m *mockBreakerManager) Sync(config *ctrlmeshproto.CircuitBreaker) (*ctrlme
179
194
resp , err := m .ManagerInterface .Sync (config )
180
195
utilruntime .Must (err )
181
196
printJson (resp )
197
+ syncCount ++
182
198
return resp , err
183
199
}
184
200
0 commit comments