-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconnectionpool_test.go
142 lines (111 loc) · 3.21 KB
/
connectionpool_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
131
132
133
134
135
136
137
138
139
140
141
142
package resolvermt
import (
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestServerPoolNew(t *testing.T) {
tests := []struct {
name string
haveInitCount int
haveMaxCount int
haveIPAddrPort string
wantCount int
wantErr bool
}{
{name: "Single", haveInitCount: 1, haveMaxCount: 1, haveIPAddrPort: "8.8.8.8:53", wantCount: 1, wantErr: false},
{name: "Couple", haveInitCount: 2, haveMaxCount: 2, haveIPAddrPort: "8.8.8.8:53", wantCount: 2, wantErr: false},
{name: "Invalid", haveInitCount: 1, haveMaxCount: 1, haveIPAddrPort: "invalid", wantCount: 0, wantErr: true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
pool, gotErr := newConnectionPool(test.haveInitCount, test.haveMaxCount, test.haveIPAddrPort)
if gotErr == nil {
gotCount := pool.Count()
assert.Equal(t, test.wantCount, gotCount)
}
assert.Equal(t, test.wantErr, gotErr != nil)
})
}
}
func TestServerPoolGet(t *testing.T) {
tests := []struct {
name string
haveInitCount int
haveMaxCount int
haveIPAddrPort string
wantCount int
wantErr bool
}{
{name: "On Demand", haveInitCount: 0, haveMaxCount: 1, haveIPAddrPort: "8.8.8.8:53", wantCount: 1, wantErr: false},
{name: "Max Conn", haveInitCount: 1, haveMaxCount: 1, haveIPAddrPort: "8.8.8.8:53", wantCount: 1, wantErr: false},
{name: "No Conn", haveInitCount: 0, haveMaxCount: 0, haveIPAddrPort: "8.8.8.8:53", wantCount: 0, wantErr: true},
{name: "Failed", haveInitCount: 0, haveMaxCount: 1, haveIPAddrPort: "invalid", wantCount: 0, wantErr: true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
pool, err := newConnectionPool(test.haveInitCount, test.haveMaxCount, test.haveIPAddrPort)
if err != nil {
t.Fatal("pool creation failed")
}
_, _, gotErr := pool.Get()
assert.Equal(t, test.wantCount, pool.Count())
assert.Equal(t, test.wantErr, gotErr != nil)
})
}
}
func TestServerPoolReturn(t *testing.T) {
pool, err := newConnectionPool(0, 1, "8.8.8.8:53")
if err != nil {
t.Fatal("pool creation failed")
}
// First request
_, connA, err := pool.Get()
assert.Nil(t, err)
// Blocking request
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
_, connB, err := pool.Get()
assert.Nil(t, err)
assert.Equal(t, connA, connB)
wg.Done()
}()
// Unblock by returning connection
time.Sleep(100 * time.Millisecond)
pool.Return(connA)
wg.Wait()
}
func TestServerPoolGetFail(t *testing.T) {
pool, err := newConnectionPool(0, 2, "8.8.8.8:53")
if err != nil {
t.Fatal("pool creation failed")
}
// First request
_, connA, err := pool.Get()
assert.Nil(t, err)
// Ensure the second request fails to create a connection
pool.IPAddrPort = "invalid"
// Start the second request
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
_, connB, _ := pool.Get()
assert.Equal(t, connA, connB)
wg.Done()
}()
// Return connA to unblock goroutine
time.Sleep(100 * time.Millisecond)
pool.Return(connA)
wg.Wait()
}
func TestServerPoolClose(t *testing.T) {
pool, err := newConnectionPool(2, 2, "8.8.8.8:53")
if err != nil {
t.Fatal("pool creation failed")
}
pool.Close()
_, open := <-pool.channel
assert.False(t, open)
}