-
Notifications
You must be signed in to change notification settings - Fork 52
/
core_test.clj
261 lines (227 loc) · 10.6 KB
/
core_test.clj
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
(ns hikari-cp.core-test
(:require [hikari-cp.core :refer :all])
(:use expectations)
(:import (com.zaxxer.hikari.pool HikariPool$PoolInitializationException)
(com.codahale.metrics MetricRegistry)
(com.codahale.metrics.health HealthCheckRegistry)
(com.zaxxer.hikari.metrics.prometheus PrometheusMetricsTrackerFactory)))
(def valid-options
{:auto-commit false
:read-only true
:connection-timeout 1000
:validation-timeout 1000
:idle-timeout 0
:max-lifetime 0
:minimum-idle 0
:maximum-pool-size 1
:pool-name "db-pool"
:adapter "postgresql"
:username "username"
:password "password"
:database-name "database"
:server-name "host-1"
:port-number 5433
:connection-init-sql "set join_collapse_limit=4"
:connection-test-query "select 0"
:register-mbeans true
:transaction-isolation "TRANSACTION_SERIALIZABLE"
#_:leak-detection-threshold #_4000}) ; a valid option but tested separately below.
(def alternate-valid-options
{:driver-class-name "org.postgresql.ds.PGPoolingDataSource"
:jdbc-url "jdbc:postgresql://localhost:5433/test"})
(def alternate-valid-options2
{:datasource-class-name "com.sybase.jdbc3.jdbc.SybDataSource"})
(def metric-registry-options
{:metric-registry (MetricRegistry.)})
(def health-check-registry-options
{:health-check-registry (HealthCheckRegistry.)})
(def metrics-tracker-factory-options
{:metrics-tracker-factory (PrometheusMetricsTrackerFactory.)})
(def datasource-config-with-required-settings
(datasource-config (apply dissoc valid-options (keys default-datasource-options))))
(def datasource-config-with-overrides
(datasource-config valid-options))
(def datasource-config-with-overrides-alternate
(datasource-config (-> (dissoc valid-options :adapter)
(merge alternate-valid-options))))
(def datasource-config-with-overrides-alternate2
(datasource-config (-> (dissoc valid-options :adapter)
(merge alternate-valid-options2))))
(def mysql8-datasource-config
(datasource-config (merge valid-options {:adapter "mysql8"})))
(def mysql-datasource-config
(datasource-config (merge valid-options
{:adapter "mysql"
:use-legacy-datetime-code false})))
(def metric-registry-config (datasource-config (merge valid-options metric-registry-options)))
(def health-check-registry-config (datasource-config (merge valid-options health-check-registry-options)))
(def metrics-tracker-factory-config (datasource-config (merge valid-options metrics-tracker-factory-options)))
(expect false
(get (.getDataSourceProperties mysql-datasource-config) "useLegacyDatetimeCode"))
(expect "com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
(.getDataSourceClassName mysql-datasource-config))
(expect "com.mysql.cj.jdbc.MysqlDataSource"
(.getDataSourceClassName mysql8-datasource-config))
(expect true
(.isAutoCommit datasource-config-with-required-settings))
(expect false
(.isReadOnly datasource-config-with-required-settings))
(expect 30000
(.getConnectionTimeout datasource-config-with-required-settings))
(expect 5000
(.getValidationTimeout datasource-config-with-required-settings))
(expect 600000
(.getIdleTimeout datasource-config-with-required-settings))
(expect 1800000
(.getMaxLifetime datasource-config-with-required-settings))
(expect 10
(.getMinimumIdle datasource-config-with-required-settings))
(expect 10
(.getMaximumPoolSize datasource-config-with-required-settings))
(expect "org.postgresql.ds.PGSimpleDataSource"
(.getDataSourceClassName datasource-config-with-required-settings))
(expect "username"
(.getUsername datasource-config-with-required-settings))
(expect "password"
(.getPassword datasource-config-with-required-settings))
(expect 5433
(-> datasource-config-with-required-settings
.getDataSourceProperties
(get "portNumber")))
(expect nil
(.getMetricRegistry datasource-config-with-required-settings))
(expect (:metric-registry metric-registry-options)
(.getMetricRegistry metric-registry-config))
(expect nil
(.getHealthCheckRegistry datasource-config-with-required-settings))
(expect (:health-check-registry health-check-registry-options)
(.getHealthCheckRegistry health-check-registry-config))
(expect nil
(.getMetricsTrackerFactory datasource-config-with-required-settings))
(expect (:metrics-tracker-factory metrics-tracker-factory-options)
(.getMetricsTrackerFactory metrics-tracker-factory-config))
(expect "TRANSACTION_SERIALIZABLE"
(.getTransactionIsolation datasource-config-with-required-settings))
(expect false
(.isAutoCommit datasource-config-with-overrides))
(expect true
(.isReadOnly datasource-config-with-overrides))
(expect 1000
(.getConnectionTimeout datasource-config-with-overrides))
(expect 1000
(.getValidationTimeout datasource-config-with-overrides))
(expect 0
(.getIdleTimeout datasource-config-with-overrides))
(expect 0
(.getMaxLifetime datasource-config-with-overrides))
(expect 0
(.getMinimumIdle datasource-config-with-overrides))
(expect 1
(.getMaximumPoolSize datasource-config-with-overrides))
(expect "db-pool"
(.getPoolName datasource-config-with-overrides))
(expect "set join_collapse_limit=4"
(.getConnectionInitSql datasource-config-with-overrides))
(expect "select 0"
(.getConnectionTestQuery datasource-config-with-overrides))
(expect true
(.isRegisterMbeans datasource-config-with-overrides))
(expect "org.postgresql.ds.PGPoolingDataSource"
(.getDriverClassName datasource-config-with-overrides-alternate))
(expect "jdbc:postgresql://localhost:5433/test"
(.getJdbcUrl datasource-config-with-overrides-alternate))
(expect "com.sybase.jdbc3.jdbc.SybDataSource"
(.getDataSourceClassName datasource-config-with-overrides-alternate2))
(expect IllegalArgumentException
(datasource-config (dissoc valid-options :adapter)))
(expect #"contains\? % :adapter"
(try
(datasource-config (validate-options (dissoc valid-options :adapter)))
(catch IllegalArgumentException e
(str (.getMessage e)))))
(expect "jdbc:postgres:test"
(.getJdbcUrl (datasource-config {:jdbc-url "jdbc:postgres:test"})))
(expect map?
(validate-options valid-options))
(expect IllegalArgumentException
(validate-options (merge valid-options {:auto-commit 1})))
(expect IllegalArgumentException
(validate-options (merge valid-options {:read-only 1})))
(expect IllegalArgumentException
(validate-options (merge valid-options {:connection-timeout "foo"})))
(expect IllegalArgumentException
(validate-options (merge valid-options {:connection-timeout 999})))
(expect IllegalArgumentException
(validate-options (merge valid-options {:validation-timeout 999})))
(expect IllegalArgumentException
(validate-options (merge valid-options {:idle-timeout -1})))
(expect IllegalArgumentException
(validate-options (merge valid-options {:max-lifetime -1})))
(expect IllegalArgumentException
(validate-options (merge valid-options {:minimum-idle -1})))
(expect IllegalArgumentException
(validate-options (merge valid-options {:maximum-pool-size -1})))
(expect IllegalArgumentException
(validate-options (merge valid-options {:maximum-pool-size 0})))
(expect IllegalArgumentException
(validate-options (merge valid-options {:adapter :foo})))
(expect IllegalArgumentException
(validate-options (merge valid-options {:datasource-classname "adsf"})))
(expect IllegalArgumentException
(validate-options (merge (dissoc valid-options :adapter) {:jdbc-url nil})))
(expect IllegalArgumentException
(validate-options (merge (dissoc valid-options :adapter) {:jdbc-url "jdbc:h2:~/test"
:driver-class-name nil})))
(expect IllegalArgumentException
(validate-options (merge valid-options {:transaction-isolation 1})))
(expect map?
(validate-options (merge valid-options {:username nil})))
(expect map?
(validate-options (dissoc valid-options :username)))
(expect map?
(validate-options (dissoc valid-options :password)))
(expect map?
(validate-options (merge valid-options {:password nil})))
(expect map?
(validate-options (merge valid-options {:database-name nil})))
(expect map?
(validate-options (dissoc valid-options :database-name)))
(expect map?
(validate-options (dissoc valid-options :server-name)))
(expect map?
(validate-options (merge valid-options {:server-name nil})))
(expect map?
(validate-options (merge valid-options {:port-number -1})))
(expect map?
(validate-options (dissoc valid-options :port-number)))
(expect map?
(validate-options (merge (dissoc valid-options :adapter) {:jdbc-url "jdbc:h2:~/test"})))
(expect map?
(validate-options (merge (dissoc valid-options :adapter) {:jdbc-url "jdbc:h2:~/test"
:driver-class-name "org.h2.Driver"})))
;; -- check leak detections option
;; default should stay 0
(expect 0 (-> valid-options
(datasource-config)
(.getLeakDetectionThreshold)))
;; it should apply a correct value
(let [config (datasource-config (assoc valid-options :leak-detection-threshold 3000))]
(expect 3000 (.getLeakDetectionThreshold config)))
;; it should complain, that value is too small
(expect IllegalArgumentException
(validate-options (assoc valid-options :leak-detection-threshold 1)))
(expect IllegalArgumentException
(validate-options (assoc valid-options :leak-detection-threshold 1999)))
;; Ensure that core options aren't being set as datasource properties
(expect #{"portNumber" "databaseName" "serverName"}
(set (keys (.getDataSourceProperties metric-registry-config))))
(expect HikariPool$PoolInitializationException
(make-datasource valid-options))
(expect "tinyInt1isBit" (translate-property :tinyInt1isBit))
(expect "tinyInt1isBit" (translate-property :tiny-int1is-bit))
(expect "useSSL" (translate-property :useSSL))
(expect "useSSL" (translate-property :use-ssl))
(expect "useFoo" (translate-property :useFOO))
;; translate-property is extensible
(defmethod translate-property ::extend-translate-test [_] 42)
(expect 42 (translate-property ::extend-translate-test))