cluster_test.go 2.4 KB
Newer Older
S
sunby 已提交
1 2 3 4 5 6
package dataservice

import (
	"testing"

	"github.com/stretchr/testify/assert"
S
sunby 已提交
7 8
	"github.com/zilliztech/milvus-distributed/internal/proto/internalpb"
	"golang.org/x/net/context"
S
sunby 已提交
9 10
)

S
sunby 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
func TestDataNodeClusterRegister(t *testing.T) {
	Params.Init()
	Params.DataNodeNum = 3
	ch := make(chan struct{})
	cluster := newDataNodeCluster(ch)
	ids := make([]int64, 0, Params.DataNodeNum)
	for i := 0; i < Params.DataNodeNum; i++ {
		c := newMockDataNodeClient(int64(i))
		err := c.Init()
		assert.Nil(t, err)
		err = c.Start()
		assert.Nil(t, err)
		cluster.Register(&dataNode{
			id: int64(i),
			address: struct {
				ip   string
				port int64
			}{"localhost", int64(9999 + i)},
			client:     c,
			channelNum: 0,
		})
		ids = append(ids, int64(i))
	}
	_, ok := <-ch
	assert.False(t, ok)
	assert.EqualValues(t, Params.DataNodeNum, cluster.GetNumOfNodes())
	assert.EqualValues(t, ids, cluster.GetNodeIDs())
	states, err := cluster.GetDataNodeStates(context.TODO())
	assert.Nil(t, err)
	assert.EqualValues(t, Params.DataNodeNum, len(states))
	for _, s := range states {
		assert.EqualValues(t, internalpb.StateCode_Healthy, s.StateCode)
	}
	cluster.ShutDownClients()
	states, err = cluster.GetDataNodeStates(context.TODO())
	assert.Nil(t, err)
	assert.EqualValues(t, Params.DataNodeNum, len(states))
	for _, s := range states {
		assert.EqualValues(t, internalpb.StateCode_Abnormal, s.StateCode)
	}
}

S
sunby 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
func TestWatchChannels(t *testing.T) {
	Params.Init()
	Params.DataNodeNum = 3
	cases := []struct {
		collectionID UniqueID
		channels     []string
		channelNums  []int
	}{
		{1, []string{"c1"}, []int{1, 0, 0}},
		{1, []string{"c1", "c2", "c3"}, []int{1, 1, 1}},
		{1, []string{"c1", "c2", "c3", "c4"}, []int{2, 1, 1}},
		{1, []string{"c1", "c2", "c3", "c4", "c5", "c6", "c7"}, []int{3, 2, 2}},
	}

	cluster := newDataNodeCluster(make(chan struct{}))
	for _, c := range cases {
		for i := 0; i < Params.DataNodeNum; i++ {
S
sunby 已提交
70 71 72 73 74
			c := newMockDataNodeClient(int64(i))
			err := c.Init()
			assert.Nil(t, err)
			err = c.Start()
			assert.Nil(t, err)
S
sunby 已提交
75 76 77 78 79 80
			cluster.Register(&dataNode{
				id: int64(i),
				address: struct {
					ip   string
					port int64
				}{"localhost", int64(9999 + i)},
S
sunby 已提交
81
				client:     c,
S
sunby 已提交
82 83 84
				channelNum: 0,
			})
		}
85
		cluster.WatchInsertChannels(c.channels)
S
sunby 已提交
86 87 88 89 90 91
		for i := 0; i < len(cluster.nodes); i++ {
			assert.EqualValues(t, c.channelNums[i], cluster.nodes[i].channelNum)
		}
		cluster.Clear()
	}
}