-
Notifications
You must be signed in to change notification settings - Fork 0
/
drumline_test.go
118 lines (112 loc) · 2.99 KB
/
drumline_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
package drumline
import (
"testing"
"time"
"log"
)
func TestProgresLimiter(t *testing.T) {
dl := NewDrumline(5)
dl.Add(0)
dl.Add(1)
dl.Add(2)
x := false
y := false
go func() {
for i := 0; i < 7; i++ { dl.Step(0) }
x = true
}()
go func() {
for i := 0; i < 7; i++ { dl.Step(1) }
y = true
}()
time.Sleep(100 * time.Millisecond)
if x { t.Errorf("Progress should have been limited (x)") }
if y { t.Errorf("Progress should have been limited (y)") }
dl.Step(2)
dl.Step(2)
time.Sleep(100 * time.Millisecond)
if !x { t.Errorf("Progress should have been able to proceed (x)") }
if !y { t.Errorf("Progress should have been able to proceed (y)") }
dl.Close()
}
func TestReset(t *testing.T) {
dl := NewDrumline(5)
dl.Add(0)
dl.Add(1)
dl.Add(2)
x := false
y := false
go func() {
for i := 0; i < 7; i++ { dl.Step(0) }
x = true
}()
go func() {
for i := 0; i < 7; i++ { dl.Step(1) }
y = true
}()
time.Sleep(100 * time.Millisecond)
if x { t.Errorf("Progress should have been limited (x)") }
if y { t.Errorf("Progress should have been limited (y)") }
<-dl.Reset(1 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
if !x { t.Errorf("Progress should have been able to proceed (x)") }
if !y { t.Errorf("Progress should have been able to proceed (y)") }
dl.Close()
if x := dl.Reset(0); x != nil {
t.Errorf("Reset on closed drumline should return nil channel, got %v", x)
}
}
func TestProgresLimiterScaled(t *testing.T) {
dl := NewDrumline(5)
dl.AddScale(0, 1)
dl.AddScale(1, 10)
dl.AddScale(2, 100)
x := false
y := false
go func() {
for i := 0; i < 7; i++ { dl.Step(0) }
x = true
}()
go func() {
for i := 0; i < 70; i++ { dl.Step(1) }
y = true
}()
time.Sleep(100 * time.Millisecond)
if x { t.Errorf("Progress should have been limited (x)") }
if y { t.Errorf("Progress should have been limited (y)") }
log.Printf("0: %v, 1: %v, 2: %v", *dl.steps[0], *dl.steps[1], *dl.steps[2])
for i := 0; i < 700; i++ {
dl.Step(2)
}
time.Sleep(100 * time.Millisecond)
if !x { t.Errorf("Progress should have been able to proceed (x)") }
if !y { t.Errorf("Progress should have been able to proceed (y)") }
dl.Close()
}
func TestProgresLimiterScaledFlipped(t *testing.T) {
dl := NewDrumline(5)
dl.AddScale(0, 1)
dl.AddScale(1, 10)
dl.AddScale(2, 100)
x := false
y := false
go func() {
for i := 0; i < 700; i++ { dl.Step(2) }
x = true
}()
go func() {
for i := 0; i < 70; i++ { dl.Step(1) }
y = true
}()
time.Sleep(100 * time.Millisecond)
if x { t.Errorf("Progress should have been limited (x)") }
if y { t.Errorf("Progress should have been limited (y)") }
log.Printf("0: %v, 1: %v, 2: %v", *dl.steps[0], *dl.steps[1], *dl.steps[2])
for i := 0; i < 7; i++ {
dl.Step(0)
}
time.Sleep(100 * time.Millisecond)
if !x { t.Errorf("Progress should have been able to proceed (x)") }
if !y { t.Errorf("Progress should have been able to proceed (y)") }
dl.Close()
}