Skip to content

Commit

Permalink
test: add more unit test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
xyombo committed Jul 31, 2023
1 parent a217d9b commit 872bafd
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions pkg/remoting/loadbalance/random_loadbalance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package loadbalance

import (
"fmt"
"sync"
"testing"

Expand All @@ -26,30 +27,49 @@ import (
"github.com/stretchr/testify/assert"
)

func TestRandomLoadBalance(t *testing.T) {
func TestRandomLoadBalance_Nomal(t *testing.T) {
ctrl := gomock.NewController(t)
// defer ctrl.Finish()
sessions := &sync.Map{}

//mock two opening session
openSession := mock.NewMockTestSession(ctrl)
openSession.EXPECT().IsClosed().Return(false).AnyTimes()
for i := 0; i < 3; i++ {
session := mock.NewMockTestSession(ctrl)
session.EXPECT().IsClosed().Return(i == 2).AnyTimes()
sessions.Store(session, fmt.Sprintf("session-%d", (i+1)))
}
result := RandomLoadBalance(sessions, "some_xid")

//mock two opening session
openSession2 := mock.NewMockTestSession(ctrl)
openSession2.EXPECT().IsClosed().Return(false).AnyTimes()
assert.NotNil(t, result)
//assert random load balance return session not closed
assert.False(t, result.IsClosed())
}

//mock one closed session
closeSession := mock.NewMockTestSession(ctrl)
closeSession.EXPECT().IsClosed().Return(true).AnyTimes()
func TestRandomLoadBalance_All_Closed(t *testing.T) {

sessions.Store(openSession, "session1")
sessions.Store(closeSession, "session2")
sessions.Store(openSession2, "session3")
ctrl := gomock.NewController(t)
sessions := &sync.Map{}

//mock closed sessions
for i := 0; i < 10; i++ {
session := mock.NewMockTestSession(ctrl)
session.EXPECT().IsClosed().Return(true).AnyTimes()
sessions.Store(session, fmt.Sprintf("session-%d", (i+1)))
}
result := RandomLoadBalance(sessions, "some_xid")

assert.NotNil(t, result)
//assert random load balance return session not closed
assert.True(t, result.IsClosed(), "found one un-closed session instance in ALL_CLOSED_SESSION_MAP")
}

func TestRandomLoadBalance_All_Opening(t *testing.T) {

ctrl := gomock.NewController(t)
sessions := &sync.Map{}
for i := 0; i < 10; i++ {
session := mock.NewMockTestSession(ctrl)
session.EXPECT().IsClosed().Return(false).AnyTimes()
sessions.Store(session, fmt.Sprintf("session-%d", (i+1)))
}
result := RandomLoadBalance(sessions, "some_xid")
//assert return session is not closed
assert.False(t, result.IsClosed())
}

0 comments on commit 872bafd

Please sign in to comment.