Kaynağa Gözat

[fin] matchingTest

master
extra1563 4 yıl önce
ebeveyn
işleme
85fa72a27e
1 değiştirilmiş dosya ile 168 ekleme ve 36 silme
  1. 168
    36
      src/broker/modules/matching_test.go

+ 168
- 36
src/broker/modules/matching_test.go Dosyayı Görüntüle

@@ -1,37 +1,169 @@
1 1
 package modules
2
-//
3
-//import (
4
-//	"fmt"
5
-//	"testing"
6
-//)
7
-//
8
-//func Test_simpleLList_pbNtraverse(t *testing.T) {
9
-//	mylist := new(LList)
10
-//	mylist.PushBack(30)
11
-//	mylist.PushBack(40)
12
-//	mylist.PushBack(20)
13
-//	mylist.PushBack(10)
14
-//	ptr := mylist.head
15
-//	for ptr != nil {
16
-//		fmt.Printf("data = %d, address = %p\n", ptr.val, ptr.next)
17
-//		ptr = ptr.next
18
-//	}
19
-//}
20
-//
21
-//func Test_simpleLList_mergeSort(t *testing.T) {
22
-//	mylist := new(LList)
23
-//	mylist.PushBack(30)
24
-//	mylist.PushBack(40)
25
-//	mylist.PushBack(20)
26
-//	mylist.PushBack(10)
27
-//	mylist.head = MergeSort(mylist.head, mylist.size / 2)
28
-//	ptr := mylist.head
29
-//	for ptr != nil {
30
-//		fmt.Printf("data = %d, address = %p\n", ptr.val, ptr.next)
31
-//		ptr = ptr.next
32
-//	}
33
-//}
34
-//
35
-//func Test_removeArray(t *testing.T){}
36
-//func Test_deleteList(t *testing.T){}
37
-//func Test_insertList(t *testing.T){}
2
+
3
+import (
4
+	"fmt"
5
+	"github.com/stretchr/testify/assert"
6
+	"math/rand"
7
+	"strconv"
8
+	"testing"
9
+	"time"
10
+)
11
+
12
+
13
+func makePubData(isAlpha bool) MsgUnit{
14
+	rand.Seed(time.Now().UnixNano())
15
+	// Set Ipaddr
16
+	msg := Message{"", "1.0", "", PM}
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
+	topic := []int64{}
30
+	value := []int64{}
31
+	content := []int64{}
32
+
33
+	// Set Topic
34
+	topicLen := rand.Int() % 10 + 1
35
+	for i := 0 ; i < topicLen; i++{
36
+		topic = append(topic, rand.Int63())
37
+	}
38
+
39
+	// Set Value
40
+	if isAlpha {
41
+		valueLen := rand.Int() % 10 + 1
42
+		for i := 0 ; i < valueLen; i++{
43
+			value = append(value, rand.Int63())
44
+		}
45
+	} else {
46
+		topic = append(topic, rand.Int63())
47
+		value = append(value, rand.Int63())
48
+	}
49
+
50
+	// Set content
51
+	contentLen := rand.Int() % 10
52
+	for i := 0; i < contentLen; i++{
53
+		content = append(content, rand.Int63())
54
+	}
55
+
56
+	return &PublishMsg{msg, topic, value, content}
57
+}
58
+
59
+func makeSubData(isAlpha bool, topic []int64, value []int64) MsgUnit{
60
+	rand.Seed(time.Now().UnixNano())
61
+	// Set Ipaddr
62
+	msg := Message{"", "1.0", "", SM}
63
+	for i := 0; i < 4; i++{
64
+		itoa := strconv.Itoa(rand.Int() % 256)
65
+		msg.from += itoa
66
+		if i != 3{
67
+			msg.from += "."
68
+		}
69
+	}
70
+
71
+	// Set Time
72
+	msg.time += strconv.Itoa(rand.Int()%24) + ":"
73
+	msg.time += strconv.Itoa(rand.Int()%60)
74
+
75
+	// Set Topic, Value, operator
76
+	operator := []string{}
77
+	candOp := []string{">", ">=", "<=" ,"<", "=="}
78
+	logicalOp := []string{"&&", "||"}
79
+
80
+	if isAlpha {
81
+		operator = append(operator, "==")
82
+	} else {
83
+		randSeed := rand.Int() % 2
84
+		if randSeed == 0 {
85
+			lop := rand.Int() % 2
86
+			operator = append(operator, candOp[rand.Int()%2])
87
+			if lop == 0 {
88
+				operator = append(operator, logicalOp[0])
89
+				for {
90
+					x := rand.Int63()
91
+					if x > value[0] {
92
+						value = append(value, x)
93
+						break
94
+					}
95
+				}
96
+			} else {
97
+				operator = append(operator, logicalOp[1])
98
+				value = append(value, rand.Int63())
99
+			}
100
+			operator = append(operator, candOp[rand.Int()%2+2])
101
+		} else {
102
+			op := candOp[rand.Int()%5]
103
+			operator = append(operator, op)
104
+		}
105
+	}
106
+
107
+	return &SubscriptionMsg{msg, topic, value, operator, isAlpha}
108
+}
109
+
110
+func Test_matching(t *testing.T) {
111
+	rand.Seed(time.Now().UnixNano())
112
+	assert.Equal(t, 1, 1)
113
+	mos := Moscato{ sub_mng: *newSubmng(),}
114
+	mos.queue.queue_init()
115
+
116
+	// 1. Make Publish Data
117
+	pubLen := rand.Int()%100 + 1 // Set dataLength
118
+	var pubDataList []MsgUnit
119
+  
120
+	for i := 0; i < pubLen; i++{
121
+		if i % 2 == 1 {
122
+			msg := makePubData(true)
123
+			mos.queue.push(msg)
124
+			pubDataList = append(pubDataList, msg)
125
+		} else{
126
+			msg := makePubData(false)
127
+			mos.queue.push(msg)
128
+			pubDataList = append(pubDataList, msg)
129
+		}
130
+	}
131
+
132
+	// 2. Creates subscription data with (the same <topic, value> or (the same <topic, difValue>
133
+	//    And Add Subscription
134
+	subLen := pubLen
135
+	var subDataList []MsgUnit
136
+	for i := 0; i < subLen; i++{
137
+		topic := pubDataList[i].(*PublishMsg).topic
138
+		value := pubDataList[i].(*PublishMsg).value
139
+		if i % 2 == 1{
140
+			msg := makeSubData(true, topic, value)
141
+			mos.sub_mng.addSubscription(msg)
142
+			subDataList = append(subDataList, msg)
143
+
144
+		} else{
145
+			msg := makeSubData(false, topic, value)
146
+			mos.sub_mng.addSubscription(msg)
147
+			subDataList = append(subDataList, msg)
148
+		}
149
+	}
150
+
151
+	// 3. Watch Data
152
+	
153
+  	// fmt.Println("PubData")
154
+	// watchData(pubDataList, pubLen, false)
155
+	// fmt.Println("\nSubData")
156
+	// watchData(subDataList, subLen, true)
157
+
158
+	// 4. Matching
159
+	for i := 0; i < pubLen; i++ {
160
+		matching, pubmsg, err := mos.match_mng.Matching(&mos.queue, &mos.sub_mng)
161
+
162
+		// Check if matching is correct
163
+		assert.Equal(t, nil, err)
164
+
165
+		fmt.Println("matching list = ", matching)
166
+		fmt.Println("pub msg = ", pubmsg)
167
+		fmt.Println("err ?= ", err)
168
+	}
169
+}

Loading…
İptal
Kaydet