-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_test.go
125 lines (111 loc) · 2.75 KB
/
process_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package concurrent
import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"runtime"
"testing"
"time"
)
func TestProcess(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())
maxIndexSize := 20
limit := 3
inputsCh := loadToStuct(maxIndexSize)
//to json
jsonResults, err := Process(inputsCh, process, limit)
if err != nil {
panic(err)
}
jsonCh := loadFromJson(jsonResults, maxIndexSize)
//from json
structResults, err := Process(jsonCh, fromJson, limit)
if err != nil {
panic(err)
}
match := true
i := 0
for v := range loadToStuct(maxIndexSize) {
fmt.Println(structResults[i].(*NumberHolder).Age, "!=", v.(*NumberHolder).Age, structResults[i].(*NumberHolder).Age != v.(*NumberHolder).Age)
if structResults[i].(*NumberHolder).Age != v.(*NumberHolder).Age {
match = false
break
}
i++
}
assert.True(t, match, "results dont match the original stage")
}
func TestProcess_empty(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())
c := make(chan interface{})
close(c)
//to json
jsonResults, err := Process(c, process, 1)
if err != nil {
panic(err)
}
assert.Equal(t, 0, len(jsonResults), "results dont match the original stage")
}
func TestProcessSlice(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())
maxIndexSize := 1000
inputsSlice := loadToStuctSlice(maxIndexSize)
//to json
jsonResults, err := ProcessSlice(inputsSlice, process, 1)
if err != nil {
panic(err)
}
//from json
structResults, err := ProcessSlice(jsonResults, fromJson, 1)
if err != nil {
panic(err)
}
match := true
for i, v := range loadToStuctSlice(maxIndexSize) {
fmt.Println(structResults[i].(*NumberHolder).Age, "!=", v.(*NumberHolder).Age, structResults[i].(*NumberHolder).Age != v.(*NumberHolder).Age)
if structResults[i].(*NumberHolder).Age != v.(*NumberHolder).Age {
match = false
break
}
}
assert.True(t, match, "results dont match the original stage")
}
func process(obj interface{}) (interface{}, error) {
time.Sleep(300* time.Millisecond)
return json.Marshal(obj)
}
func fromJson(obj interface{}) (interface{}, error) {
n := NumberHolder{}
if err := json.Unmarshal(obj.([]byte), &n); err != nil {
return nil, err
}
return &n, nil
}
type NumberHolder struct {
Age int `json:"age"`
}
func loadFromJson(results []interface{}, maxNum int) chan interface{} {
ch := make(chan interface{}, maxNum)
for i := 0; i < maxNum; i++ {
ch <- results[i]
}
close(ch)
return ch
}
func loadToStuct(maxNum int) chan interface{} {
ch := make(chan interface{})
go func() {
defer close(ch)
for i := 0; i < maxNum; i++ {
ch <- &NumberHolder{Age: i}
}
}()
return ch
}
func loadToStuctSlice(maxNum int) []interface{} {
s := make([]interface{}, maxNum)
for i := 0; i < maxNum; i++ {
s[i] = &NumberHolder{Age: i}
}
return s
}