-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_processor_test.go
95 lines (81 loc) · 1.75 KB
/
data_processor_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package hub
import (
"fmt"
"math/rand"
"testing"
"time"
)
type Handle1 struct{}
func (Handle1) Name() string {
return "Handle1"
}
func (Handle1) OnData(data interface{}) interface{} {
fmt.Println("Handle1:", data)
return data
}
type Handle2 struct{}
func (Handle2) Name() string {
return "Handle2"
}
func (Handle2) OnData(data interface{}) interface{} {
fmt.Println("Handle2:", data)
return data
}
type Handle3 struct{}
func (Handle3) Name() string {
return "Handle3"
}
func (Handle3) OnData(data interface{}) interface{} {
fmt.Println("Handle3:", data)
return data
}
func Test_Queue(t *testing.T) {
h1 := &Handle1{}
h2 := &Handle2{}
h3 := &Handle3{}
c := newQueue(nil)
arr := []IDataProcessor{h1, h2, h3}
for _, v := range arr {
go func(v IDataProcessor) {
c.Append(v)
}(v)
}
time.Sleep(time.Second)
go func() {
t.Log("AfterAppend:", c.Len(), c.String())
cursor := c.Cursor()
for cursor.Next() {
t.Log("cursor.Name", cursor.Value().Name())
}
}()
time.Sleep(time.Second)
rand.Seed(time.Now().UnixNano())
randseq := rand.Perm(len(arr))
for _, i := range randseq {
go func(v IDataProcessor) {
t.Log("delete", v.Name(), "=>", c.Delete(v.Name()).Name())
}(arr[i])
}
time.Sleep(time.Second)
go func() {
t.Log("AfterDelete:", c.Len(), c.String())
cursor := c.Cursor()
for cursor.Next() {
t.Log("cursor.Name", cursor.Value().Name())
}
}()
go func() {
for _, e := range arr {
c.Insert(e.Name(), e)
}
}()
time.Sleep(time.Second)
go func() {
t.Log("AfterInsert:", c.Len(), c.String())
cursor := c.Cursor()
for cursor.Next() {
t.Log("cursor.Name", cursor.Value().Name())
}
}()
time.Sleep(time.Second)
}