forked from minus5/gofreetds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn_test.go
383 lines (349 loc) · 8.94 KB
/
conn_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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package freetds
import (
"fmt"
"github.com/stretchrcom/testify/assert"
"os"
"strings"
"testing"
"time"
)
var CREATE_DB_SCRIPTS = [...]string{`
if exists(select * from sys.tables where name = 'freetds_types')
drop table freetds_types
;
create table freetds_types (
int int null,
long bigint null,
smallint smallint null,
tinyint tinyint null,
varchar varchar(255) null,
nvarchar nvarchar(255) null,
char char(255) null,
nchar nchar(255) null,
text text null,
ntext ntext null,
datetime datetime null,
smalldatetime smalldatetime null,
money money null,
smallmoney smallmoney null,
real real null,
float float(53) null,
bit bit null,
timestamp timestamp null,
binary binary(10) null
)
;
insert into freetds_types (int, long, smallint, tinyint, varchar, nvarchar, char, nchar, text, ntext, datetime, smalldatetime, money, smallmoney, real, float, bit, binary)
values (2147483647, 9223372036854775807, 32767, 255, 'išo medo u dućan ','išo medo u dućan 2','išo medo u dućan 3','išo medo u dućan 4','išo medo u dućan 5','išo medo u dućan 6', '1972-08-08T10:11:12','1972-08-08T10:11:12', 1234.5678, 1234.5678, 1234.5678, 1234.5678, 0, 0x123567890)
insert into freetds_types (int, long, smallint, tinyint, varchar, nvarchar, char, nchar, text, ntext, datetime, smalldatetime, money, smallmoney, real, float, bit, binary)
values (-2147483648, -9223372036854775808, -32768, 0, 'nije reko dobar dan','nije reko dobar dan 2','nije reko dobar dan 3','nije reko dobar dan 4','nije reko dobar dan 5','nije reko dobar dan 6', '1998-10-10T16:17:18','1998-10-10T16:17:18', -1234.5678, -1234.5678, -1234.5678, -1234.5678, 1, 0x0987654321abcd)
insert into freetds_types (int) values (3)
`, `
if exists(select * from sys.procedures where name = 'freetds_return_value')
drop procedure freetds_return_value
`, `
create procedure freetds_return_value as
return -5`}
func ConnectToTestDb(t *testing.T) *Conn {
conn, err := NewConn(testDbConnStr(1))
if err != nil {
t.Errorf("can't connect to the test database")
return nil
}
return conn
}
func testDbConnStr(maxPoolSize int) string {
connStr := os.Getenv("GOFREETDS_CONN_STR")
mirror := os.Getenv("GOFREETDS_MIRROR_HOST")
if mirror != "" {
connStr = fmt.Sprintf("%s;mirror=%s", connStr, mirror)
}
connStr = fmt.Sprintf("%s;max_pool_size=%d", connStr, maxPoolSize)
return connStr
}
func IsMirrorHostDefined() bool {
return os.Getenv("GOFREETDS_MIRROR_HOST") != ""
}
func TestConnect(t *testing.T) {
conn := ConnectToTestDb(t)
if conn == nil {
return
}
defer conn.Close()
if !conn.isLive() {
t.Error()
}
if conn.isDead() {
t.Error()
}
}
func TestItIsSafeToCloseFailedConnection(t *testing.T) {
conn := new(Conn)
if conn == nil {
return
}
if conn.isLive() {
t.Error()
}
if !conn.isDead() {
t.Error()
}
conn.Close()
}
func TestCreateTable(t *testing.T) {
conn := ConnectToTestDb(t)
if conn == nil {
return
}
defer conn.Close()
for _, s := range CREATE_DB_SCRIPTS {
_, err := conn.Exec(s)
if err != nil {
t.Error(err)
}
}
}
func TestStoredProcedureReturnValue(t *testing.T) {
conn := ConnectToTestDb(t)
if conn == nil {
return
}
defer conn.Close()
results, err := conn.Exec("exec freetds_return_value")
if err != nil {
t.Error(err)
}
if results[0].ReturnValue != -5 {
t.Errorf("expected return value was -5 got %d", results[0].ReturnValue)
}
}
func TestReading(t *testing.T) {
conn := ConnectToTestDb(t)
if conn == nil {
return
}
defer conn.Close()
//text i ntext columns is not readed properly
results, err := conn.Exec(`select * from freetds_types`)
printResults(results)
if err != nil || len(results) != 1 {
fmt.Printf("error: %s\n%s\n%s", err, conn.Message, conn.Error)
return
}
}
func TestRetryOnKilledConnection(t *testing.T) {
conn1 := ConnectToTestDb(t)
conn2 := ConnectToTestDb(t)
if conn1 == nil || conn2 == nil {
return
}
pid1, _ := conn1.SelectValue("select @@spid")
conn2.Exec(fmt.Sprintf("kill %d", pid1))
if conn1.isLive() {
t.Error()
}
if !conn1.isDead() {
t.Error()
}
_, err := conn1.exec("select * from authors")
if err == nil {
t.Error()
}
rst, err := conn1.Exec("select * from authors")
if err != nil || len(rst) != 1 || len(rst[0].Rows) != 23 || rst[0].RowsAffected != 23 {
t.Error()
}
}
func TestExecute(t *testing.T) {
conn := ConnectToTestDb(t)
if conn == nil {
return
}
defer conn.Close()
rst, err := conn.Exec("select 1")
if rst == nil || err != nil {
t.Error()
}
if len(rst) != 1 {
t.Error()
}
rst, err = conn.Exec("select missing")
if rst != nil || err == nil {
t.Error()
}
rst, err = conn.Exec("print 'pero'")
if err != nil || !strings.Contains(conn.Message, "pero") || len(rst) != 1 || len(rst[0].Rows) > 0 {
t.Error()
}
rst, err = conn.Exec("sp_help 'authors'")
if err != nil || len(rst) != 9 {
t.Error()
}
}
func TestRowsAffected(t *testing.T) {
conn := ConnectToTestDb(t)
if conn == nil {
return
}
defer conn.Close()
rst, err := conn.Exec("select * from authors")
if err != nil || len(rst) != 1 || len(rst[0].Rows) != 23 || rst[0].RowsAffected != 23 {
t.Error()
}
rst, err = conn.Exec("update authors set zip = zip")
if err != nil || len(rst) != 1 || len(rst[0].Rows) > 0 || rst[0].RowsAffected != 23 {
t.Error()
}
}
func TestSelectValue(t *testing.T) {
conn := ConnectToTestDb(t)
if conn == nil {
return
}
defer conn.Close()
val, err := conn.SelectValue("select 1")
if val.(int32) != 1 || err != nil {
t.Error()
}
val, err = conn.SelectValue("select 1 where 1=2")
if val != nil || err == nil {
t.Error()
}
val, err = conn.SelectValue("select missing")
if val != nil || err == nil {
t.Error()
}
}
func TestDbUse(t *testing.T) {
conn := ConnectToTestDb(t)
if conn == nil {
return
}
defer conn.Close()
err := conn.DbUse()
if err != nil {
t.Error()
}
conn.database = "missing"
err = conn.DbUse()
if err == nil && !strings.Contains(err.Error(), "unable to use database missing") {
t.Error()
}
}
func TestMirroring(t *testing.T) {
if !IsMirrorHostDefined() {
t.Skip("mirror host is not defined")
}
conn := ConnectToTestDb(t)
if conn == nil {
return
}
defer conn.Close()
rst, err := conn.Exec("select * from authors")
if err != nil && rst != nil && len(rst) == 1 && len(rst[0].Rows) == 23 {
t.Error()
}
err = failover(conn)
if err != nil {
fmt.Printf("failover error %s %s %s\n", err, conn.Error, conn.Message)
t.Error()
}
rst, err = conn.Exec("select * from authors")
if err != nil && rst != nil && len(rst) == 1 && len(rst[0].Rows) == 23 {
t.Error()
}
}
func failover(conn *Conn) error {
_, err := conn.Exec("use master; ALTER DATABASE pubs SET PARTNER FAILOVER")
time.Sleep(1e9)
return err
}
func BenchmarkConnectExecute(b *testing.B) {
for i := 0; i < 100; i++ {
conn := ConnectToTestDb(nil)
conn.Exec("select * from authors")
conn.Close()
}
}
func BenchmarkParalelConnectExecute(b *testing.B) {
pool := make(chan int, 5) //connection pool for x connections
running := 0
for i := 0; i < 100; i++ {
go func(i int) {
pool <- i
running++
//fmt.Printf("starting %d\n", i)
conn := ConnectToTestDb(nil)
defer conn.Close()
conn.Exec("select * from authors")
<-pool
running--
//fmt.Printf("finished %d\n", i)
}(i)
}
for {
time.Sleep(1e8)
fmt.Printf("running %d\n", running)
if running == 0 {
break
}
}
}
func TestTransactionCommitRollback(t *testing.T) {
conn := ConnectToTestDb(t)
createTestTable2(t, conn, "test_transaction", "")
err := conn.Begin()
assert.Nil(t, err)
conn.Exec("insert into test_transaction values('1')")
conn.Exec("insert into test_transaction values('2')")
err = conn.Commit()
assert.Nil(t, err)
rows, err := conn.SelectValue("select count(*) from test_transaction")
assert.Nil(t, err)
assert.Equal(t, rows, 2)
//roollback
err = conn.Begin()
assert.Nil(t, err)
conn.Exec("insert into test_transaction values('3')")
err = conn.Rollback()
assert.Nil(t, err)
rows, err = conn.SelectValue("select count(*) from test_transaction")
assert.Nil(t, err)
assert.Equal(t, rows, 2)
}
func createTestTable2(t *testing.T, conn *Conn, name string, columDef string) {
if columDef == "" {
columDef = "id int not null identity, name varchar(255)"
}
sql := fmt.Sprintf(`
if exists(select * from sys.tables where name = 'table_name')
drop table table_name
;
create table table_name (
%s
)
`, columDef)
sql = strings.Replace(sql, "table_name", name, 3)
_, err := conn.Exec(sql)
assert.Nil(t, err)
}
//usefull for testing n
func printResults(results []*Result) {
fmt.Printf("results %v", results)
for _, r := range results {
if r.Rows != nil {
fmt.Printf("\n\nColums:\n")
for j, c := range r.Columns {
fmt.Printf("\t%3d%20s%10d%10d\n", j, c.Name, c.DbType, c.DbSize)
}
for i, _ := range r.Rows {
for j, _ := range r.Columns {
fmt.Printf("value[%2d, %2d]: %v\n", i, j, r.Rows[i][j])
}
fmt.Printf("\n")
}
}
fmt.Printf("rows affected: %d\n", r.RowsAffected)
fmt.Printf("return value: %d\n", r.ReturnValue)
}
}