-
Notifications
You must be signed in to change notification settings - Fork 7
/
tools_test.go
104 lines (74 loc) · 1.82 KB
/
tools_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
// Copyright © 2016-present Thomas Rabaix <thomas.rabaix@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package pkgmirror
import (
"sync/atomic"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_Compress(t *testing.T) {
d := []byte("Hello")
c, err := Compress(d)
assert.NoError(t, err)
assert.True(t, len(c) > 0)
}
func Test_Compress_EmptyData(t *testing.T) {
d := []byte("")
c, err := Compress(d)
assert.NoError(t, err)
assert.True(t, len(c) > 0)
}
func Test_Decompress(t *testing.T) {
c, err := Compress([]byte("Hello"))
assert.NoError(t, err)
d, err := Decompress(c)
assert.NoError(t, err)
assert.Equal(t, []byte("Hello"), d)
}
func Test_WorkerManager_WorkerNumber(t *testing.T) {
// should be called 10 times
var cpt int32
m := NewWorkerManager(10, func(id int, data <-chan interface{}, result chan interface{}) {
atomic.AddInt32(&cpt, 1)
})
m.Start()
m.Wait()
assert.Equal(t, cpt, int32(10))
}
type chnStruct struct {
v int32
}
func Test_WorkerManager_DataIn(t *testing.T) {
// should be called 10 times
var cpt int32
m := NewWorkerManager(5, func(id int, data <-chan interface{}, result chan interface{}) {
for raw := range data {
atomic.AddInt32(&cpt, raw.(chnStruct).v)
}
})
m.Start()
m.Add(chnStruct{v: 5})
m.Add(chnStruct{v: 5})
m.Add(chnStruct{v: 5})
m.Wait()
assert.Equal(t, cpt, int32(15))
}
func Test_WorkerManager_Result(t *testing.T) {
var cpt int32
m := NewWorkerManager(5, func(id int, data <-chan interface{}, result chan interface{}) {
for raw := range data {
result <- raw
}
})
m.ResultCallback(func(raw interface{}) {
cpt += raw.(chnStruct).v
})
m.Start()
m.Add(chnStruct{v: 5})
m.Add(chnStruct{v: 5})
m.Add(chnStruct{v: 5})
m.Wait()
assert.Equal(t, int32(15), cpt)
}