|
|
@@ -1,145 +1,145 @@
|
|
1
|
1
|
package modules
|
|
2
|
|
-
|
|
3
|
|
-import (
|
|
4
|
|
- "fmt"
|
|
5
|
|
- _ "fmt"
|
|
6
|
|
- "math/rand"
|
|
7
|
|
- "strconv"
|
|
8
|
|
- "testing"
|
|
9
|
|
- "time"
|
|
10
|
|
- "github.com/stretchr/testify/assert"
|
|
11
|
|
-)
|
|
12
|
|
-
|
|
13
|
|
-func dataMake(isalpha bool) MsgUnit{
|
|
14
|
|
- rand.Seed(time.Now().UnixNano())
|
|
15
|
|
-
|
|
16
|
|
- // Set Ipaddr
|
|
17
|
|
- msg := Message{"", "1.0", "", SM}
|
|
18
|
|
- for i := 0; i < 4; i++{
|
|
19
|
|
- itoa := strconv.Itoa(rand.Int() % 256)
|
|
20
|
|
-
|
|
21
|
|
- msg.from += itoa
|
|
22
|
|
- if i != 3{
|
|
23
|
|
- msg.from += "."
|
|
24
|
|
- }
|
|
25
|
|
- }
|
|
26
|
|
-
|
|
27
|
|
- // Set Time
|
|
28
|
|
- msg.time += strconv.Itoa(rand.Int()%24) + ":"
|
|
29
|
|
- msg.time += strconv.Itoa(rand.Int()%60)
|
|
30
|
|
-
|
|
31
|
|
- // Set Topic, Value, operator
|
|
32
|
|
- topic := []int64{}
|
|
33
|
|
- value := []int64{}
|
|
34
|
|
- operator := []string{}
|
|
35
|
|
- candOp := []string{">", ">=", "<=" ,"<", "=="}
|
|
36
|
|
- logicalOp := []string{"&&", "||"}
|
|
37
|
|
-
|
|
38
|
|
- topic = append(topic, rand.Int63())
|
|
39
|
|
-
|
|
40
|
|
- if isalpha{
|
|
41
|
|
- value = append(value, rand.Int63())
|
|
42
|
|
- operator = append(operator, "==")
|
|
43
|
|
- } else{
|
|
44
|
|
- len := rand.Int() % 2 + 1
|
|
45
|
|
- value = append(value, rand.Int63())
|
|
46
|
|
-
|
|
47
|
|
- if len == 1{
|
|
48
|
|
- operator = append(operator, candOp[rand.Int()%5])
|
|
49
|
|
- } else{
|
|
50
|
|
- value = append(value, rand.Int63())
|
|
51
|
|
- op := rand.Int()%2
|
|
52
|
|
- operator = append(operator, candOp[rand.Int()%2])
|
|
53
|
|
- if op == 0 {
|
|
54
|
|
- operator = append(operator, logicalOp[0])
|
|
55
|
|
- } else{
|
|
56
|
|
- operator = append(operator, logicalOp[1])
|
|
57
|
|
- }
|
|
58
|
|
- operator = append(operator, candOp[rand.Int()%2 + 2])
|
|
59
|
|
- }
|
|
60
|
|
- }
|
|
61
|
|
-
|
|
62
|
|
- return &SubscriptionMsg{msg, topic, value, operator, isalpha}
|
|
63
|
|
-}
|
|
64
|
|
-
|
|
65
|
|
-func Test_addSubscription(t *testing.T) {
|
|
66
|
|
- rand.Seed(time.Now().UnixNano())
|
|
67
|
|
-
|
|
68
|
|
- // To Init sub_mng
|
|
69
|
|
- mos := Moscato{sub_mng : sub_manager{}}
|
|
70
|
|
-
|
|
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))
|
|
80
|
|
- }
|
|
81
|
|
- }
|
|
82
|
|
-
|
|
83
|
|
- // Watch Data set
|
|
84
|
|
- for i := 0; i < dataLen ; i++{
|
|
85
|
|
- msg := msgList[i]
|
|
86
|
|
- 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)
|
|
93
|
|
- }
|
|
94
|
|
-
|
|
95
|
|
- // Test addSubScription
|
|
96
|
|
- for i := 0; i < dataLen ; i++{
|
|
97
|
|
-
|
|
98
|
|
- msg := msgList[i]
|
|
99
|
|
- ip := msg.(*SubscriptionMsg).Message.from
|
|
100
|
|
- topic := msg.(*SubscriptionMsg).topic
|
|
101
|
|
- value := msg.(*SubscriptionMsg).value
|
|
102
|
|
- //operator := msg.(*SubscriptionMsg).operator
|
|
103
|
|
- //isAlpha := msg.(*SubscriptionMsg).isAlpha
|
|
104
|
|
- subnumber := mos.sub_mng.count_sub
|
|
105
|
|
-
|
|
106
|
|
- mos.sub_mng.addSubscription(msg)
|
|
107
|
|
-
|
|
108
|
|
-
|
|
109
|
|
- // 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")
|
|
111
|
|
-
|
|
112
|
|
- // 2. Check topicNode
|
|
113
|
|
- nameptr := mos.sub_mng.list.head
|
|
114
|
|
- for nameptr != nil{
|
|
115
|
|
- if nameptr.topic[0] == topic[0]{
|
|
116
|
|
- break
|
|
117
|
|
- }
|
|
118
|
|
- nameptr = nameptr.next
|
|
119
|
|
- }
|
|
120
|
|
-
|
|
121
|
|
- for i := 0 ; i < len(topic); i++{
|
|
122
|
|
- assert.Equal(t, topic[i], nameptr.topic[i], "topicNode Add is failed")
|
|
123
|
|
- }
|
|
124
|
|
-
|
|
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
|
|
138
|
|
- }
|
|
139
|
|
- fmt.Println("my value = ", valptr.val, " real value = ", value)
|
|
140
|
|
-
|
|
141
|
|
- for i := 0; i < len(value); i++{
|
|
142
|
|
- assert.Equal(t, value[i], valptr.val[i], "ValNode Add is failed")
|
|
143
|
|
- }
|
|
144
|
|
- }
|
|
145
|
|
-}
|
|
|
2
|
+//
|
|
|
3
|
+//import (
|
|
|
4
|
+// "fmt"
|
|
|
5
|
+// _ "fmt"
|
|
|
6
|
+// "math/rand"
|
|
|
7
|
+// "strconv"
|
|
|
8
|
+// "testing"
|
|
|
9
|
+// "time"
|
|
|
10
|
+// "github.com/stretchr/testify/assert"
|
|
|
11
|
+//)
|
|
|
12
|
+//
|
|
|
13
|
+//func dataMake(isalpha bool) MsgUnit{
|
|
|
14
|
+// rand.Seed(time.Now().UnixNano())
|
|
|
15
|
+//
|
|
|
16
|
+// // Set Ipaddr
|
|
|
17
|
+// msg := Message{"", "1.0", "", SM}
|
|
|
18
|
+// for i := 0; i < 4; i++{
|
|
|
19
|
+// itoa := strconv.Itoa(rand.Int() % 256)
|
|
|
20
|
+//
|
|
|
21
|
+// msg.from += itoa
|
|
|
22
|
+// if i != 3{
|
|
|
23
|
+// msg.from += "."
|
|
|
24
|
+// }
|
|
|
25
|
+// }
|
|
|
26
|
+//
|
|
|
27
|
+// // Set Time
|
|
|
28
|
+// msg.time += strconv.Itoa(rand.Int()%24) + ":"
|
|
|
29
|
+// msg.time += strconv.Itoa(rand.Int()%60)
|
|
|
30
|
+//
|
|
|
31
|
+// // Set Topic, Value, operator
|
|
|
32
|
+// topic := []int64{}
|
|
|
33
|
+// value := []int64{}
|
|
|
34
|
+// operator := []string{}
|
|
|
35
|
+// candOp := []string{">", ">=", "<=" ,"<", "=="}
|
|
|
36
|
+// logicalOp := []string{"&&", "||"}
|
|
|
37
|
+//
|
|
|
38
|
+// topic = append(topic, rand.Int63())
|
|
|
39
|
+//
|
|
|
40
|
+// if isalpha{
|
|
|
41
|
+// value = append(value, rand.Int63())
|
|
|
42
|
+// operator = append(operator, "==")
|
|
|
43
|
+// } else{
|
|
|
44
|
+// len := rand.Int() % 2 + 1
|
|
|
45
|
+// value = append(value, rand.Int63())
|
|
|
46
|
+//
|
|
|
47
|
+// if len == 1{
|
|
|
48
|
+// operator = append(operator, candOp[rand.Int()%5])
|
|
|
49
|
+// } else{
|
|
|
50
|
+// value = append(value, rand.Int63())
|
|
|
51
|
+// op := rand.Int()%2
|
|
|
52
|
+// operator = append(operator, candOp[rand.Int()%2])
|
|
|
53
|
+// if op == 0 {
|
|
|
54
|
+// operator = append(operator, logicalOp[0])
|
|
|
55
|
+// } else{
|
|
|
56
|
+// operator = append(operator, logicalOp[1])
|
|
|
57
|
+// }
|
|
|
58
|
+// operator = append(operator, candOp[rand.Int()%2 + 2])
|
|
|
59
|
+// }
|
|
|
60
|
+// }
|
|
|
61
|
+//
|
|
|
62
|
+// return &SubscriptionMsg{msg, topic, value, operator, isalpha}
|
|
|
63
|
+//}
|
|
|
64
|
+//
|
|
|
65
|
+//func Test_addSubscription(t *testing.T) {
|
|
|
66
|
+// rand.Seed(time.Now().UnixNano())
|
|
|
67
|
+//
|
|
|
68
|
+// // To Init sub_mng
|
|
|
69
|
+// mos := Moscato{sub_mng : sub_manager{}}
|
|
|
70
|
+//
|
|
|
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))
|
|
|
80
|
+// }
|
|
|
81
|
+// }
|
|
|
82
|
+//
|
|
|
83
|
+// // Watch Data set
|
|
|
84
|
+// for i := 0; i < dataLen ; i++{
|
|
|
85
|
+// msg := msgList[i]
|
|
|
86
|
+// 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)
|
|
|
93
|
+// }
|
|
|
94
|
+//
|
|
|
95
|
+// // Test addSubScription
|
|
|
96
|
+// for i := 0; i < dataLen ; i++{
|
|
|
97
|
+//
|
|
|
98
|
+// msg := msgList[i]
|
|
|
99
|
+// ip := msg.(*SubscriptionMsg).Message.from
|
|
|
100
|
+// topic := msg.(*SubscriptionMsg).topic
|
|
|
101
|
+// value := msg.(*SubscriptionMsg).value
|
|
|
102
|
+// //operator := msg.(*SubscriptionMsg).operator
|
|
|
103
|
+// //isAlpha := msg.(*SubscriptionMsg).isAlpha
|
|
|
104
|
+// subnumber := mos.sub_mng.count_sub
|
|
|
105
|
+//
|
|
|
106
|
+// mos.sub_mng.addSubscription(msg)
|
|
|
107
|
+//
|
|
|
108
|
+//
|
|
|
109
|
+// // 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")
|
|
|
111
|
+//
|
|
|
112
|
+// // 2. Check topicNode
|
|
|
113
|
+// nameptr := mos.sub_mng.list.head
|
|
|
114
|
+// for nameptr != nil{
|
|
|
115
|
+// if nameptr.topic[0] == topic[0]{
|
|
|
116
|
+// break
|
|
|
117
|
+// }
|
|
|
118
|
+// nameptr = nameptr.next
|
|
|
119
|
+// }
|
|
|
120
|
+//
|
|
|
121
|
+// for i := 0 ; i < len(topic); i++{
|
|
|
122
|
+// assert.Equal(t, topic[i], nameptr.topic[i], "topicNode Add is failed")
|
|
|
123
|
+// }
|
|
|
124
|
+//
|
|
|
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
|
|
|
138
|
+// }
|
|
|
139
|
+// fmt.Println("my value = ", valptr.val, " real value = ", value)
|
|
|
140
|
+//
|
|
|
141
|
+// for i := 0; i < len(value); i++{
|
|
|
142
|
+// assert.Equal(t, value[i], valptr.val[i], "ValNode Add is failed")
|
|
|
143
|
+// }
|
|
|
144
|
+// }
|
|
|
145
|
+//}
|