istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/status/resourcelock_test.go (about) 1 // Copyright Istio Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package status 16 17 import ( 18 "context" 19 "sync/atomic" 20 "testing" 21 22 . "github.com/onsi/gomega" 23 "k8s.io/apimachinery/pkg/runtime/schema" 24 25 "istio.io/api/meta/v1alpha1" 26 "istio.io/istio/pkg/config" 27 ) 28 29 func TestResourceLock_Lock(t *testing.T) { 30 g := NewGomegaWithT(t) 31 r1 := Resource{ 32 GroupVersionResource: schema.GroupVersionResource{ 33 Group: "r1", 34 Version: "r1", 35 }, 36 Namespace: "r1", 37 Name: "r1", 38 Generation: "11", 39 } 40 r1a := Resource{ 41 GroupVersionResource: schema.GroupVersionResource{ 42 Group: "r1", 43 Version: "r1", 44 }, 45 Namespace: "r1", 46 Name: "r1", 47 Generation: "12", 48 } 49 var runCount int32 50 x := make(chan struct{}) 51 y := make(chan struct{}) 52 mgr := NewManager(nil) 53 fakefunc := func(status *v1alpha1.IstioStatus, context any) *v1alpha1.IstioStatus { 54 x <- struct{}{} 55 atomic.AddInt32(&runCount, 1) 56 y <- struct{}{} 57 return nil 58 } 59 c1 := mgr.CreateIstioStatusController(fakefunc) 60 c2 := mgr.CreateIstioStatusController(fakefunc) 61 workers := NewWorkerPool(func(_ *config.Config, _ any) { 62 }, func(resource Resource) *config.Config { 63 return &config.Config{ 64 Meta: config.Meta{Generation: 11}, 65 } 66 }, 10) 67 ctx, cancel := context.WithCancel(context.Background()) 68 workers.Run(ctx) 69 workers.Push(r1, c1, nil) 70 workers.Push(r1, c2, nil) 71 workers.Push(r1, c1, nil) 72 <-x 73 <-y 74 <-x 75 workers.Push(r1, c1, nil) 76 workers.Push(r1a, c1, nil) 77 <-y 78 <-x 79 select { 80 case <-x: 81 t.FailNow() 82 default: 83 } 84 <-y 85 result := atomic.LoadInt32(&runCount) 86 g.Expect(result).To(Equal(int32(3))) 87 cancel() 88 }