rocketmq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From huzongt...@apache.org
Subject [rocketmq-client-go] branch native updated: [ISSUE #75] Add internal/route.go unit tests (#116)
Date Mon, 15 Jul 2019 01:05:20 GMT
This is an automated email from the ASF dual-hosted git repository.

huzongtang pushed a commit to branch native
in repository https://gitbox.apache.org/repos/asf/rocketmq-client-go.git


The following commit(s) were added to refs/heads/native by this push:
     new 25f4036  [ISSUE #75] Add internal/route.go unit tests (#116)
25f4036 is described below

commit 25f403678ec98ca65678b89bdb4b40224bef68dd
Author: 兰园望月 <1376030681@qq.com>
AuthorDate: Mon Jul 15 09:05:16 2019 +0800

    [ISSUE #75] Add internal/route.go unit tests (#116)
    
    * add route test
    
    * test AddBroker and repair encodeMessages
---
 internal/client.go     |   1 +
 internal/route_test.go | 111 ++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 111 insertions(+), 1 deletion(-)

diff --git a/internal/client.go b/internal/client.go
index 09b7ca3..35ad9e7 100644
--- a/internal/client.go
+++ b/internal/client.go
@@ -600,6 +600,7 @@ func encodeMessages(message []*primitive.Message) []byte {
 	index := 0
 	for index < len(message) {
 		buffer.Write(message[index].Body)
+		index ++
 	}
 	return buffer.Bytes()
 }
diff --git a/internal/route_test.go b/internal/route_test.go
index 7bc0846..ee95755 100644
--- a/internal/route_test.go
+++ b/internal/route_test.go
@@ -18,9 +18,118 @@ limitations under the License.
 package internal
 
 import (
+	. "github.com/smartystreets/goconvey/convey"
 	"testing"
 )
 
+const (
+	topic = "TopicTest"
+)
+
+func init() {
+	srvs := []string{"127.0.0.1:9876"}
+	namesrv, err := NewNamesrv(srvs...)
+	if err != nil {
+		panic("register namesrv fail")
+	}
+	RegisterNamsrv(namesrv)
+}
+
+func TestAddBroker(t *testing.T) {
+	Convey("Given a starting topic", t, func() {
+		remoteRouteData, err := queryTopicRouteInfoFromServer(topic)
+		So(err, ShouldBeNil)
+		AddBroker(remoteRouteData)
+
+		Convey("brokerData from brokerAddressesMap by brokeName should be deep equal remoteBrokeData
from server", func() {
+			for _, remoteBrokerData := range remoteRouteData.BrokerDataList {
+				brokerName := remoteBrokerData.BrokerName
+				brokerData, ok := brokerAddressesMap.Load(brokerName)
+				So(ok, ShouldBeTrue)
+				So(brokerData, ShouldResemble, remoteBrokerData)
+			}
+		})
+	})
+}
+
 func TestUpdateTopicRouteInfo(t *testing.T) {
-	UpdateTopicRouteInfo("test")
+	Convey("Given a starting topic", t, func() {
+		updatedRouteData := UpdateTopicRouteInfo(topic)
+
+		Convey("updatedRouteData should be deep equal remoteRouteData", func() {
+			remoteRouteData, err := queryTopicRouteInfoFromServer(topic)
+			So(err, ShouldBeNil)
+			So(updatedRouteData, ShouldResemble, remoteRouteData)
+		})
+		Convey("updatedRouteData should be deep equal localRouteData", func() {
+			localRouteData, exist := routeDataMap.Load(topic)
+			So(exist, ShouldBeTrue)
+			So(updatedRouteData, ShouldResemble, localRouteData)
+		})
+	})
 }
+
+func TestFindBrokerAddrByTopic(t *testing.T) {
+	Convey("Given a starting topic", t, func() {
+		addr := FindBrokerAddrByTopic(topic)
+		remoteRouteData, err := queryTopicRouteInfoFromServer(topic)
+		So(err, ShouldBeNil)
+		brokerAddrList := remoteRouteData.BrokerDataList
+
+		Convey("addr from FindBrokerAddrByTopic should be contained in remoteRouteData", func()
{
+			flag := false
+			for _, brokerData := range brokerAddrList {
+				for _, ba := range brokerData.BrokerAddresses {
+					if ba == addr {
+						flag = true
+						break
+					}
+				}
+			}
+			So(flag, ShouldBeTrue)
+		})
+	})
+}
+
+func TestFindBrokerAddrByName(t *testing.T) {
+	Convey("Given a starting topic", t, func() {
+		remoteRouteData, err := queryTopicRouteInfoFromServer(topic)
+		So(err, ShouldBeNil)
+		brokerAddrList := remoteRouteData.BrokerDataList
+
+		Convey("addr from FindBrokerAddrByName should be equal remoteBrokerAddr from server", func()
{
+			for _, brokerData := range brokerAddrList {
+				brokerName := brokerData.BrokerName
+				addr := FindBrokerAddrByName(brokerName)
+				remoteBrokerAddr := brokerData.BrokerAddresses[MasterId]
+				So(addr, ShouldEqual, remoteBrokerAddr)
+			}
+		})
+	})
+}
+
+func TestFindBrokerAddressInSubscribe(t *testing.T) {
+	Convey("Given a starting topic", t, func() {
+		remoteRouteData, err := queryTopicRouteInfoFromServer(topic)
+		So(err, ShouldBeNil)
+		brokerAddrList := remoteRouteData.BrokerDataList
+
+		Convey("range BrokerAddress and compare them in turn", func() {
+			for _, brokerData := range brokerAddrList {
+				brokerName := brokerData.BrokerName
+				for id, ba := range brokerData.BrokerAddresses {
+					findBrokerRes := FindBrokerAddressInSubscribe(brokerName, id, true)
+					res := &FindBrokerResult{
+						BrokerAddr:    ba,
+						Slave:         false,
+						BrokerVersion: findBrokerVersion(brokerName, ba),
+					}
+					if id != MasterId {
+						res.Slave = true
+					}
+					So(findBrokerRes, ShouldResemble, res)
+				}
+			}
+		})
+	})
+}
\ No newline at end of file


Mime
View raw message