istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/config/memory/monitor_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 memory_test 16 17 import ( 18 "sync" 19 "testing" 20 21 "istio.io/istio/pilot/pkg/config/memory" 22 "istio.io/istio/pilot/pkg/model" 23 "istio.io/istio/pilot/test/mock" 24 "istio.io/istio/pkg/config" 25 "istio.io/istio/pkg/config/schema/collections" 26 ) 27 28 func TestMonitorLifecycle(t *testing.T) { 29 // Regression test to ensure no race conditions during monitor shutdown 30 store := memory.Make(collections.Mocks) 31 m := memory.NewMonitor(store) 32 stop := make(chan struct{}) 33 go m.Run(stop) 34 m.ScheduleProcessEvent(memory.ConfigEvent{}) 35 close(stop) 36 m.ScheduleProcessEvent(memory.ConfigEvent{}) 37 } 38 39 func TestEventConsistency(t *testing.T) { 40 store := memory.Make(collections.Mocks) 41 controller := memory.NewController(store) 42 43 testConfig := mock.Make(TestNamespace, 0) 44 var testEvent model.Event 45 46 done := make(chan bool) 47 48 lock := sync.Mutex{} 49 50 controller.RegisterEventHandler(collections.Mock.GroupVersionKind(), func(_, config config.Config, event model.Event) { 51 lock.Lock() 52 tc := testConfig 53 lock.Unlock() 54 55 if event != testEvent { 56 t.Errorf("desired %v, but %v", testEvent, event) 57 } 58 if !mock.Compare(tc, config) { 59 t.Errorf("desired %v, but %v", tc, config) 60 } 61 done <- true 62 }) 63 64 stop := make(chan struct{}) 65 go controller.Run(stop) 66 67 // Test Add Event 68 testEvent = model.EventAdd 69 var rev string 70 var err error 71 if rev, err = controller.Create(testConfig); err != nil { 72 t.Error(err) 73 return 74 } 75 76 lock.Lock() 77 testConfig.ResourceVersion = rev 78 lock.Unlock() 79 80 <-done 81 82 // Test Update Event 83 testEvent = model.EventUpdate 84 if _, err := controller.Update(testConfig); err != nil { 85 t.Error(err) 86 return 87 } 88 <-done 89 90 // Test Delete Event 91 testEvent = model.EventDelete 92 if err := controller.Delete(collections.Mock.GroupVersionKind(), testConfig.Name, TestNamespace, nil); err != nil { 93 t.Error(err) 94 return 95 } 96 <-done 97 close(stop) 98 }