...

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