-
Notifications
You must be signed in to change notification settings - Fork 0
/
balancer_test.go
130 lines (98 loc) · 2.15 KB
/
balancer_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
126
127
128
129
130
package golang_balancer
import (
"github.com/pkg/errors"
"testing"
"time"
)
func getInstance() (b *Balancer) {
var (
ch = make(chan interface{})
handler = func(job interface{}) (err error) {
println(job.(string))
return
}
err = make(chan error)
)
b = NewBalancer(ch, handler, err, 10)
return
}
func TestInit(t *testing.T) {
b := getInstance()
if b.CountPerSecond() != 10 {
t.Fatal("Can't init balancer correctly")
}
}
func TestIncrease(t *testing.T) {
b := getInstance()
b.Increase()
b.Increase()
if b.CountPerSecond() != 12 {
t.Fatal("Can't increase amount goroutins in balancer")
}
}
func TestDecrease(t *testing.T) {
b := getInstance()
b.Decrease()
b.Decrease()
if b.CountPerSecond() != 8 {
t.Fatal("Can't decrease amount goroutins in balancer")
}
}
func TestIncreaseWithLimitation(t *testing.T) {
b := getInstance()
b.SetMax(11)
b.Increase()
b.Increase()
if b.CountPerSecond() != 11 {
t.Fatal("Can't increase (with limitation) amount goroutins in balancer")
}
}
func TestDecreaseWithLimitation(t *testing.T) {
b := getInstance()
b.SetMin(9)
b.Decrease()
b.Decrease()
if b.CountPerSecond() != 9 {
t.Fatal("Can't decrease (with limitation) amount goroutins in balancer")
}
}
func TestErrChan(t *testing.T) {
var (
ch = make(chan interface{})
handler = func(job interface{}) (err error) {
return errors.New("some error")
}
err = make(chan error)
)
NewBalancer(ch, handler, err, 10)
ch <- "some job"
time.AfterFunc(5*time.Second, func() {
t.Fatal("Can't get error from channel")
})
<-err
}
func TestCountPerSecond(t *testing.T) {
b := getInstance()
b.SetCountPerSecond(11)
if b.CountPerSecond() != 11 {
t.Fatal("Can't get expected count per second value")
}
}
func TestEfficiency(t *testing.T) {
var (
ch = make(chan interface{})
handler = func(job interface{}) (err error) {
return
}
err = make(chan error)
)
b := NewBalancer(ch, handler, err, 10)
time.AfterFunc(1100*time.Millisecond, func() {
if b.Efficiency() != 10 {
t.Fatal("Can't balance necessary amount jobs execution per second")
}
})
for i := 0; i < 12; i++ {
ch <- "some job"
}
}