-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpool_test.go
139 lines (104 loc) · 3.01 KB
/
pool_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
// Copyright 2021 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package garment
import (
"errors"
"testing"
"github.com/franela/goblin"
)
type DatabaseClient struct {
Connection int
}
func (d *DatabaseClient) Execute() int {
return d.Connection
}
func (d *DatabaseClient) Terminate() {
d.Connection = 0
}
func (d *DatabaseClient) Close() {
d.Connection = 0
}
func (d *DatabaseClient) Reconnect() {
d.Connection = 1
}
func (d *DatabaseClient) Ping() bool {
if d.Connection == 1 {
return true
}
return false
}
// TestUnitPool test cases
func TestUnitPool(t *testing.T) {
g := goblin.Goblin(t)
g.Describe("#Pool", func() {
g.It("It should satisfy all provided test cases", func() {
pool1 := NewPool()
pool2 := NewPool()
g.Assert(pool1.Count()).Equal(0)
g.Assert(pool2.Count()).Equal(0)
ping := func(con interface{}) error {
if con.(*DatabaseClient).Ping() {
return nil
}
return errors.New("DB connection is lost")
}
close := func(con interface{}) error {
con.(*DatabaseClient).Close()
return nil
}
reconnect := func(con interface{}) error {
con.(*DatabaseClient).Reconnect()
return nil
}
pool1.Set("db", &DatabaseClient{Connection: 1}, ping, close, reconnect)
// Concurrent Access
go func() {
for i := 0; i < 100; i++ {
if pool1.Has("db") && pool1.Ping("db") == nil {
g.Assert(pool1.Get("db").(*DatabaseClient).Execute()).Equal(1)
}
}
}()
go func() {
for i := 0; i < 100; i++ {
if pool1.Has("db") && pool1.Ping("db") == nil {
g.Assert(pool1.Get("db").(*DatabaseClient).Execute()).Equal(1)
}
}
}()
g.Assert(pool1.Count()).Equal(1)
g.Assert(pool2.Count()).Equal(1)
g.Assert(pool1.Has("db")).Equal(true)
g.Assert(pool2.Has("db")).Equal(true)
g.Assert(pool1.Has("db1")).Equal(false)
g.Assert(pool2.Has("db1")).Equal(false)
g.Assert(pool1.Get("db").(*DatabaseClient).Execute()).Equal(1)
g.Assert(pool2.Get("db").(*DatabaseClient).Execute()).Equal(1)
// Terminate Connection
pool2.Get("db").(*DatabaseClient).Terminate()
g.Assert(pool1.Get("db").(*DatabaseClient).Execute()).Equal(0)
pool1.Reconnect("db")
g.Assert(pool1.Get("db").(*DatabaseClient).Execute()).Equal(1)
pool2.Remove("db")
pool1.Reconnect("db")
pool2.Remove("db")
pool2.Ping("db")
g.Assert(pool1.Has("db")).Equal(false)
g.Assert(pool2.Has("db")).Equal(false)
g.Assert(pool1.Count()).Equal(0)
g.Assert(pool2.Count()).Equal(0)
pool2.Set("db", &DatabaseClient{Connection: 1}, ping, close, reconnect)
g.Assert(pool1.Count()).Equal(1)
g.Assert(pool2.Count()).Equal(1)
g.Assert(pool1.Ping("db") == nil).Equal(true)
g.Assert(pool2.Ping("db") == nil).Equal(true)
pool1.Close("db")
pool1.Close("db1")
g.Assert(pool1.Ping("db") == nil).Equal(false)
g.Assert(pool2.Ping("db") == nil).Equal(false)
g.Assert(pool1.Count()).Equal(1)
g.Assert(pool2.Count()).Equal(1)
})
})
}