...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/clientpool/client_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/clientpool

     1  // Copyright 2021 Chaos Mesh 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  
    16  package clientpool
    17  
    18  import (
    19  	"strconv"
    20  	"testing"
    21  
    22  	. "github.com/onsi/gomega"
    23  	"k8s.io/apimachinery/pkg/runtime"
    24  	"k8s.io/client-go/rest"
    25  	pkgclient "sigs.k8s.io/controller-runtime/pkg/client"
    26  
    27  	"github.com/chaos-mesh/chaos-mesh/pkg/mock"
    28  )
    29  
    30  func TestClientPool(t *testing.T) {
    31  	g := NewWithT(t)
    32  
    33  	t.Run("client pool", func(t *testing.T) {
    34  		defer mock.With("MockCreateK8sClient", func(config *rest.Config, options pkgclient.Options) (pkgclient.Client, error) {
    35  			return nil, nil
    36  		})()
    37  
    38  		k8sClients, err := NewClientPool(&rest.Config{}, &runtime.Scheme{}, 5)
    39  		g.Expect(err).ToNot(HaveOccurred())
    40  
    41  		for i := 0; i < 6; i++ {
    42  			_, err := k8sClients.Client(strconv.Itoa(i))
    43  			g.Expect(err).ToNot(HaveOccurred())
    44  		}
    45  
    46  		// remain key 1, 2, 3, 4, 5 in cache, 0 is evicted
    47  		g.Expect(k8sClients.Num()).To(Equal(5))
    48  
    49  		_, err = k8sClients.Client("6")
    50  		g.Expect(err).ToNot(HaveOccurred())
    51  
    52  		// 6 in cache, and 1 is evict because it is the oldest key which is not used recently
    53  		g.Expect(k8sClients.Contains("6")).To(Equal(true))
    54  		g.Expect(k8sClients.Contains("1")).To(Equal(false))
    55  	})
    56  }
    57