forked from cubefs/cubefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlimit_test.go
123 lines (99 loc) · 2.28 KB
/
limit_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
package datanode
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/time/rate"
)
func TestInitRepairLimit(t *testing.T) {
initRepairLimit()
assert.Equal(t, MaxExtentRepairLimit, len(extentRepairLimitRater))
}
func TestRequestDoExtentRepair(t *testing.T) {
initRepairLimit()
go func() {
<-extentRepairLimitRater
}()
err := requestDoExtentRepair()
require.NoError(t, err)
stop := false
for {
if stop {
break
}
select {
case <-extentRepairLimitRater:
// do nothing
default:
stop = true
break
}
}
close(extentRepairLimitRater)
err = requestDoExtentRepair()
require.NoError(t, err)
}
func TestFininshDoExtentRepair(t *testing.T) {
initRepairLimit()
setDoExtentRepair(2)
err := requestDoExtentRepair()
require.NoError(t, err)
stop := false
for {
if stop {
break
}
select {
case <-extentRepairLimitRater:
// do nothing
default:
stop = true
break
}
}
// finishDoExtentRepair() will send a struct{} to extentRepairLimitRater
fininshDoExtentRepair()
select {
case <-extentRepairLimitRater:
if len(extentRepairLimitRater) != 0 {
t.Fatalf("extentRepairLimitRater should be empty, but len(extentRepairLimitRater) is %v", len(extentRepairLimitRater))
}
default:
}
}
func TestSetDoExtentRepair(t *testing.T) {
initRepairLimit()
setDoExtentRepair(15)
assert.Equal(t, 15, CurExtentRepairLimit)
setDoExtentRepair(-10)
assert.Equal(t, MaxExtentRepairLimit, CurExtentRepairLimit)
setDoExtentRepair(0)
assert.Equal(t, MaxExtentRepairLimit, CurExtentRepairLimit)
setDoExtentRepair(25000)
assert.Equal(t, MaxExtentRepairLimit, CurExtentRepairLimit)
setDoExtentRepair(1)
assert.Equal(t, MinExtentRepairLimit, CurExtentRepairLimit)
}
func TestDeleteLimiterWait(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
done := make(chan struct{})
go func() {
DeleteLimiterWait()
close(done)
}()
select {
case <-done:
case <-ctx.Done():
t.Fatalf("DeleteLimiterWait took too long")
}
}
func TestSetLimiter(t *testing.T) {
limiter := rate.NewLimiter(1, 1)
setLimiter(limiter, 10)
assert.Equal(t, rate.Limit(10), limiter.Limit())
setLimiter(limiter, 0)
assert.Equal(t, rate.Inf, limiter.Limit())
}