istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/config/memory/controller_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/atomic"
    19  	"testing"
    20  
    21  	"istio.io/istio/pilot/pkg/config/memory"
    22  	"istio.io/istio/pilot/test/mock"
    23  	"istio.io/istio/pkg/config/schema/collections"
    24  )
    25  
    26  const (
    27  	// TestNamespace specifies the namespace for testing
    28  	TestNamespace = "istio-memory-test"
    29  )
    30  
    31  func TestControllerEvents(t *testing.T) {
    32  	store := memory.Make(collections.Mocks)
    33  	ctl := memory.NewController(store)
    34  	// Note that the operations must go through the controller since the store does not trigger back events
    35  	mock.CheckCacheEvents(ctl, ctl, TestNamespace, 5, t)
    36  }
    37  
    38  func TestControllerCacheFreshness(t *testing.T) {
    39  	store := memory.Make(collections.Mocks)
    40  	ctl := memory.NewController(store)
    41  	mock.CheckCacheFreshness(ctl, TestNamespace, t)
    42  }
    43  
    44  func TestControllerClientSync(t *testing.T) {
    45  	store := memory.Make(collections.Mocks)
    46  	ctl := memory.NewController(store)
    47  	mock.CheckCacheSync(store, ctl, TestNamespace, 5, t)
    48  }
    49  
    50  func TestControllerHashSynced(t *testing.T) {
    51  	store := memory.Make(collections.Mocks)
    52  	var v int32
    53  	ctl := memory.NewController(store)
    54  
    55  	ctl.RegisterHasSyncedHandler(func() bool {
    56  		return atomic.LoadInt32(&v) > 0
    57  	})
    58  
    59  	if ctl.HasSynced() {
    60  		t.Error("has synced but should not")
    61  	}
    62  	atomic.StoreInt32(&v, 1)
    63  	if !ctl.HasSynced() {
    64  		t.Error("has not synced but should")
    65  	}
    66  }