-
Notifications
You must be signed in to change notification settings - Fork 11
/
connection_test.go
executable file
·113 lines (96 loc) · 2.72 KB
/
connection_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
package dsc_test
import (
"errors"
"github.com/stretchr/testify/assert"
"github.com/viant/dsc"
"testing"
"time"
)
func TestConnectionConfig(t *testing.T) {
config := &dsc.Config{}
connection := newTestConnection(config)
assert.Equal(t, config, connection.Config())
assert.Nil(t, connection.Begin())
assert.Nil(t, connection.Commit())
assert.Nil(t, connection.Rollback())
}
func TestConnectionProvider(t *testing.T) {
{
config := &dsc.Config{}
provider := newTestConnectionProvider(config)
connection, err := provider.Get()
assert.Nil(t, err)
assert.NotNil(t, connection)
provider.Close()
}
{
config := &dsc.Config{MaxPoolSize: 3, PoolSize: 2}
provider := newTestConnectionProvider(config)
for i := 0; i < 3; i++ {
connection, err := provider.Get()
assert.Nil(t, err)
assert.NotNil(t, connection)
}
provider.Close()
}
{
config := &dsc.Config{MaxPoolSize: 3, PoolSize: 2}
provider := newTestConnectionProvider(config)
provider.error = errors.New("Test error")
_, err := provider.Get()
assert.NotNil(t, err)
}
{
config := &dsc.Config{MaxPoolSize: 3, PoolSize: 2}
provider := newTestConnectionProvider(config)
sleep := int(2 * time.Second)
provider.sleep = &sleep
connection, err := provider.Get()
assert.Nil(t, err)
assert.NotNil(t, connection)
}
}
type testConnection struct {
*dsc.AbstractConnection
}
func (t *testConnection) CloseNow() error {
return nil
}
func newTestConnection(config *dsc.Config) dsc.Connection {
if config.MaxPoolSize == 0 {
config.MaxPoolSize = 1
}
testConnection := &testConnection{}
connection := dsc.NewAbstractConnection(config, make(chan dsc.Connection, config.MaxPoolSize), testConnection)
testConnection.AbstractConnection = connection
return testConnection
}
type testConnectionProvider struct {
*dsc.AbstractConnectionProvider
sleep *int
error error
}
func (cp *testConnectionProvider) NewConnection() (dsc.Connection, error) {
if cp.sleep != nil {
time.Sleep(time.Duration(*cp.sleep))
}
if cp.error != nil {
return nil, cp.error
}
config := cp.Config()
var testConnection = &testConnection{}
var connection = testConnection
var super = dsc.NewAbstractConnection(config, cp.ConnectionProvider.ConnectionPool(), connection)
testConnection.AbstractConnection = super
return connection, nil
}
func newTestConnectionProvider(config *dsc.Config) *testConnectionProvider {
if config.MaxPoolSize == 0 {
config.MaxPoolSize = 1
}
testConnectionProvider := &testConnectionProvider{}
var connectionProvider = testConnectionProvider
super := dsc.NewAbstractConnectionProvider(config, make(chan dsc.Connection, config.MaxPoolSize), connectionProvider)
testConnectionProvider.AbstractConnectionProvider = super
return connectionProvider
}