Просмотр исходного кода

[refactor] subscption.constructor

master
extra1563 4 лет назад
Родитель
Сommit
6c3535ada3
1 измененных файлов: 286 добавлений и 289 удалений
  1. 286
    289
      src/broker/modules/subscription_test.go

+ 286
- 289
src/broker/modules/subscription_test.go Просмотреть файл

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

Загрузка…
Отмена
Сохранить