forked from go-gorm/dbresolver
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolicy_test.go
40 lines (34 loc) · 793 Bytes
/
policy_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
package dbresolver
import (
"sync/atomic"
"testing"
"gorm.io/gorm"
)
func TestPolicy_RoundRobinPolicy(t *testing.T) {
var p1, p2, p3 gorm.ConnPool
var pools = []gorm.ConnPool{
p1, p2, p3,
}
for i := 0; i < 10; i++ {
if pools[i%3] != RoundRobinPolicy().Resolve(pools) {
t.Errorf("RoundRobinPolicy failed")
}
if pools[i%3] != StrictRoundRobinPolicy().Resolve(pools) {
t.Errorf("StrictRoundRobinPolicy failed")
}
}
}
func BenchmarkPolicy_StrictRoundRobinPolicy(b *testing.B) {
var p1, p2, p3 gorm.ConnPool
var pools = []gorm.ConnPool{
p1, p2, p3,
}
var i int64
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if pools[int(atomic.AddInt64(&i, 1))%3] != StrictRoundRobinPolicy().Resolve(pools) {
b.Errorf("RoundRobinPolicy failed")
}
}
})
}