-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcrate_test.go
157 lines (141 loc) · 5.29 KB
/
crate_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"context"
"runtime"
"testing"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/stretchr/testify/require"
)
var CPU_COUNT = int32(runtime.NumCPU())
func TestNewCrateEndpoint(t *testing.T) {
conf := builtinConfig()
conf.Endpoints[0].Password = "foobar+&%"
conf.Endpoints[0].Schema = "testdrive"
endpoint := newCrateEndpoint(&conf.Endpoints[0])
require.Equal(t,
"host=localhost port=5432 user=crate password=foobar+&% database=testdrive connect_timeout=10",
endpoint.poolConf.ConnString(),
)
require.GreaterOrEqual(t, endpoint.poolConf.MaxConns, CPU_COUNT)
}
func TestConfigurePoolDefault(t *testing.T) {
/**
* Verify that, by default, the pool size got configured with one connection per core by default.
**/
poolConf, _ := pgxpool.ParseConfig("postgres://crate:foo@localhost:5432/")
require.Equal(t, "localhost", poolConf.ConnConfig.Host)
require.GreaterOrEqual(t, poolConf.MaxConns, CPU_COUNT)
ctx := context.Background()
pool, err := createPool(ctx, poolConf)
require.IsType(t, &pgxpool.Pool{}, pool)
require.Equal(t, err, nil)
require.GreaterOrEqual(t, pool.Config().MaxConns, CPU_COUNT)
}
func TestConfigurePoolWithPoolSizeFromConnectionString(t *testing.T) {
/**
* Verify that the pool size gets obtained from the database connection string.
**/
poolConf, _ := pgxpool.ParseConfig("postgres://crate:foo@localhost:5432/?pool_max_conns=42")
require.Equal(t, "localhost", poolConf.ConnConfig.Host)
require.Equal(t, int32(42), poolConf.MaxConns)
ctx := context.Background()
pool, err := createPool(ctx, poolConf)
require.IsType(t, &pgxpool.Pool{}, pool)
require.Equal(t, err, nil)
require.Equal(t, int32(42), pool.Config().MaxConns)
}
func TestConfigurePoolWithPoolSizeFromSettingsVanilla(t *testing.T) {
/**
* Verify that the pool size can be configured using a configuration setting.
**/
poolConf, _ := pgxpool.ParseConfig("postgres://crate:foo@localhost:5432/")
require.Equal(t, "localhost", poolConf.ConnConfig.Host)
require.GreaterOrEqual(t, poolConf.MaxConns, CPU_COUNT)
ctx := context.Background()
pool, err := createPoolWithPoolSize(ctx, poolConf, 42)
require.IsType(t, &pgxpool.Pool{}, pool)
require.Equal(t, err, nil)
require.Equal(t, int32(42), pool.Config().MaxConns)
}
func TestConfigurePoolWithPoolSizeFromSettingsPrecedence(t *testing.T) {
/**
* Verify that the pool size configuration setting takes precedence over the connection string.
**/
poolConf, _ := pgxpool.ParseConfig("postgres://crate:foo@localhost:5432/?pool_max_conns=33")
require.Equal(t, "localhost", poolConf.ConnConfig.Host)
require.Equal(t, int32(33), poolConf.MaxConns)
ctx := context.Background()
pool, err := createPoolWithPoolSize(ctx, poolConf, 42)
require.IsType(t, &pgxpool.Pool{}, pool)
require.Equal(t, err, nil)
require.Equal(t, int32(42), pool.Config().MaxConns)
}
func TestPoolsDefault(t *testing.T) {
/**
* Verify connection pool sizes when not configured explicitly.
**/
conf := builtinConfig()
endpoint := newCrateEndpoint(&conf.Endpoints[0])
ctx := context.Background()
endpoint.createPools(ctx)
require.IsType(t, &pgxpool.Pool{}, endpoint.readPool)
require.Equal(t,
"host=localhost port=5432 user=crate connect_timeout=10",
endpoint.poolConf.ConnString(),
)
require.GreaterOrEqual(t, endpoint.readPool.Config().MaxConns, CPU_COUNT)
require.GreaterOrEqual(t, endpoint.writePool.Config().MaxConns, CPU_COUNT)
}
func TestPoolsWithMaxConnections(t *testing.T) {
/**
* Verify connection pool sizes when configured using `MaxConnections`.
**/
conf := builtinConfig()
conf.Endpoints[0].MaxConnections = 42
endpoint := newCrateEndpoint(&conf.Endpoints[0])
ctx := context.Background()
endpoint.createPools(ctx)
require.IsType(t, &pgxpool.Pool{}, endpoint.readPool)
require.Equal(t,
"host=localhost port=5432 user=crate connect_timeout=10 pool_max_conns=42",
endpoint.poolConf.ConnString(),
)
require.Equal(t, int32(42), endpoint.readPool.Config().MaxConns)
require.Equal(t, int32(42), endpoint.writePool.Config().MaxConns)
}
func TestPoolsWithIndividualPoolSizes(t *testing.T) {
/**
* Verify connection pool sizes when configured using `ReadPoolSize` and `WritePoolSize`.
**/
conf := builtinConfig()
conf.Endpoints[0].ReadPoolSize = 11
conf.Endpoints[0].WritePoolSize = 22
endpoint := newCrateEndpoint(&conf.Endpoints[0])
ctx := context.Background()
endpoint.createPools(ctx)
require.IsType(t, &pgxpool.Pool{}, endpoint.readPool)
require.Equal(t,
"host=localhost port=5432 user=crate connect_timeout=10",
endpoint.poolConf.ConnString(),
)
require.Equal(t, int32(11), endpoint.readPool.Config().MaxConns)
require.Equal(t, int32(22), endpoint.writePool.Config().MaxConns)
}
func TestPoolsWithMaxConnectionsAndIndividualPoolSizes(t *testing.T) {
/**
* Verify connection pool sizes when configured using `MaxConnections` and `ReadPoolSize`.
**/
conf := builtinConfig()
conf.Endpoints[0].MaxConnections = 5
conf.Endpoints[0].ReadPoolSize = 40
endpoint := newCrateEndpoint(&conf.Endpoints[0])
ctx := context.Background()
endpoint.createPools(ctx)
require.IsType(t, &pgxpool.Pool{}, endpoint.readPool)
require.Equal(t,
"host=localhost port=5432 user=crate connect_timeout=10 pool_max_conns=5",
endpoint.poolConf.ConnString(),
)
require.Equal(t, int32(40), endpoint.readPool.Config().MaxConns)
require.Equal(t, int32(5), endpoint.writePool.Config().MaxConns)
}