|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package loadbalance |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "strconv" |
| 23 | + "sync" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/golang/mock/gomock" |
| 27 | + "github.com/seata/seata-go/pkg/remoting/mock" |
| 28 | + "github.com/seata/seata-go/pkg/remoting/rpc" |
| 29 | + "github.com/stretchr/testify/assert" |
| 30 | +) |
| 31 | + |
| 32 | +func TestLeastActiveLoadBalance(t *testing.T) { |
| 33 | + ctrl := gomock.NewController(t) |
| 34 | + sessions := &sync.Map{} |
| 35 | + |
| 36 | + for i := 1; i <= 3; i++ { |
| 37 | + session := mock.NewMockTestSession(ctrl) |
| 38 | + session.EXPECT().IsClosed().Return(false).AnyTimes() |
| 39 | + addr := "127.0.0." + strconv.Itoa(i) + ":8000" |
| 40 | + session.EXPECT().RemoteAddr().AnyTimes().DoAndReturn(func() string { |
| 41 | + return addr |
| 42 | + }) |
| 43 | + sessions.Store(session, fmt.Sprintf("session-%d", i)) |
| 44 | + rpc.BeginCount(addr) |
| 45 | + } |
| 46 | + |
| 47 | + session := mock.NewMockTestSession(ctrl) |
| 48 | + session.EXPECT().IsClosed().Return(true).AnyTimes() |
| 49 | + addr := "127.0.0.5:8000" |
| 50 | + session.EXPECT().RemoteAddr().AnyTimes().DoAndReturn(func() string { |
| 51 | + return addr |
| 52 | + }) |
| 53 | + sessions.Store(session, "session-5") |
| 54 | + rpc.BeginCount(addr) |
| 55 | + |
| 56 | + countTwo := "127.0.0.1:8000" |
| 57 | + rpc.BeginCount(countTwo) |
| 58 | + |
| 59 | + result := LeastActiveLoadBalance(sessions, "test_xid") |
| 60 | + assert.False(t, result.RemoteAddr() == countTwo) |
| 61 | + assert.False(t, result.RemoteAddr() == addr) |
| 62 | + assert.False(t, result.IsClosed()) |
| 63 | +} |
0 commit comments