ソースを参照

[fin] test_add_subcription

master
extra1563 4年前
コミット
c82bce7354
1個のファイルの変更204行の追加59行の削除
  1. 204
    59
      src/broker/modules/subscription_test.go

+ 204
- 59
src/broker/modules/subscription_test.go ファイルの表示

@@ -3,21 +3,19 @@ package modules
3 3
 import (
4 4
 	"fmt"
5 5
 	_ "fmt"
6
+	"github.com/stretchr/testify/assert"
6 7
 	"math/rand"
7 8
 	"strconv"
8 9
 	"testing"
9 10
 	"time"
10
-	"github.com/stretchr/testify/assert"
11 11
 )
12 12
 
13
-func dataMake(isalpha bool) MsgUnit{
13
+func makeData(isAlpha bool) MsgUnit{
14 14
 	rand.Seed(time.Now().UnixNano())
15
-
16 15
 	// Set Ipaddr
17 16
 	msg := Message{"", "1.0", "", SM}
18 17
 	for i := 0; i < 4; i++{
19 18
 		itoa := strconv.Itoa(rand.Int() % 256)
20
-
21 19
 		msg.from += itoa
22 20
 		if i != 3{
23 21
 			msg.from += "."
@@ -35,16 +33,26 @@ func dataMake(isalpha bool) MsgUnit{
35 33
 	candOp := []string{">", ">=", "<=" ,"<", "=="}
36 34
 	logicalOp := []string{"&&", "||"}
37 35
 
38
-	topic = append(topic, rand.Int63())
36
+	if isAlpha {
37
+		topicLen := rand.Int() % 10 + 1
38
+		for i := 0 ; i < topicLen; i++{
39
+			topic = append(topic, rand.Int63())
40
+		}
41
+
42
+		valueLen := rand.Int() % 10 + 1
43
+		for i := 0 ; i < valueLen; i++{
44
+			value = append(value, rand.Int63())
45
+		}
39 46
 
40
-	if isalpha{
41
-		value = append(value, rand.Int63())
42 47
 		operator = append(operator, "==")
48
+
43 49
 	} else{
44
-		len := rand.Int() % 2 + 1
50
+		valueLen := rand.Int() % 2 + 1
51
+
52
+		topic = append(topic, rand.Int63())
45 53
 		value = append(value, rand.Int63())
46 54
 
47
-		if len == 1{
55
+		if valueLen == 1{
48 56
 			operator = append(operator, candOp[rand.Int()%5])
49 57
 		} else{
50 58
 			value = append(value, rand.Int63())
@@ -59,87 +67,224 @@ func dataMake(isalpha bool) MsgUnit{
59 67
 		}
60 68
 	}
61 69
 
62
-	return &SubscriptionMsg{msg, topic, value, operator, isalpha}
70
+	return &SubscriptionMsg{msg, topic, value, operator, isAlpha}
63 71
 }
64 72
 
65
-func Test_addSubscription(t *testing.T) {
73
+func makeMsgList(dataLen int, isAlpha bool) []MsgUnit{
66 74
 	rand.Seed(time.Now().UnixNano())
75
+	var ret []MsgUnit
76
+	for i := 0 ; i < dataLen ; i++{
77
+		ret = append(ret, makeData(isAlpha))
78
+	}
79
+	return ret
80
+}
67 81
 
68
-	// To Init sub_mng
69
-	mos := Moscato{sub_mng : sub_manager{}}
82
+func checkOperatorList(isSingle bool, sub int, operator string, l *valueNode) int{
83
+	ret := -1
84
+	if isSingle{
85
+		switch operator {
86
+		case "<":
87
+			ret = findSub(l.single2sub_s, sub)
88
+		case "<=":
89
+			ret = findSub(l.single2sub_es, sub)
90
+		case ">":
91
+			ret = findSub(l.single2sub_b, sub)
92
+		case ">=":
93
+			ret = findSub(l.single2sub_eb, sub)
94
+		case "==":
95
+			ret = findSub(l.single2sub_e, sub)
96
+		}
70 97
 
71
-	// Make Data set(Subscription)
72
-	msgList := []MsgUnit{}
73
-	dataLen:= 3
74
-	for i := 0 ; i < dataLen ; i++{
75
-		seed := rand.Int()%2
76
-		if seed == 0{
77
-			msgList = append(msgList, dataMake(false))
78
-		} else{
79
-			msgList = append(msgList, dataMake(true))
98
+	} else{
99
+		switch operator {
100
+		case "<":
101
+			ret = findSub(l.range2sub_s, sub)
102
+		case "<=":
103
+			ret = findSub(l.range2sub_es, sub)
104
+		case ">":
105
+			ret = findSub(l.range2sub_b, sub)
106
+		case ">=":
107
+			ret = findSub(l.range2sub_eb, sub)
80 108
 		}
81 109
 	}
110
+	return ret
111
+}
82 112
 
83
-	// Watch Data set
84
-	for i := 0; i < dataLen ; i++{
113
+func watchData(msgList []MsgUnit, dataLen int){
114
+	for i := 0; i < dataLen; i++{
85 115
 		msg := msgList[i]
86 116
 		fmt.Println(
87
-			" from = ", msg.(*SubscriptionMsg).Message.from,
88
-			" time = ", msg.(*SubscriptionMsg).Message.time,
89
-			" topic = ", msg.(*SubscriptionMsg).topic,
90
-			" value = ", msg.(*SubscriptionMsg).value,
91
-			" operator = ", msg.(*SubscriptionMsg).operator,
92
-			" isAlpha ?= ", msg.(*SubscriptionMsg).isAlpha)
117
+			"\nfrom = ", msg.(*SubscriptionMsg).Message.from,
118
+			"\ntime = ", msg.(*SubscriptionMsg).Message.time,
119
+			"\ntopic = ", msg.(*SubscriptionMsg).topic,
120
+			"\nvalue = ", msg.(*SubscriptionMsg).value,
121
+			"\noperator = ", msg.(*SubscriptionMsg).operator,
122
+			"\nisAlpha ?= ", msg.(*SubscriptionMsg).isAlpha,
123
+		)
124
+	}
125
+}
126
+
127
+//Test addSubScription(1) (dif all [topic, value, operator])
128
+func Test_addSubscription_allDif(t *testing.T) {
129
+	rand.Seed(time.Now().UnixNano())
130
+
131
+	// To Init sub_mng
132
+	s := sub_manager{
133
+		ip2sub:     make(map[string][]int),
134
+		sub2node:   make(map[int][]*valueNode),
135
+		israngesub: make(map[int]bool),
93 136
 	}
137
+	mos := Moscato{sub_mng: s}
138
+
139
+	// Make Data set(Subscription)
140
+	var msgList []MsgUnit
141
+	dataLen := 100
142
+	msgList = makeMsgList(dataLen, false)
94 143
 
95
-	// Test addSubScription
96
-	for i := 0; i < dataLen ; i++{
144
+	//Watch Data set
145
+	//watchData(msgList, dataLen)
97 146
 
147
+	for i := 0; i < dataLen; i++ {
98 148
 		msg := msgList[i]
99 149
 		ip := msg.(*SubscriptionMsg).Message.from
100 150
 		topic := msg.(*SubscriptionMsg).topic
101 151
 		value := msg.(*SubscriptionMsg).value
102
-		//operator := msg.(*SubscriptionMsg).operator
103
-		//isAlpha := msg.(*SubscriptionMsg).isAlpha
152
+		operator := msg.(*SubscriptionMsg).operator
104 153
 		subnumber := mos.sub_mng.count_sub
154
+		isSingle := true
105 155
 
106
-		mos.sub_mng.addSubscription(msg)
107
-
156
+		// 0. Check addSubscription
157
+		err := mos.sub_mng.addSubscription(msg)
158
+		assert.Equal(t, nil, err)
108 159
 
109 160
 		// 1. Check if ip mapping is correct
110
-		assert.Equal(t, subnumber, mos.sub_mng.ip2sub[ip][len(mos.sub_mng.ip2sub[ip]) - 1], "Ip mapping is failed")
161
+		assert.Equal(t, subnumber, mos.sub_mng.ip2sub[ip][len(mos.sub_mng.ip2sub[ip])-1], "Ip mapping is failed")
111 162
 
112 163
 		// 2. Check topicNode
113
-		nameptr := mos.sub_mng.list.head
114
-		for nameptr != nil{
115
-			if nameptr.topic[0] == topic[0]{
164
+		topicPtr := mos.sub_mng.list.head
165
+		for topicPtr != nil {
166
+			if Compare(topic, topicPtr.topic) == 0 {
116 167
 				break
117 168
 			}
118
-			nameptr = nameptr.next
169
+			topicPtr = topicPtr.next
119 170
 		}
120 171
 
121
-		for i := 0 ; i < len(topic); i++{
122
-			assert.Equal(t, topic[i], nameptr.topic[i], "topicNode Add is failed")
172
+		assert.Equal(t, topic, topicPtr.topic, "topicNode Add is failed")
173
+
174
+		// Check isSingle ?
175
+		if len(operator) == 3 && operator[1] == "&&" {
176
+			isSingle = false
123 177
 		}
124 178
 
125
-		// 3. Check Value in ValueNode
126
-		valptr := nameptr.list.head
127
-		for valptr != nil{
128
-			for i:=0 ; i < len(value); i++{
129
-				find := true
130
-				if value[i] != valptr.val[i]{
131
-					find = false
132
-				}
133
-				if find{
134
-					break
135
-				}
136
-			}
137
-			valptr = valptr.next
179
+		// 3. Check Value in ValueNode & Check Operator in ValueNode
180
+		if !isSingle || (len(operator) == 3 && operator[1] == "||") {
181
+			valptr1 := topicPtr.list.getValueNodePos([]int64{value[0]})
182
+			valptr2 := topicPtr.list.getValueNodePos([]int64{value[1]})
183
+
184
+			assert.Equal(t, []int64{value[0]}, valptr1.val)
185
+			assert.Equal(t, []int64{value[1]}, valptr2.val)
186
+
187
+			assert.NotEqual(t, -1, checkOperatorList(isSingle, subnumber, operator[0], valptr1))
188
+			assert.NotEqual(t, -1, checkOperatorList(isSingle, subnumber, operator[2], valptr2))
189
+
190
+		} else {
191
+			valptr := topicPtr.list.getValueNodePos(value)
192
+			assert.Equal(t, value, valptr.val)
193
+			assert.NotEqual(t, -1, checkOperatorList(isSingle, subnumber, operator[0], valptr))
138 194
 		}
139
-		fmt.Println("my value = ", valptr.val, " real value = ", value)
195
+	}
196
+}
197
+
198
+// Test addSubScription(2) (same [topic, value] dif [operator])
199
+func Test_addSubscription_same_topicNvalue(t *testing.T) {
200
+	rand.Seed(time.Now().UnixNano())
201
+
202
+	// To Init sub_mng
203
+	s := sub_manager{
204
+		ip2sub:     make(map[string][]int),
205
+		sub2node:   make(map[int][]*valueNode),
206
+		israngesub: make(map[int]bool),
207
+	}
208
+
209
+	mos := Moscato{sub_mng: s}
210
+
211
+	// Fix Topic & Value
212
+	topicLen := rand.Int()%10 + 1
213
+	staticTopic := make([]int64, topicLen)
214
+	staticValue := []int64{rand.Int63()}
215
+
216
+
217
+	// Make Data
218
+	var msgList []MsgUnit
219
+	dataLen := 100
220
+	msgList = makeMsgList(dataLen, false)
221
+
222
+	for i := 0; i < topicLen; i++ {
223
+		staticTopic[i] = rand.Int63()
224
+	}
225
+
226
+	// Fix Same Topic & Value
227
+	for i := 0; i < dataLen; i++ {
228
+		operator := msgList[i].(*SubscriptionMsg).operator
229
+		msgList[i].(*SubscriptionMsg).topic = staticTopic
230
+
231
+		if len(operator) == 1{
232
+			msgList[i].(*SubscriptionMsg).value = staticValue
233
+		}
234
+	}
235
+
236
+	// Watch Data set
237
+	// watchData(msgList, dataLen)
238
+
239
+	for i := 0; i < dataLen; i++ {
240
+		msg := msgList[i]
241
+		err := mos.sub_mng.addSubscription(msg)
242
+		assert.Equal(t, nil, err)
243
+	}
244
+
245
+	topicPtr := mos.sub_mng.list.head
246
+	for topicPtr != nil {
247
+		if Compare(topicPtr.topic, staticTopic) == 0 {
248
+			break
249
+		}
250
+		topicPtr = topicPtr.next
251
+	}
252
+	watchValueNode(topicPtr.list.head)
253
+
254
+}
140 255
 
141
-		for i := 0; i < len(value); i++{
142
-			assert.Equal(t, value[i], valptr.val[i], "ValNode Add is failed")
256
+func watchValueNode(ptr *valueNode) {
257
+	valPtr := ptr
258
+	for valPtr != nil{
259
+		fmt.Println("Value = ", valPtr.val)
260
+		if len(valPtr.single2sub_s) != 0 {
261
+			fmt.Println("Single2sub (<) List = ", valPtr.single2sub_s)
262
+		}
263
+		if len(valPtr.single2sub_es) != 0 {
264
+			fmt.Println("Single2sub (<=) List = ", valPtr.single2sub_es)
265
+		}
266
+		if len(valPtr.single2sub_b) != 0 {
267
+			fmt.Println("Single2sub (>) List = ", valPtr.single2sub_b)
268
+		}
269
+		if len(valPtr.single2sub_eb) != 0 {
270
+			fmt.Println("Single2sub (>=) List = ", valPtr.single2sub_eb)
271
+		}
272
+		if len(valPtr.single2sub_e) != 0 {
273
+			fmt.Println("Single2sub (==) List = ", valPtr.single2sub_e)
274
+		}
275
+
276
+		if len(valPtr.range2sub_s) != 0 {
277
+			fmt.Println("range2sub (<) List = ", valPtr.range2sub_s)
278
+		}
279
+		if len(valPtr.range2sub_es) != 0 {
280
+			fmt.Println("range2sub (<=) List = ", valPtr.range2sub_es)
281
+		}
282
+		if len(valPtr.range2sub_b) != 0 {
283
+			fmt.Println("range2sub (>) List = ", valPtr.range2sub_b)
284
+		}
285
+		if len(valPtr.range2sub_eb) != 0 {
286
+			fmt.Println("range2sub (>=) List = ", valPtr.range2sub_eb)
143 287
 		}
288
+		valPtr = valPtr.next
144 289
 	}
145 290
 }

読み込み中…
キャンセル
保存