Merge pull request #3862 from ikaven1024/fix-ut

fix ut in TestMultiClusterCache watch
This commit is contained in:
karmada-bot 2023-07-29 15:11:40 +08:00 committed by GitHub
commit 04d2ef17d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 15 deletions

View File

@ -932,10 +932,10 @@ func TestMultiClusterCache_Watch(t *testing.T) {
want want want want
}{ }{
{ {
name: "resource version is empty", name: "resource version is zero",
args: args{ args: args{
options: &metainternalversion.ListOptions{ options: &metainternalversion.ListOptions{
ResourceVersion: "", ResourceVersion: "0",
}, },
}, },
want: want{ want: want{
@ -943,10 +943,10 @@ func TestMultiClusterCache_Watch(t *testing.T) {
}, },
}, },
{ {
name: "resource version of cluster2 is empty", name: "resource version of cluster2 is zero",
args: args{ args: args{
options: &metainternalversion.ListOptions{ options: &metainternalversion.ListOptions{
ResourceVersion: buildMultiClusterRV(cluster1.Name, "1002"), ResourceVersion: buildMultiClusterRV(cluster1.Name, "1002", cluster2.Name, "0"),
}, },
}, },
want: want{ want: want{
@ -954,7 +954,7 @@ func TestMultiClusterCache_Watch(t *testing.T) {
}, },
}, },
{ {
name: "resource versions are not empty", name: "resource versions are not zero",
args: args{ args: args{
options: &metainternalversion.ListOptions{ options: &metainternalversion.ListOptions{
ResourceVersion: buildMultiClusterRV(cluster1.Name, "1002", cluster2.Name, "2002"), ResourceVersion: buildMultiClusterRV(cluster1.Name, "1002", cluster2.Name, "2002"),
@ -968,7 +968,7 @@ func TestMultiClusterCache_Watch(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(request.WithNamespace(context.TODO(), metav1.NamespaceDefault), time.Second) ctx, cancel := context.WithCancel(request.WithNamespace(context.TODO(), metav1.NamespaceDefault))
defer cancel() defer cancel()
watcher, err := cache.Watch(ctx, podGVR, tt.args.options) watcher, err := cache.Watch(ctx, podGVR, tt.args.options)
if err != nil { if err != nil {
@ -976,7 +976,7 @@ func TestMultiClusterCache_Watch(t *testing.T) {
return return
} }
defer watcher.Stop() defer watcher.Stop()
timeout := time.After(time.Second * 5) waitCh := time.After(time.Second)
gets := sets.New[string]() gets := sets.New[string]()
LOOP: LOOP:
@ -990,9 +990,8 @@ func TestMultiClusterCache_Watch(t *testing.T) {
if err == nil { if err == nil {
gets.Insert(accessor.GetName()) gets.Insert(accessor.GetName())
} }
case <-timeout: case <-waitCh:
t.Error("timeout") break LOOP
return
} }
} }
@ -1101,15 +1100,15 @@ func TestMultiClusterCache_Watch_Namespaced(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(request.WithNamespace(context.TODO(), tt.args.ns), time.Second) ctx, cancel := context.WithCancel(request.WithNamespace(context.TODO(), tt.args.ns))
defer cancel() defer cancel()
watcher, err := cache.Watch(ctx, podGVR, &metainternalversion.ListOptions{}) watcher, err := cache.Watch(ctx, podGVR, &metainternalversion.ListOptions{ResourceVersion: "0"})
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
} }
defer watcher.Stop() defer watcher.Stop()
timeout := time.After(time.Second * 5) waitCh := time.After(time.Second)
gots := sets.New[string]() gots := sets.New[string]()
LOOP: LOOP:
@ -1123,8 +1122,8 @@ func TestMultiClusterCache_Watch_Namespaced(t *testing.T) {
if err == nil { if err == nil {
gots.Insert(accessor.GetName()) gots.Insert(accessor.GetName())
} }
case <-timeout: case <-waitCh:
t.Fatal("timeout") break LOOP
} }
} }