2023-06-13 10:20:37 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
// "License"); you may not use this file except in compliance
|
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
2023-06-27 09:52:44 +08:00
|
|
|
"context"
|
2023-06-13 10:20:37 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RoundRobinBalancerSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
balancer *RoundRobinBalancer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RoundRobinBalancerSuite) SetupTest() {
|
|
|
|
s.balancer = NewRoundRobinBalancer()
|
2023-06-27 09:52:44 +08:00
|
|
|
s.balancer.Start(context.Background())
|
2023-06-13 10:20:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RoundRobinBalancerSuite) TestRoundRobin() {
|
|
|
|
availableNodes := []int64{1, 2}
|
2023-06-27 09:52:44 +08:00
|
|
|
s.balancer.SelectNode(context.TODO(), availableNodes, 1)
|
|
|
|
s.balancer.SelectNode(context.TODO(), availableNodes, 1)
|
|
|
|
s.balancer.SelectNode(context.TODO(), availableNodes, 1)
|
|
|
|
s.balancer.SelectNode(context.TODO(), availableNodes, 1)
|
2023-06-13 10:20:37 +08:00
|
|
|
|
2023-06-16 18:38:39 +08:00
|
|
|
workload, ok := s.balancer.nodeWorkload.Get(1)
|
|
|
|
s.True(ok)
|
|
|
|
s.Equal(int64(2), workload.Load())
|
|
|
|
workload, ok = s.balancer.nodeWorkload.Get(1)
|
|
|
|
s.True(ok)
|
|
|
|
s.Equal(int64(2), workload.Load())
|
2023-06-13 10:20:37 +08:00
|
|
|
|
2023-06-27 09:52:44 +08:00
|
|
|
s.balancer.SelectNode(context.TODO(), availableNodes, 3)
|
|
|
|
s.balancer.SelectNode(context.TODO(), availableNodes, 1)
|
|
|
|
s.balancer.SelectNode(context.TODO(), availableNodes, 1)
|
|
|
|
s.balancer.SelectNode(context.TODO(), availableNodes, 1)
|
2023-06-13 10:20:37 +08:00
|
|
|
|
2023-06-16 18:38:39 +08:00
|
|
|
workload, ok = s.balancer.nodeWorkload.Get(1)
|
|
|
|
s.True(ok)
|
|
|
|
s.Equal(int64(5), workload.Load())
|
|
|
|
workload, ok = s.balancer.nodeWorkload.Get(1)
|
|
|
|
s.True(ok)
|
|
|
|
s.Equal(int64(5), workload.Load())
|
2023-06-13 10:20:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RoundRobinBalancerSuite) TestNoAvailableNode() {
|
|
|
|
availableNodes := []int64{}
|
2023-06-27 09:52:44 +08:00
|
|
|
_, err := s.balancer.SelectNode(context.TODO(), availableNodes, 1)
|
2023-06-13 10:20:37 +08:00
|
|
|
s.Error(err)
|
|
|
|
}
|
|
|
|
|
2023-06-16 18:38:39 +08:00
|
|
|
func (s *RoundRobinBalancerSuite) TestCancelWorkload() {
|
|
|
|
availableNodes := []int64{101}
|
2023-06-27 09:52:44 +08:00
|
|
|
_, err := s.balancer.SelectNode(context.TODO(), availableNodes, 5)
|
2023-06-16 18:38:39 +08:00
|
|
|
s.NoError(err)
|
|
|
|
workload, ok := s.balancer.nodeWorkload.Get(101)
|
|
|
|
s.True(ok)
|
|
|
|
s.Equal(int64(5), workload.Load())
|
|
|
|
s.balancer.CancelWorkload(101, 5)
|
|
|
|
s.Equal(int64(0), workload.Load())
|
|
|
|
}
|
|
|
|
|
2023-06-13 10:20:37 +08:00
|
|
|
func TestRoundRobinBalancerSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(RoundRobinBalancerSuite))
|
|
|
|
}
|