| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- package modules
-
- import (
- "fmt"
- _ "fmt"
- "github.com/stretchr/testify/assert"
- "math/rand"
- "strconv"
- "testing"
- "time"
- )
-
- func makeData(isAlpha bool) MsgUnit{
- rand.Seed(time.Now().UnixNano())
- // Set Ipaddr
- msg := Message{"", "1.0", "", SM}
- for i := 0; i < 4; i++{
- itoa := strconv.Itoa(rand.Int() % 256)
- msg.from += itoa
- if i != 3{
- msg.from += "."
- }
- }
-
- // Set Time
- msg.time += strconv.Itoa(rand.Int()%24) + ":"
- msg.time += strconv.Itoa(rand.Int()%60)
-
- // Set Topic, Value, operator
- topic := []int64{}
- value := []int64{}
- operator := []string{}
- candOp := []string{">", ">=", "<=" ,"<", "=="}
- logicalOp := []string{"&&", "||"}
-
- if isAlpha {
- topicLen := rand.Int() % 10 + 1
- for i := 0 ; i < topicLen; i++{
- topic = append(topic, rand.Int63())
- }
-
- valueLen := rand.Int() % 10 + 1
- for i := 0 ; i < valueLen; i++{
- value = append(value, rand.Int63())
- }
-
- operator = append(operator, "==")
-
- } else{
- valueLen := rand.Int() % 2 + 1
-
- topic = append(topic, rand.Int63())
- value = append(value, rand.Int63())
-
- if valueLen == 1{
- operator = append(operator, candOp[rand.Int()%5])
- } else{
- value = append(value, rand.Int63())
- op := rand.Int()%2
- operator = append(operator, candOp[rand.Int()%2])
- if op == 0 {
- operator = append(operator, logicalOp[0])
- } else{
- operator = append(operator, logicalOp[1])
- }
- operator = append(operator, candOp[rand.Int()%2 + 2])
- }
- }
-
- return &SubscriptionMsg{msg, topic, value, operator, isAlpha}
- }
-
- func makeMsgList(dataLen int, isAlpha bool) []MsgUnit{
- rand.Seed(time.Now().UnixNano())
- var ret []MsgUnit
- for i := 0 ; i < dataLen ; i++{
- ret = append(ret, makeData(isAlpha))
- }
- return ret
- }
-
- func checkOperatorList(isSingle bool, sub int, operator string, l *valueNode) int{
- ret := -1
- if isSingle{
- switch operator {
- case "<":
- ret = findSub(l.single2sub_s, sub)
- case "<=":
- ret = findSub(l.single2sub_es, sub)
- case ">":
- ret = findSub(l.single2sub_b, sub)
- case ">=":
- ret = findSub(l.single2sub_eb, sub)
- case "==":
- ret = findSub(l.single2sub_e, sub)
- }
-
- } else{
- switch operator {
- case "<":
- ret = findSub(l.range2sub_s, sub)
- case "<=":
- ret = findSub(l.range2sub_es, sub)
- case ">":
- ret = findSub(l.range2sub_b, sub)
- case ">=":
- ret = findSub(l.range2sub_eb, sub)
- }
- }
- return ret
- }
-
- func watchData(msgList []MsgUnit, dataLen int, isSubscription bool){
- for i := 0; i < dataLen; i++{
- msg := msgList[i]
- if isSubscription {
- fmt.Println(
- "\nfrom = ", msg.(*SubscriptionMsg).Message.from,
- "\ntime = ", msg.(*SubscriptionMsg).Message.time,
- "\ntopic = ", msg.(*SubscriptionMsg).topic,
- "\nvalue = ", msg.(*SubscriptionMsg).value,
- "\noperator = ", msg.(*SubscriptionMsg).operator,
- "\nisAlpha ?= ", msg.(*SubscriptionMsg).isAlpha,
- )
- } else{
- fmt.Println(
- "\nfrom = ", msg.(*PublishMsg).Message.from,
- "\ntime = ", msg.(*PublishMsg).Message.time,
- "\ntopic = ", msg.(*PublishMsg).topic,
- "\nvalue = ", msg.(*PublishMsg).value,
- )
- }
- }
- }
-
- //Test addSubScription(1) (dif all [topic, value, operator])
- func Test_addSubscription_allDif(t *testing.T) {
- rand.Seed(time.Now().UnixNano())
-
- // To Init sub_mng
- mos := Moscato{sub_mng: *newSubmng()}
-
- // Make Data set(Subscription)
- var msgList []MsgUnit
- dataLen := 100
- msgList = makeMsgList(dataLen, false)
-
- //Watch Data set
- //watchData(msgList, dataLen, true)
-
- for i := 0; i < dataLen; i++ {
- msg := msgList[i]
- ip := msg.(*SubscriptionMsg).Message.from
- topic := msg.(*SubscriptionMsg).topic
- value := msg.(*SubscriptionMsg).value
- operator := msg.(*SubscriptionMsg).operator
- subnumber := mos.sub_mng.count_sub
- isSingle := true
-
- // 0. Check addSubscription
- err := mos.sub_mng.addSubscription(msg)
- assert.Equal(t, nil, err)
-
- // 1. Check if ip mapping is correct
- assert.Equal(t, subnumber, mos.sub_mng.ip2sub[ip][len(mos.sub_mng.ip2sub[ip])-1], "Ip mapping is failed")
-
- // 2. Check topicNode
- topicPtr := mos.sub_mng.list.head
- for topicPtr != nil {
- if Compare(topic, topicPtr.topic) == 0 {
- break
- }
- topicPtr = topicPtr.next
- }
-
- assert.Equal(t, topic, topicPtr.topic, "topicNode Add is failed")
-
- // Check isSingle ?
- if len(operator) == 3 && operator[1] == "&&" {
- isSingle = false
- }
-
- // 3. Check Value in ValueNode & Check Operator in ValueNode
- if !isSingle || (len(operator) == 3 && operator[1] == "||") {
- valptr1 := topicPtr.list.getValueNodePos([]int64{value[0]})
- valptr2 := topicPtr.list.getValueNodePos([]int64{value[1]})
-
- assert.Equal(t, []int64{value[0]}, valptr1.val)
- assert.Equal(t, []int64{value[1]}, valptr2.val)
-
- assert.NotEqual(t, -1, checkOperatorList(isSingle, subnumber, operator[0], valptr1))
- assert.NotEqual(t, -1, checkOperatorList(isSingle, subnumber, operator[2], valptr2))
-
- } else {
- valptr := topicPtr.list.getValueNodePos(value)
- assert.Equal(t, value, valptr.val)
- assert.NotEqual(t, -1, checkOperatorList(isSingle, subnumber, operator[0], valptr))
- }
- }
- }
-
- // Test addSubScription(2) (same [topic, value] dif [operator])
- func Test_addSubscription_same_topicNvalue(t *testing.T) {
- rand.Seed(time.Now().UnixNano())
-
- // To Init sub_mng
- mos := Moscato{sub_mng: *newSubmng()}
-
- // Fix Topic & Value
- topicLen := rand.Int()%10 + 1
- staticTopic := make([]int64, topicLen)
- staticValue := []int64{rand.Int63()}
-
-
- // Make Data
- var msgList []MsgUnit
- dataLen := 100
- msgList = makeMsgList(dataLen, false)
-
- for i := 0; i < topicLen; i++ {
- staticTopic[i] = rand.Int63()
- }
-
- // Fix Same Topic & Value
- for i := 0; i < dataLen; i++ {
- operator := msgList[i].(*SubscriptionMsg).operator
- msgList[i].(*SubscriptionMsg).topic = staticTopic
-
- if len(operator) == 1{
- msgList[i].(*SubscriptionMsg).value = staticValue
- }
- }
-
- // Watch Data set
- //watchData(msgList, dataLen, true)
-
- for i := 0; i < dataLen; i++ {
- msg := msgList[i]
- err := mos.sub_mng.addSubscription(msg)
- assert.Equal(t, nil, err)
- }
-
- topicPtr := mos.sub_mng.list.head
- for topicPtr != nil {
- if Compare(topicPtr.topic, staticTopic) == 0 {
- break
- }
- topicPtr = topicPtr.next
- }
- watchValueNode(topicPtr.list.head)
-
- }
-
- func watchValueNode(ptr *valueNode) {
- valPtr := ptr
- for valPtr != nil{
- fmt.Println("Value = ", valPtr.val)
- if len(valPtr.single2sub_s) != 0 {
- fmt.Println("Single2sub (<) List = ", valPtr.single2sub_s)
- }
- if len(valPtr.single2sub_es) != 0 {
- fmt.Println("Single2sub (<=) List = ", valPtr.single2sub_es)
- }
- if len(valPtr.single2sub_b) != 0 {
- fmt.Println("Single2sub (>) List = ", valPtr.single2sub_b)
- }
- if len(valPtr.single2sub_eb) != 0 {
- fmt.Println("Single2sub (>=) List = ", valPtr.single2sub_eb)
- }
- if len(valPtr.single2sub_e) != 0 {
- fmt.Println("Single2sub (==) List = ", valPtr.single2sub_e)
- }
-
- if len(valPtr.range2sub_s) != 0 {
- fmt.Println("range2sub (<) List = ", valPtr.range2sub_s)
- }
- if len(valPtr.range2sub_es) != 0 {
- fmt.Println("range2sub (<=) List = ", valPtr.range2sub_es)
- }
- if len(valPtr.range2sub_b) != 0 {
- fmt.Println("range2sub (>) List = ", valPtr.range2sub_b)
- }
- if len(valPtr.range2sub_eb) != 0 {
- fmt.Println("range2sub (>=) List = ", valPtr.range2sub_eb)
- }
- valPtr = valPtr.next
- }
- }
|