github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/utilcomponent/periodicalhandler/periodical_handler_test.go (about)

     1  /*
     2  Copyright 2022 The Katalyst Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package periodicalhandler
    18  
    19  import (
    20  	"context"
    21  	"sync"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  	"github.com/stretchr/testify/require"
    27  	"k8s.io/apimachinery/pkg/runtime"
    28  
    29  	katalystbase "github.com/kubewharf/katalyst-core/cmd/base"
    30  	"github.com/kubewharf/katalyst-core/cmd/katalyst-agent/app/agent"
    31  	"github.com/kubewharf/katalyst-core/pkg/config"
    32  	configagent "github.com/kubewharf/katalyst-core/pkg/config/agent"
    33  	dynamicconfig "github.com/kubewharf/katalyst-core/pkg/config/agent/dynamic"
    34  	"github.com/kubewharf/katalyst-core/pkg/metaserver"
    35  	metaserveragent "github.com/kubewharf/katalyst-core/pkg/metaserver/agent"
    36  	"github.com/kubewharf/katalyst-core/pkg/metaserver/agent/pod"
    37  	"github.com/kubewharf/katalyst-core/pkg/metaserver/external"
    38  	"github.com/kubewharf/katalyst-core/pkg/metrics"
    39  	"github.com/kubewharf/katalyst-core/pkg/util/machine"
    40  )
    41  
    42  func makeTestGenericContext(t *testing.T) *agent.GenericContext {
    43  	genericCtx, err := katalystbase.GenerateFakeGenericContext([]runtime.Object{})
    44  	assert.NoError(t, err)
    45  
    46  	return &agent.GenericContext{
    47  		GenericContext: genericCtx,
    48  		MetaServer:     makeMetaServer(),
    49  		PluginManager:  nil,
    50  	}
    51  }
    52  
    53  func makeMetaServer() *metaserver.MetaServer {
    54  	cpuTopology, _ := machine.GenerateDummyCPUTopology(16, 2, 4)
    55  
    56  	return &metaserver.MetaServer{
    57  		MetaAgent: &metaserveragent.MetaAgent{
    58  			KatalystMachineInfo: &machine.KatalystMachineInfo{
    59  				CPUTopology:      cpuTopology,
    60  				ExtraNetworkInfo: &machine.ExtraNetworkInfo{},
    61  			},
    62  		},
    63  		ExternalManager: external.InitExternalManager(&pod.PodFetcherStub{}),
    64  	}
    65  }
    66  
    67  func TestPeriodicalHandlerManager(t *testing.T) {
    68  	t.Parallel()
    69  	as := require.New(t)
    70  
    71  	var lock sync.RWMutex
    72  	agentCtx := makeTestGenericContext(t)
    73  	_, pmgr, _ := NewPeriodicalHandlerManager(agentCtx, &config.Configuration{AgentConfiguration: &configagent.AgentConfiguration{}}, nil, "test_periodical_handler_mgr")
    74  
    75  	rctx, cancel := context.WithCancel(context.Background())
    76  	var wg sync.WaitGroup
    77  	wg.Add(1)
    78  	go func() {
    79  		pmgr.Run(rctx)
    80  		wg.Done()
    81  	}()
    82  
    83  	testNum := 5
    84  	testNum1 := 1
    85  
    86  	gName := "test_group"
    87  	hName := "test_handler"
    88  	_ = RegisterPeriodicalHandler(gName, hName, func(coreConf *config.Configuration,
    89  		extraConf interface{},
    90  		dynamicConf *dynamicconfig.DynamicAgentConfiguration,
    91  		emitter metrics.MetricEmitter,
    92  		metaServer *metaserver.MetaServer,
    93  	) {
    94  		lock.Lock()
    95  		testNum = 10
    96  		lock.Unlock()
    97  	},
    98  		time.Second)
    99  
   100  	hName1 := "test_handler1"
   101  	_ = RegisterPeriodicalHandler(gName, hName1, func(coreConf *config.Configuration,
   102  		extraConf interface{},
   103  		dynamicConf *dynamicconfig.DynamicAgentConfiguration,
   104  		emitter metrics.MetricEmitter,
   105  		metaServer *metaserver.MetaServer,
   106  	) {
   107  		lock.Lock()
   108  		testNum1 = 2
   109  		lock.Unlock()
   110  	},
   111  		time.Second)
   112  
   113  	ticker := time.After(2 * time.Second)
   114  
   115  testLoop1:
   116  	for {
   117  		lock.RLock()
   118  		as.Equal(5, testNum)
   119  		as.Equal(1, testNum1)
   120  		lock.RUnlock()
   121  		select {
   122  		case <-ticker:
   123  			break testLoop1
   124  		default:
   125  		}
   126  		time.Sleep(time.Second)
   127  	}
   128  
   129  	ReadyToStartHandlersByGroup(gName)
   130  
   131  	ticker = time.After(10 * time.Second)
   132  
   133  testLoop2:
   134  	for {
   135  		lock.RLock()
   136  		if testNum == 10 && testNum1 == 2 {
   137  			lock.RUnlock()
   138  			break
   139  		}
   140  		lock.RUnlock()
   141  
   142  		select {
   143  		case <-ticker:
   144  			break testLoop2
   145  		default:
   146  		}
   147  		time.Sleep(time.Second)
   148  	}
   149  
   150  	lock.RLock()
   151  	as.Equal(10, testNum)
   152  	as.Equal(2, testNum1)
   153  	lock.RUnlock()
   154  	cancel()
   155  }