-
Notifications
You must be signed in to change notification settings - Fork 2
/
cpool_test.go
110 lines (82 loc) · 2.38 KB
/
cpool_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
package rwdb
import (
"database/sql"
"testing"
)
func TestNextInPool(t *testing.T) {
var c = CPool{pool: []*sql.DB{}}
if got := c.nextInPool(); got != -1 {
t.Errorf("expect next position to be -1, got %d", got)
}
c.AddReader(&sql.DB{})
if got := c.nextInPool(); got != 0 {
t.Errorf("expect next position to be 1, got %d", got)
}
for n := 1; n < 5; n++ {
c.nextInPool()
}
if c.next != 0 {
t.Errorf("expect next position to be 0, got %d", c.next)
}
}
func TestAddReader(t *testing.T) {
var c = CPool{pool: make([]*sql.DB, 5)}
c.AddReader(&sql.DB{})
if len(c.pool) != 5 {
t.Errorf("expect number of connections in pool to be 5, got %d", len(c.pool))
t.Error(c.pool)
}
c = CPool{pool: make([]*sql.DB, 1)}
c.AddReader(&sql.DB{})
if len(c.pool) != 2 {
t.Errorf("expect number of connections in pool to be 2, got %d", len(c.pool))
}
}
func TestAddWriter(t *testing.T) {
var c = CPool{pool: []*sql.DB{}}
db1 := &sql.DB{}
c.AddWriter(db1)
if c.pool[0] != db1 {
t.Errorf("expect writer to be %v, got %v instead", db1, c.pool[0])
}
db2 := &sql.DB{}
c.AddWriter(db2)
if c.pool[0] != db2 {
t.Errorf("expect writer to be %v, got %v instead", db2, c.pool[0])
}
if c.pool[1] != db1 {
t.Errorf("expect reader to be %v, got %v instead", db1, c.pool[1])
}
}
func TestGetReader(t *testing.T) {
var c = CPool{pool: []*sql.DB{}}
if db, err := c.Reader(); err == nil {
t.Errorf("get reader from empty cpool expect to return err, got %v instead", db)
}
c = CPool{pool: make([]*sql.DB, 2)}
if db, err := c.Reader(); err == nil {
t.Errorf("get reader from nil cpool expect to return err, got %v instead", db)
}
c.AddReader(&sql.DB{})
if db, _ := c.Reader(); db == nil {
t.Errorf("expect db, got %v instead", db)
}
}
func TestGetWriter(t *testing.T) {
var c = CPool{pool: []*sql.DB{}}
if db, err := c.Writer(); err == nil {
t.Errorf("get writer from empty cpool expect to return err, got %v instead", db)
}
c = CPool{pool: []*sql.DB{}}
if db, err := c.Writer(); err == nil {
t.Errorf("get writer from nil cpool expect to return err, got %v instead", db)
}
c.AddWriter(&sql.DB{})
if db, err := c.Writer(); err != nil {
t.Errorf("get writer from cpool expect to return db, got %v instead", db)
}
var d = CPool{pool: []*sql.DB{nil}}
if db, err := d.Writer(); err == nil {
t.Errorf("get writer from empty cpool expect to return err, got %v instead", db)
}
}