forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_test.go
111 lines (96 loc) · 2.66 KB
/
async_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
// Copyright (c) 2021-2022 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"context"
"fmt"
"testing"
)
func TestAsyncMode(t *testing.T) {
ctx := WithAsyncMode(context.Background())
numrows := 100000
cnt := 0
var idx int
var v string
runTests(t, dsn, func(dbt *DBTest) {
rows := dbt.mustQueryContext(ctx, fmt.Sprintf(selectRandomGenerator, numrows))
defer rows.Close()
// Next() will block and wait until results are available
for rows.Next() {
if err := rows.Scan(&idx, &v); err != nil {
t.Fatal(err)
}
cnt++
}
logger.Infof("NextResultSet: %v", rows.NextResultSet())
if cnt != numrows {
t.Errorf("number of rows didn't match. expected: %v, got: %v", numrows, cnt)
}
dbt.mustExec("create or replace table test_async_exec (value boolean)")
res := dbt.mustExecContext(ctx, "insert into test_async_exec values (true)")
count, err := res.RowsAffected()
if err != nil {
t.Fatalf("res.RowsAffected() returned error: %v", err)
}
if count != 1 {
t.Fatalf("expected 1 affected row, got %d", count)
}
})
}
func TestAsyncModeCancel(t *testing.T) {
withCancelCtx, cancel := context.WithCancel(context.Background())
ctx := WithAsyncMode(withCancelCtx)
numrows := 100000
runTests(t, dsn, func(dbt *DBTest) {
dbt.mustQueryContext(ctx, fmt.Sprintf(selectRandomGenerator, numrows))
cancel()
})
}
func TestAsyncQueryFail(t *testing.T) {
ctx := WithAsyncMode(context.Background())
runTests(t, dsn, func(dbt *DBTest) {
rows := dbt.mustQueryContext(ctx, "selectt 1")
defer rows.Close()
if rows.Next() {
t.Fatal("should have no rows available")
} else {
if err := rows.Err(); err == nil {
t.Fatal("should return a syntax error")
}
}
})
}
func TestMultipleAsyncQueries(t *testing.T) {
ctx := WithAsyncMode(context.Background())
s1 := "foo"
s2 := "bar"
ch1 := make(chan string)
ch2 := make(chan string)
runTests(t, dsn, func(dbt *DBTest) {
rows1 := dbt.mustQueryContext(ctx, fmt.Sprintf("select distinct '%v' from table (generator(timelimit=>%v))", s1, 30))
defer rows1.Close()
rows2 := dbt.mustQueryContext(ctx, fmt.Sprintf("select distinct '%v' from table (generator(timelimit=>%v))", s2, 10))
defer rows2.Close()
go retrieveRows(rows1, ch1)
go retrieveRows(rows2, ch2)
select {
case res := <-ch1:
t.Fatalf("value %v should not have been called earlier.", res)
case res := <-ch2:
if res != s2 {
t.Fatalf("query failed. expected: %v, got: %v", s2, res)
}
}
})
}
func retrieveRows(rows *RowsExtended, ch chan string) {
var s string
for rows.Next() {
if err := rows.Scan(&s); err != nil {
ch <- err.Error()
close(ch)
return
}
}
ch <- s
close(ch)
}